October 21, 2016
Hot Topics:

Consuming RSS Feeds with Ruby

  • March 11, 2008
  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Executing parserss.rb anew will produce the same output as before, in addition to output that looks like this:

<a href='http://www.wjgilmore.com/?p=39'>
   Great Rails Documentation Interface

Published on: Thu, 06 Mar 2008 11:09:50 -0500
I met local Rails guru Josh Schairbaum for lunch yesterday,
   and [...]
<a href='http://www.wjgilmore.com/?p=38'>
   Adding Multiple Markers with YM4R
Published on: Thu, 06 Mar 2008 09:39:29 -0500
Continuing the mini-series on Rails/YM4R and Google Maps, one of
   the most common problems [...]
<a href='http://www.wjgilmore.com/?p=37'>
   Changing the default Google Maps API Icon
Published on: Wed, 05 Mar 2008 09:52:28 -0500
I&#8217;m using Bill Eisenhauer&#8217;s awesome YM4R Rails [...]

Of course, you're free to apply liberal amounts of Ruby to the data before it's output. For instance, the default date format is not very user-friendly. One way to improve it is by using strftime, like so:

rss_html <<
   "Published on: #{item.date.strftime("%B %d, %Y")}
<br />"

This will result in the publication dates being formatted like this: March 05, 2008.

Dumping the Transformed RSS to an HTML file

Viewing the transformed RSS in a terminal window doesn't exactly improve your situation. Instead, you'll want to view the HTML in a browser. Fortunately, dumping the HTML to a file is easy. Just modify the previous code to look like this:

rss_html = ""

rss.items.each do |item|

   rss_html <<
      "<p><a href='#{item.link}'>#{item.title}
   </a><br />"

   rss_html <<
      "Published on: #{item.date.strftime("%B %d, %Y")}
   <br />"
   rss_html << "#{item.description}</p>"


File.open("wjgilmore.html", "w") do |f|
   f.write rss_html

Execute this revised script and then load the file that has been created (wjgilmore.html) into your browser. You'll see output similar to that shown in the following screenshot.

Figure 1: Converting the RSS feed to HTML.

Sorting Feeds According to Date

Most RSS feeds are sorted according to descending date order (newest on top); however, occasionally you'll encounter a feed that doesn't comply. Logically, you'll want to ensure it conforms to this logical ordering, and so you will need to account for it within your script. Or, perhaps you would for some reason rather read the posts in order of oldest first. Using Ruby's sort! method, making sure the feeds are sorted in ascending orderm is very simple:

rss.items.sort! {|a,b| a.date <=> b.date}

If you want to sort them according to oldest first, just add the following line after the above:


Where to From Here?

With great web-based aggregators such as Google Reader at your disposal, there's little reason to re-invent the wheel and create your own. However, there remain countless possibilities for integrating feeds into your own web site, or creating a new application that helps users filter information more efficiently than ever before. Hopefully, this tutorial will serve as a catalyst for creating the next RSS-driven solution!

About the Author

W. Jason Gilmore is a freelance web developer, consultant, and technical writer. He's the author of several books, including the best-selling "Beginning PHP and MySQL 5: Novice to Professional, Second Edition" (Apress, 2006. 913pp.).

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel