Skin of Stars

Icon

Kevin Carmody on machines, media and miscellanea.

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

My Twitterings....

Powered by Twitter Tools

About

My name is Kevin Carmody and I live in Oxford, United Kingdom. I am a web developer with a penchant for community sites and a pedantry for open standards.

This here is a collection of my thoughts and musings, a spot for pooling a little of what's rattling around. Thanks for taking the time to visit and I hope you enjoy your stay.