Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from pauldix/gist:57285
Created February 4, 2009 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kivanio/58062 to your computer and use it in GitHub Desktop.
Save kivanio/58062 to your computer and use it in GitHub Desktop.
require 'feedzirra'
# fetching a single feed
feed = Feedzirra::Feed.fetch_and_parse("http://feeds.feedburner.com/PaulDixExplainsNothing")
# feed and entries accessors
feed.title # => "Paul Dix Explains Nothing"
feed.url # => "http://www.pauldix.net"
feed.feed_url # => "http://feeds.feedburner.com/PaulDixExplainsNothing"
feed.etag # => "GunxqnEP4NeYhrqq9TyVKTuDnh0"
feed.last_modified # => Sat Jan 31 17:58:16 -0500 2009 # it's a Time object
entry = feed.entries.first
entry.title # => "Ruby Http Client Library Performance"
entry.url # => "http://www.pauldix.net/2009/01/ruby-http-client-library-performance.html"
entry.author # => "Paul Dix"
entry.summary # => "..."
entry.content # => "..."
entry.published # => Thu Jan 29 17:00:19 UTC 2009 # it's a Time object
# updating a single feed
updated_feed = Feedzirra::Feed.update(feed)
# an updated feed has the following extra accessors
updated_feed.updated? # returns true if any of the feed attributes have been modified. will return false if only new entries
updated_feed.new_entries # a collection of the entry objects that are newer than the latest in the feed before update
# fetching multiple feeds
feed_urls = ["http://feeds.feedburner.com/PaulDixExplainsNothing", "http://feeds.feedburner.com/trottercashion"]
feeds = Feedzirra::Feed.fetch_and_parse(feed_urls)
# feeds is now a hash with the feed_urls as keys and the parsed feed objects as values. If an error was thrown
# there will be a Fixnum of the http response code instead of a feed object
# updating multiple feeds. it expects a collection of feed objects
updated_feeds = Feedzirra::Feed.udpate(feeds.values)
# defining custom behavior on failure or success. note that a return status of 304 (not modified) will call the on_success handler
feed = Feedzirra::Feed.fetch_and_parse("http://feeds.feedburner.com/PaulDixExplainsNothing",
:on_success => lambda {|feed| puts feed.title },
:on_failure => lambda {|url, response_code, response_header, response_body| puts response_body })
# if a collection was passed into fetch_and_parse, the handlers will be called for each one
# the behavior for the handlers when using Feedzirra::Feed.update is slightly different. The feed passed into on_success will be
# the updated feed with the standard updated accessors. on failure it will be the original feed object passed into update
# Defining custom parsers
# TODO: the functionality is here, just write some good examples that show how to do this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment