Question

I have been programming with Ruby and Rails for the last 6 months, and I am pretty much comfortable with it now. But, I never had to deal with RSS feeds before with my Rails app. For my current project, I have a display page which shows different market indices with their open, high, low , volume values. Now, I would like to bring RSS feeds from yahoo finance and want to show them in the same page. I want to fetch RSS feeds from this link http://finance.yahoo.com/rssindex and then I want to show them in my display page in my Rails app. While searching for a solution for this problem, I came across this : Generating RSS feed in Rails 3

But, I think this is for generating RSS feeds for one's app. I believe my need is different than this. I want to fetch RSS feeds and show them in my display page. For fetching RSS feeds I found a ruby gem called feedzirra which seems to be great. But, I am still a bit confused about how I will actually display the news feeds with my Rails app ? I mean, do I need to create any model, view and controller for that ? do I need to save the news feeds in my data base for displaying them in my display page ? Or, is there any way of just fetching using feedzirra and displaying them in my display page ? I am just not sure how to move forward from this position. What is the best Rails way of solving this problem ?

Was it helpful?

Solution

Ryan Bates has a railscast on one way to handle the problem: http://railscasts.com/episodes/168-feed-parsing

You should definitely have a model for this data. The goal should be to get the model handling the fetching and parsing of data.

If you aren't interested in saving information to your own database(as is shown in the railscast episode), beware you will incur a bit of a performance penalty for making a request to yahoo on every page load.

Here's a simple example of a method you could add to your model.

def self.lookup_stock_titles(stock_name)
  titles = Array.new
  Feedzirra::Feed.fetch_and_parse("http://feeds.finance.yahoo.com/rss/2.0/headline?s=#{stock_name}&region=US&lang=en-US").entries.each do |entry|
     titles << entry.title
  end
  return titles
end

This would give you an array of titles from the rss feed which you could integrate into a controller or view however you like.

<% ModelName.lookup_stock_titles("ABC").each do |title| %>
  <h2><%= title %></h2>
<% end %>

Also be sure that yahoo allows you to use their data the way you want, saving it to a database, number of requests per hour, what kind of credit do you need to give to them, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top