This post is by john from RailsTips - Home
Click here to view on the original site: Original Post
In which I release a new gem named Columbus that auto-discovers feed urls.
Sorry for the rush of posts the past few days. I’ve been feeling a bit inspired of late, and with the discovery of Jeweler, it is now easier to make a gem than it is to just let the code sit in ~/dev/ruby/ on my computer.
A few weeks ago, I showed how to follow redirects using net/http, but I provided no context really as to why I wanted to follow redirects. Basically, that code was one piece of some code I wrote to auto-discover feed urls for a given url.
I was a little more creative with the name this time than last time, calling it Columbus. Get it? Auto-discovery and Columbus was a discoverer. Yeah, you get it. I’m sure I don’t need to explain it. Right? Yeah.
There isn’t much code in the gem and using it is even easier.
# get the primary feed primary = Columbus.new('http://railstips.org').primary puts primary.url, primary.title, primary.body # get all the feeds Columbus.new('http://railstips.org').all
The first returns a single feed if one is found else nil. The second returns all the feeds found in an array. That probably doesn’t feel like much, but there is a lot more going on behind the scenes.
Behind the Scenes
- Gets the response for the passed in url.
- If the URL is a redirect, it follows the redirect up to 5 times to find the endpoint.
- Once it has the endpoint, it uses Hpricot to get all the link tags in the response body that appear to be RSS or Atom feeds.
- For each link tag found, it gets the response for the URL and once again follows redirects up to 5 times until it finds an endpoint.
- Once the endpoint for each feed is found, it returns the URL, the title and the response body for you to fart around with.
Once again, I used shoulda, matchy and fakeweb to do the testing. I didn’t need HTTParty, but I did break out an old friend Hpricot, which I haven’t used since XML parsing in the Twitter gem. Kind of funny that this is the first time I used Hpricot for its original intent, parsing HTML.
For now the gem is just up on Github so the usual routine will get your going.
sudo gem install jnunemaker-columbus --source http://gems.github.com
Hopefully someone finds it useful someday. 🙂 I’ve already got my mileage out of it.