1. Computing
Send to a Friend via Email

Spotlight on RubyGems: The Simple-RSS RubyGem

Reading RSS Documents

By

The Simple-RSS RubyGem provides a simplified interface for reading RSS and Atom documents. You will find short, concise syntax and an easy way to take information from feeds and iterate over the elements in the document. Though this interface is rather simplistic--or example it has no XPath queries and can't write RSS documents--it's just what you need 99% of the time.

When parsing some file formats, you don't always need many features. For example, when parsing an RSS feed, you don't usually need all the features provided to you by a full-blown XML parser interface. Most of the time, you'll just be grabbing specific pieces of information or iterating over a number of elements in the document.

Installing

The Simple-RSS gem is installed as all other gems. There are no native extensions and it doesn't depend on any other gems.

$ gem install simple-rss

Basic Use

To parse an RSS feed, you first have to fetch it. While you could use the Net::HTTP interface, this is a bit wordy and verbose for our needs. Instead, simply require the open-uri module and any HTTP URLs passed to the open method will now be transparently opened using the Net::HTTP interface, and you don't have to worry about the details of it.

#!/usr/bin/env ruby
require 'open-uri'

puts open('http://0.tqn.com/6/g/ruby/b/rss2.xml').read

Once you have something to parse, pass this to the SimpleRSS.parse method.

#!/usr/bin/env ruby
require 'rubygems'
require 'simple-rss'
require 'open-uri'

rss = SimpleRSS.parse(
  open('http://0.tqn.com/6/g/ruby/b/rss2.xml').read
)

puts rss

Iterating Over Items

Now that you have a parsed document, you can then iterate over the items. Each item will have elements such as pubDate, title, link, etc that you can query. The following example program iterates over the RSS feed and prints the title and link for every item in the feed.

#!/usr/bin/env ruby
require 'rubygems'
require 'simple-rss'
require 'open-uri'

rss = SimpleRSS.parse(
  open('http://0.tqn.com/6/g/ruby/b/rss2.xml').read
)

rss.items.each do|i|
  puts "Title: #{i.title}"
  puts "Link: #{i.link}"
  puts "=" * 80
end
See More About
  1. About.com
  2. Computing
  3. Ruby
  4. Gems
  5. Simple RSS RubyGem: Spotlight on Ruby Gems

©2014 About.com. All rights reserved.