Feedjira

The Feedjira Ruby Gem

Feedjira (formerly Feedzirra) is a Ruby gem for fetching and parsing RSS feeds. Version 2.0 was recently released.

Getting Started

Feedjira is tested with Ruby version 1.9.3 and 2.x so like any Ruby gem, the first step is to install the gem:

1
$ gem install feedjira

Or add it to your Gemfile:

1
gem 'feedjira'

Fetching and Parsing

For many users, the fetch_and_parse method is what they use Feedjira for. This method takes a url and returns a Parser object:

1
2
3
url = "http://feedjira.com/blog/feed.xml" feed = Feedjira::Feed.fetch_and_parse url # => #<Feedjira::Parser::Atom...>

These feed objects have both the meta data for a feed and an entries collection that contains all the entries that were found:

1
2
3
4
5
6
feed.title # => "Feedjira Blog" feed.url # => "http://feedjira.com/blog" feed.entries # returns an array of Entry objects # => [<Feedjira::Feed::Entry ...>, <Feedjira::Feed::Entry ...>, ...]

These entry objects contain the data parsed from the feed XML:

1
2
3
4
5
entry = feed.entries.first entry.title # => "Announcing verison 1.0" entry.url # => "http://feedjira.com/blog/2014-02-12-announcing-version-10.html"

Just Parsing

The parsing functionality of Feedjira has been exposed so that it can be used in isolation:

1
2
3
4
xml = Faraday.get(url).body feed = Feedjira::Feed.parse xml feed.entries.first.title # => "Announcing verison 1.0"

Adding a feed parsing class

When determining which parser to use for a given XML document, the following list of parser classes is used:

You can insert your own parser at the front of this stack by calling add_feed_class, like this:

1
Feedjira::Feed.add_feed_class MyAwesomeParser

Now when you fetch_and_parse, MyAwesomeParser will be the first one to get a chance to parse the feed.

If you have the XML and just want to provide a parser class for one parse, you can specify that using parse_with:

1
Feedjira::Feed.parse_with MyAwesomeParser, xml

Adding attributes to all feeds types / all entries types

1
2
3
4
5
6
7
8
9
10
# Add the generator attribute to all feed types Feedjira::Feed.add_common_feed_element('generator') Feedjira::Feed.fetch_and_parse("http://www.pauldix.net/atom.xml").generator # => 'TypePad' # Add some GeoRss information Feedjira::Feed.add_common_feed_entry_element('geo:lat', :as => :lat) Feedjira::Feed.fetch_and_parse("http://www.earthpublisher.com/georss.php").entries.each do |e| p "lat: #[e.lat}, long: #{e.long]" end

Adding attributes to only one class

If you want to add attributes for only one class you simply have to declare them in the class

1
2
3
4
5
6
7
8
9
10
11
12
# Add some GeoRss information require 'lib/feedzirra/parser/rss_entry' class Feedjira::Parser::RSSEntry element 'geo:lat', :as => :lat element 'geo:long', :as => :long end # Fetch a feed containing GeoRss info and print them Feedjira::Feed.fetch_and_parse("http://www.earthpublisher.com/georss.php").entries.each do |e| p "lat: #{e.lat}, long: #{e.long}" end

Testing

Feedjira uses faraday to perform requests, so testing Feedjira is really about stubbing out faraday requests.

Projects that use Feedjira

Feedjira is used in some awesome projects around the web - from RSS readers to add-ons and everything in between. Here are some of them:

Note: to get your project on this list, simply send an email with your project’s details.