The Feedtools library is a very comprehensive and powerful Ruby library for handling rss, atom, etc as well as caching. It makes creating, consuming and manipulating feeds a piece of cake.
It’s ideal for parsing RSS feeds in Ruby on Rails applications.
Assuming that you already have installed Ruby and Rubygem, simply run
gem install feedtools
to install the FeedTools gem
or download it from the FeedTools project page and put the untar/unzipped folder in the vendor/plugins directory of your app.
To do a very quick test, create a file called testfeed.rb and add the below code in it:
require "RubyGems" require "feed_tools" feedurls = 'http://www.sphred.com/combined_feed' # If you want to fetch more than one feed then comment the above feedurls variable and uncomment the below one. #feedurls = %w(http://feeds.feedburner.com/Sphred_top_10_feeds http://feeds.feedburner.com/Sphred_site_only http://feeds.feedburner.com/Sphred_site_feature) my_feeds = FeedTools::build_merged_feed feedurls my_feeds.title = 'Sphred.com Feed' my_feeds.copyright = 'SPhred' my_feeds.author = 'Nasir ' my_feeds.id = "http://www.sphred.com/combined_feed" File.open('./my_feeds.xml', 'w') do |file| file.puts my_feeds.build_xml() end
Running this file from IRB will create a my_feeds.xml file in your current directory with all the feed contents.
To show feeds on a website, you need to do a few things:
at the top of your controller
2) Create an action within this controller to show your feeds, lets call that action user_data
def user_data @feed = FeedTools::Feed.open(params[:feed_url]) # You can first test it with a static feed url like this #@feed = FeedTools::Feed.open('http://www.sphred.com/combined_feed') end
3) In the corresponding view add this code
<div class="feeds"> <h3> <a href="<%= h @feed.link %>"> <%= @feed.title %></a> </h3> <p><%= @feed.description %></p> <% for feed in @feed.items %> <div class="feed_item"> <h4> <a href="<%= h feed.link %>"> <%= feed.title %></a> </h4> <p><%= feed.description %></p> </div> <% end %> </div>
This is all good for development environment or for small apps but once you go to production environment then you have to think of caching to avoid hitting the feed server every time before displaying feed contents.