Some time ago it was an elegant as Ruby RSS parser and generator intended to help you bring together Ruby and RSS in your project. But Ruby already has wonderful RSS parser and generator which must be used instead. Here I'll show you example of how to parse RSS, create it, and extend RSS::Rss class with to_html function.


How to parse RSS feed

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

source = "http://rubyforge.org/export/rss_sfnewreleases.php" # url or local file
content = "" # raw content of rss feed will be loaded here
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)
        

That's it. All necessary data from source file was loaded and parsed. Below is an example of how to use rss object.


puts "Root values"
print "RSS title: ", rss.channel.title, "\n"
print "RSS link: ", rss.channel.link, "\n"
print "RSS description: ", rss.channel.description, "\n"
print "RSS publication date: ", rss.channel.date, "\n"

puts "Item values"
print "number of items: ", rss.items.size, "\n"
print "title of first item: ", rss.items[0].title, "\n"
print "link of first item: ", rss.items[0].link, "\n"
print "description of first item: ", rss.items[0].description, "\n"
print "date of first item: ", rss.items[0].date, "\n"
        

How to create RSS feed

require 'rss/maker'

version = "2.0" # ["0.9", "1.0", "2.0"]
destination = "test_maker.xml" # local file to write

content = RSS::Maker.make(version) do |m|
m.channel.title = "Example Ruby RSS feed"
m.channel.link = "http://www.rubyrss.com"
m.channel.description = "Old news (or new olds) at Ruby RSS"
m.items.do_sort = true # sort items by date
  
i = m.items.new_item
i.title = "Ruby can parse RSS feeds"
i.link = "http://www.rubyrss.com/"
i.date = Time.parse("2007/2/11 14:01")

i = m.items.new_item
i.title = "Ruby can create RSS feeds"
i.link = "http://www.rubyrss.com/"
i.date = Time.now
end

File.open(destination,"w") do |f|
f.write(content)
end
        

This script will create test_maker.xml file in the current directory. Check it to see that it's a valid RSS 2.0 feed.


Ok, I can parse and create RSSs. Now I want to add content from RSS feed to my website. Let's create extension to RSS::Rss class - function called to_html. It will generate HTML code from parsed RSS feed based on template.


RSS feed to HTML

class RSS::Rss
  def to_html
    max_description_length = 100

    html = "<h4><a href='#{channel.link}'>#{channel.title}</a></h4>"
    html << "<small>Updated on #{channel.date.strftime('%m/%d/%Y')}</small>" \
            if channel.date
    html << "<p>#{channel.description}</p>"
    html << "<ol>"

    channel.items.each do |i|
      html << "<li><strong><a href='#{i.link}'>#{i.title}</a></strong><br/>"
      html << "<small>Added on #{i.date.strftime("%m/%d/%Y")} at \
#{i.date.strftime("%I:%M%p")}</small><br/>" if i.date
      desc_text = i.description.gsub(/<[^>]+>/,"").squeeze(" ").strip
      if desc_text.length > max_description_length
        desc_text = desc_text[0,max_description_length] + "&hellip;"
      else
        desc_text = i.description
      end
      html << "#{desc_text}"
      html << "</li>"
    end

    html << "</ol>"
    html
  end
end
        

If you are interested how RubyRSS was looked back in June 10, 2006 (latest release) check RubyRSS project at RubyForge. I'm happy to say that Robert Blum turned RubyRSS into ruby gem, which can be downloaded here: http://robertblum.com/downloads/rubyrss-1.1.gem. Thank you, Robert! :)