del.icio.us on Rails (and other XML feeds)

Here’s some quick and dirty information on how to include an XML feed into your ruby site.

Install & Implement Gem

Begin by installing the feed_tools gem.

gem install feedtools

In the environment.rb file, include at the bottom:

require 'feed_tools'

Using the Gem

In your controller, pick a method (mine sits inside view) and implement a new feed: (This pulls my del.icio.us feed)
@feed = FeedTools::Feed.open("http://del.icio.us/rss/matthewvb")

Then, I like to add some pagination, cause I don’t want all the info, just the latest:

@pages, @items = paginate_collection @feed.items, :page => @params[:page]

Finally, render it partially (this will display each instance of your XML data — basically it does the looping through the array for you)
render :partial => "feed"

Pagnation

I used a pagination above which pulls from application.rb (The application controller). I pulled this online.

def paginate_collection(collection, options = {})
default_options = {:per_page => 5, :page => 1}
options = default_options.merge options
pages = Paginator.new self, collection.size, options[:per_page], options[:page]
first = pages.current.offset
last = [first + options[:per_page], collection.size].min
slice = collection[first...last]
return [pages, slice]
end

Note the second line; here is where you can specify how many items to display and how many pages. I just pulled 5 to keep the list short.

View

In the partial rendering file (_feed.rhtml), I have the following code, specific to the feed I’m pulling:

<% for item in @items %>
<%= item.description %>
<% end %>

Here’s what’s happening:

First, we’ll set up our loop for going through all the items in the array. @items was defined in the controller

<% for item in @items %>

Finally, I display the description tag of the XML file and close my loop.
<%= item.description %>
<% end %>

XML File Sample

Here’s what some of the XML looks like that we have pulled:

You can pull all these things just by using the item.____ command.

Enjoy pulling feeds! (See my info on implementing flickr on ruby for more info on that.)

Note: WordPress didn’t like me using so much code in this posts — I’ve tried to clean up where it got deleted, but the XML isn’t playing nice. Just go to del.icio.us/rss/matthewvb to get an example of the XML.

Update: 1.2.07: I noticed this post wasn’t allowing for any future posts to go thru. I deleted the XML sample from this post, but you can still see an example at the link above. Sorry. Just ask with questions.

2 Responses to del.icio.us on Rails (and other XML feeds)
  1. Patrick Reagan
    December 29, 2006 | 4:49 pm

    Thanks for the snippet, Matt. This should come in handy for some RSS aggregation we’re doing for a couple clients.

  2. [...] 01/02/2007 — Patrick Reagan After reading a post from local blogger Matthew Van Boogart, I took a look at Bob Aman’s FeedTools to pull in RSS feeds from Google News to support a client project that I’m working on. The process required to consume the feed was really trivial – after a couple hours I had a barebones feed reader complete with unit and functional tests. [...]

Leave a Reply

Wanting to leave an <em>phasis on your comment?

Trackback URL http://www.matthewvb.me/2006/12/20/delicious-on-rails-and-other-xml-feeds/trackback/