Skin of Stars

Machines, Media and Miscellanea

by Kevin Carmody

Ruby On Rails, RSS and Atom feed parsing with Feed Normalizer and subsequent storage

I’ve battled for days on this, but I now finally know how to parse feeds and store them in a database in Ruby On Rails. This won’t be of much interest to the casual reader, but if you are scouring the web for an answer (as I was) then you will probably find this very useful:


class Feed < ActiveRecord::Base
require_association 'post'
require 'feed-normalizer'
require 'open-uri'
require 'rss/2.0'

belongs_to :user
has_many :posts, :dependent => :destroy

#put some other stuff here for feed validation etc

def refresh_all
refresh(Feed.find(:all))
end

def refresh(feeds)
feeds.each do |feed|
rss = FeedNormalizer::FeedNormalizer.parse open(feed.uri)
rss.entries.each do |item|
post = Post.new(:feed_id => feed.id)
post.link = item.url or raise "post has no link tag"
post.title = item.title or "no title"
post.content = item.content or "no text"
post.created_at = item.date_published if item.date_published
post.save
end
end
end

end

Comments

By santosh on

is this code to read the RSS feed from different sites

By Kevin on

Yeah. Basically, you have a collection of RSS feeds, you parse them using Feed Normalizer and then use them as post objects however you please. I’m not sure I would recommend using Feed Normalizer now though. I think Nokogiri (http://nokogiri.org/) is what the cool kids are using these days :)

Good luck.

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . To display code, manually escape it.