0

Integrating Google Shared Items with Rails

Posted on 10 February 2008

by Robin Fisher

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’

Next, set up some variables that we will use throughout the method:

content
will be used to contain our shared items
days
used to calculate how old the oldest shared item will be
time
used to make sure each entry is not too old
share_url
the RSS feed for the shared items

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 << "

  • \n"
    content << " #{entry.title}\n"
    content << "

    #{entry.find_node(“source/title/text()”).to_s}\n"
    content << "

  • \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 = “

      ” + content
      content << “
    \n”
    else
    content = “

    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.

    There are no comments yet. Why not be the first?