Parsing RSS with Ruby

by Andrew Stewart
published February 13, 2014

Last night, I decided to write a small script that involved parsing an RSS feed I’d fetched over HTTP.

I thought I’d need to install Nokogiri to be able to accomplish this, and wasn’t relishing having to manually construct XPath queries to find the data I wanted to extract from the RSS.

After browsing around for a bit, I found out that Ruby has a built-in RSS class, and that it’s actually pretty good.

Here’s a quick example of using it to print the name, URL, and description of every item in a feed:

require 'rss'
require 'httparty'

response = HTTParty.get 'http://coolwebsite.com/path/to/feed.rss'

feed = RSS::Parser.parse response.body

feed.items.each do |item|
  puts "Title: #{item.title}"
  puts "URL: #{item.link}"
  puts item.description
end

# => Title: Cool Podcast Episode
# => URL: http://coolwebsite.com/episodes/404
# => Description: This is a cool podcast episode and you should listen to it.

Fairly simple, but very useful if you don’t want to rely on third-party gems. Check out more about it here.