Over a million developers have joined DZone.

Ruby Simple RSS Parsing

·
def fetch_rss_items(url, max_items = nil)
  %w{open-uri rss/0.9 rss/1.0 rss/2.0 rss/parser}.each do |lib|
    require(lib)
  end
  rss = RSS::Parser.parse(open(url).read)
  rss.items[0...(max_items ? max_items : rss.items.length)]
end
 
items = fetch_rss_items('http://www.digg.com/rss/index.xml', 5)
items.collect { |item| item.title }
=> ["Understanding AJAX - A Beginner's Guide",
    "Anti-cancer Compound In Beer", ...]
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}