***CONTROLLER***:
source = "http://#{city}.craigslist.org/#{category}/index.rss" #
url or local file
content = "" # raw content of rss feed will be loaded here
open(source) do |s| content = s.read end
craigslist_rss_feed = RSS::Parser.parse(content, false)
***VIEW***:
<!-- Craigslist Category Title -->
<h1> <%= "RSS title: #{craigslist_rss_feed.channel.title}" %> </h1>
<br>
<!-- Load All Craigslist ads for City & Category -->
<% for rss_item in craigslist_rss_feed.items do %>
<a href='<%= "#{rss_item.link}" %>'> <%= rss_item.title %> </a><br>
<%= rss_item.description %><br><br>
<% end %>
Thanks,
Frank
Ruby 1.8:
http://pragprog.com/titles/ruby/programming-ruby
Ruby 1.9
http://www.pragprog.com/titles/ruby3/programming-ruby-1-9
If you are going to parse RSS in the view, you should use a helper.
Also, it generally isn't a good idea to hit the RSS feed every time a
page is visited. Cache it and only check it every so often (how often
to be dependent on the nature of the feed and how time-sensitive it
is).
Cheers,
Daniel
I hope this helps someone else out. I couldn't find any good articles
when googling on how to parse RSS feeds. Found some that were pretty
vague and some that didn't even work. When mixed all together, here's
what worked for parsing a Craigslist RSS feed. If you can improve the
code, please do so and help us all out =).
***CONTROLLER***:
source = "http://#{city}.craigslist.org/#{category}/index.rss" #
url or local file
content = "" # raw content of rss feed will be loaded here
open(source) do |s| content = s.read end
craigslist_rss_feed = RSS::Parser.parse(content, false)
***VIEW***:
<!-- Craigslist Category Title -->
<h1> <%= "RSS title: #{craigslist_rss_feed.channel.title}" %> </h1>
<br>
<!-- Load All Craigslist ads for City & Category -->
<% for rss_item in craigslist_rss_feed.items do %>
<a href='<%= "#{rss_item.link}" %>'> <%= rss_item.title %> </a><br>
<%= rss_item.description %><br><br>
<% end %>
Thanks,
Frank
--
==========================================================
You received this message because you are subscribed to the Google
Groups "Nashville Ruby Group" group.
To post to this group, send email to
nashville-...@googlegroups.com
To unsubscribe from this group, send email to
nashville-ruby-g...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nashville-ruby-group?hl=en
None, unless your using it in more than one place. Do you plan to
read more than one feed, for example.
> When you mean 'cache' do you mean use something like the following? If not,
> how should it be coded?
> source ||= "http://#{city}.craigslist.org/#{category}/index.rss"
Cache the RSS data, in a local file or in your database. The point is
to only make the remote call periodically.
--
Greg Donald
destiney.com | gregdonald.com
--
When the cache is outdated, the RSS feed is read and parsed anew. It
is parsed into an array that is then passed into
render_content_from_rss_array. Render_content_from_rss_array is in the
controller specific helper, so we get a form of duck typing: the call
to render_content_from_rss_array is called from application_helper.rb,
but the method is defined in any of the other controller specific
helper files, enabling the custom rendering code to be kept in its
proper helper.
If the connection to the feed fails, or if parsing fails, then the
cached content is used, and the cache is updated so that we only try
again after the time delay elapses again (otherwise, we'd continue to
hit the problematic resource every time the page were displayed; a
cache isn't much good if it only saves your system resources when the
target resource is functioning properly).
Here is a gist of the RSS methods in my application_helper.rb:
http://gist.github.com/292834
Cheers,
Daniel
Cheers,
Daniel