DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Simple RSS Parsing

05.28.2007
| 6493 views |
  • submit to reddit
        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", ...]