Integrating Google Shared Items with Rails
Written by Robin Fisher in 252 words
10
Feb
As part of the home page on captured sparks, I wanted to have a “Recent Reads” section where I could list the articles that have caught my attention recently. I do most of my reading in Google Reader and thought that the Shared Items feature in Reader would be perfect for this. Thanks to the FeedTools gem, adding this was straightforward.
Firstly, grab the FeedTools gem, using sudo if necessary:
gem install feedtools
The way I added this function was to use a helper method inside my home_helper.rb call shared_items. Firstly require the gem in the head of the helper:
gem 'feedtools' require 'feed_tools'
Setting up the method now looks like:
content = '' days = 15 time = Time.now.utc - (86400 * days.to_i) share_url = 'http://www.google.com/reader/public/atom/user/13411963057092720812/state/com.google/broadcast'
To create the shared item list:
feed = FeedTools::Feed.open(share_url)
feed.entries.each do |entry|
if entry.published > time
content << " <li>\n"
content << " <a href='#{entry.link}'>#{entry.title}</a>\n"
content << " <p />#{entry.find_node("source/title/text()").to_s}\n"
content << " </li>\n"
end
end
Finally, check to see if anything is shared and add the appropriate HTML container (or witty comment if nothing is shared), then return the content.
if content.length > 0 content = "<ul>" + content content << "</ul>\n" else content = "<p />Not read anything of interest recently." end return content
The helper method is then called in the view by simply adding:
<%= shared_items %>
You can read more about FeedTools in the FeedTools API.