Read my latest article: Ruby 1.8.7 on MacPorts causing some problems (posted Fri, 20 Jun 2008 21:11:00 GMT)

Parsing a RSS Feed 8

Posted by Robby Russell Wed, 11 May 2005 21:44:00 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 …

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

Comments