Read my latest article: Planet Argon Blog (posted Wed, 17 Feb 2010 15:11:00 GMT)

Parsing a RSS Feed

Posted by Robby Russell Wed, 11 May 2005 21:44:00 GMT

23 comments Latest by Neil Chandler Thu, 04 Feb 2010 10:43:09 GMT

A friend was asking me how they could easily read a RSS feed and display the last x items in their rails project. This was my quick and dirty response.


require 'rss/2.0'
require 'open-uri'

class RssfeedController < ApplicationController

  def index
    feed_url = 'http://www.planetrubyonrails.org/xml/rss'
    output = "<h1>My RSS Reader</h1>" 
    open(feed_url) do |http|
      response = http.read
      result = RSS::Parser.parse(response, false)
      output += "Feed Title: #{result.channel.title}<br />" 
      result.items.each_with_index do |item, i|
        output += "#{i+1}. #{item.title}<br />" if i &lt; 10  
      end  
    end
    render_text output
  end

end

Is there an easier way to do this with another RSS library? I figured that the simplest method would be to just use the standard library that comes with Ruby.

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    blah Sat, 03 Jun 2006 23:45:43 GMT

    blah blah blah. wanna buy some erection cream?

  2. Avatar
    Pascal Van Hecke Sun, 08 Oct 2006 13:46:26 GMT

    I found this googling for info on the simple-rss gem. Will try to write how I used it on http://ruby.vanhecke.info

  3. Avatar
    Patrick Crowley Wed, 28 Feb 2007 19:26:30 GMT

    Note: For this to work, you need to edit the last part of line 14. (It has an incorrectly encoded less-than sign between if i and 10.

  4. Avatar
    Baachus Thu, 26 Apr 2007 11:43:33 GMT

    Here’s a version you can pop in you lib and call from your pages… cheers

    
      require 'rss/2.0'
      require 'open-uri'
    
      class RssReader
    
        def parseFeed (url, length)
          feed_url = url
          output = "";
          open(feed_url) do |http|
            response = http.read
            result = RSS::Parser.parse(response, false)
            output = "<span class=\"feedTitle\">#{result.channel.title}</span><br /><ul>" 
            result.items.each_with_index do |item, i|
              output += "<li><a href=\"#{item.link}\">#{item.title}</a></li>" if ++i < length  
            end 
            output += "</ul>" 
          end
          return output
        end
    
      end
    
    
  5. Avatar
    Anlek Mon, 18 Jun 2007 19:25:37 GMT

    Wow, I’ve been looking for this all day! Thank you!

  6. Avatar
    arj Fri, 05 Oct 2007 01:11:33 GMT

    i’m the type of guy that likes these kind of posts

  7. Avatar
    HappyNoff Fri, 21 Dec 2007 11:53:54 GMT
    Just note that if you want to read RSS 1.0, you need to add
    require 'rss/1.0.rb'
    
    it will work just like it… nothing else…
  8. Avatar
    HappyNoff Fri, 21 Dec 2007 11:54:58 GMT
    Sorry, only
    require 'rss/1.0'
    
    without .rb …
  9. Avatar
    chaos Thu, 17 Jul 2008 19:14:59 GMT

    thanks!

  10. Avatar
    chaos Thu, 17 Jul 2008 19:15:02 GMT

    thanks!

  11. Avatar
    chaos Thu, 17 Jul 2008 19:15:06 GMT

    thanks!

  12. Avatar
    chaos Thu, 17 Jul 2008 19:15:07 GMT

    thanks!

  13. Avatar
    Jason Green Wed, 30 Jul 2008 08:38:50 GMT

    Thanks Robby, really useful.

  14. Avatar
    Veez Wed, 30 Jul 2008 20:38:44 GMT

    I refactored this to be more railsy:

    
    require 'rss/2.0'
    require 'open-uri'
    
    class RssReader
    
      def self.posts_for(feed_url, length=2, perform_validation=false)
        posts = []
        open(feed_url) do |rss|
          posts = RSS::Parser.parse(rss, perform_validation).items
        end
        posts[0..length - 1] if posts.size > length
      end
    
    end
    

    In this case, the view (in haml) would be:

    
    %ul{:id => 'recent_blog_posts'}
    - @posts.each do |post|
      %li
        = post.title
        %br/
        = post.description
    
  15. Avatar
    Dimitri Mallis Sat, 23 Aug 2008 21:00:56 GMT

    thanks for that quickie :)

  16. Avatar
    Andrew Sat, 06 Sep 2008 00:09:21 GMT

    Can I get a link for that erection cream?

  17. Avatar
    naruto Sat, 13 Sep 2008 02:01:05 GMT

    how to parser RSS with Ajax on Rails?

  18. Avatar
    Kevin Mon, 20 Oct 2008 19:05:20 GMT

    Not to be grammatically picky, but one would say ‘an RSS feed’ as the pronunciation of letter R starts with a vowel sound.

    Otherwise, a useful titbit, thanks :)

  19. Avatar
    Transporter Sun, 09 Nov 2008 21:57:42 GMT

    More than useful, small, dry, working. Thanks for that piece.

    Cheers, Jason

  20. Avatar
    Clément Mon, 19 Jan 2009 17:24:46 GMT

    The post is excellent, the comments make it even more usefull. Did you know you are nice guys ? :-)

  21. Avatar
    etagwerker Tue, 28 Apr 2009 02:05:01 GMT

    Hi, this is very useful.

    I will had to add some rescue statements for what I want to do, but this is a good start.

    Thank you!

  22. Avatar
    grosser Thu, 28 May 2009 19:03:40 GMT

    this can also be done with acts_as_feed http://github.com/grosser/acts_as_feed , which will represent the feed as feed model or as addition to a model, and parsing can be offloaded to a background process.

  23. Avatar
    Neil Chandler Thu, 04 Feb 2010 10:43:09 GMT

    Just a quick update. The last line

    posts[0..length – 1] if posts.size > length

    will only return posts if the rss returns a number of posts > length otherwise it returns nil

    posts.size > length ? posts[0..length – 1] : posts

    should be used to return all posts when the total available posts is less the length

Share your thoughts... (really...I want to hear them)

Comments