Suffix

Published by Simon Schoeters

Google Social Graph in RoR

Have a look at my social graph in the sidebar on the about page. It shows a bunch of links to websites elsewhere on the web that are loosely related to me (e.g. my LinkedIn profile or Flickr photos) or my “friends”. These links are automatically collected based on XFN and FOAF data with the Google Social Graph API.

What?

A social graph consists of who an individual is connected to based on the type of connections. In the internet world people use XFN and FOAF to describe their connections with other peoples websites. These websites can be other websites of myself (such as my Flickr photo page) or a website of a friend of mine.

<a href="http://fousa.be" rel="friend met">Jelle</a>
<a href="http://flickr.com/photos/schoeters" rel="me">My photos</a>

The previous 2 XFN examples describe my connections with Jelle 's website, a friend of mine, and my Flickr photo page. Now, I'm not the only one using this, thousands of links (blogs, LinkedIn profiles, Twitter profiles, etc.) have been marked up in this way. Wouldn't it be nice when we could visualize all these online connections?

Google recently released their Social Graph API which crawls the Google index for all XFN and FOAF enabled links based on a list of URL's. This returns a JSON response with all these links and the connection types associated with it. As the API is using the Google index you have access to a huge amount of data in a very fast way. On the downside the correctness of the data depends on the freshness of the Google index. Today my Twitter profile link still shows up in the results even though I removed my account a few months ago and the links in my previous blog post aren't found yet.

Ruby on Rails implementation

Let's have a look at the Ruby on Rails code to build our own social graph. First you'll need the JSON gem to parse the response from Google (the may be different in Rails 2.x, I didn't check).

gem install json

Great, now lets ask Google for our social graph. I built an array with all the pages on this blog but for the sake of the example I will use a static array with some sample links.

require 'open-uri'
require 'json'

links = ['http://www.example.com/','http://www.example.com/example']
url = "http://socialgraph.apis.google.com/lookup?q=" + links.join(',') + "&amp;fme=0&amp;edi=1&amp;edo=0"
resp = Net::HTTP.get_response(URI.parse(url))
result = JSON.parse(resp.body)

graph = Array.new
for node in result['nodes']
  for sub_node in node[1]['nodes_referenced_by']
    graph << [ sub_node[0], sub_node[1]['types'] ]
  end
end
graph.uniq!

At the end we remove the duplicate URL's and there you have it, an array with all you XFN and FOAF friends linking to the given URL's. You can play around with the 3 parameters at the end of the Google URL:

fme true, false, 1 or 0 Follow the ‘me’ links found on the given URL's.
edi true, false, 1 or 0 Return a list with pages linking to the given URL's.
edo true, false, 1 or 0 Return a list with pages linked from the given URL's.

Resources