Suffix

Google Social Graph

Building a social graph with Ruby on Rails Google’s Social Graph API.

Check the social graph in the sidebar on the about page (the website has been redesigned and the sidebar has been removed now). It shows a bunch of links to websites elsewhere that are loosely related to me, my LinkedIn profile or Flickr photos for example, or websites of friends. These links are automatically collected based on XFN and FOAF data via the Google Social Graph API.

A Social Graph

A social graph consists of who and how an individual is connected to. In the internet world people use XFN and FOAF to describe their connections with other people’s websites. These websites can be my other websites (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="https://flickr.com/photos/schoeters" rel="me">My photos</a>

The XFN examples above describe my connection to Jelle Vandebeeck’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. What if we could visualize all these online connections?

Google released the Social Graph API which crawls their index for all XFN and FOAF enabled links. Their API returns a JSON response with all these links and the connection types associated with it. Since it uses the Google index you have fast access to massive amounts of data. The correctness of the data depends on the freshness of the Google index of course. 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 earlier blog post aren’t included yet.

Ruby on Rails Implementation

How do we build our own social graph in Ruby on Rails? You’ll need the JSON gem to parse the Google response (might be included in Rails 2, not sure).

gem install json

Next, we ask Google for our social graph. This builds an array with all the pages on a webpage.

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!

We remove the duplicates at the end and there you have it, an array with all your XFN and FOAF friends linking to the given URLs. You can play 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.
edi true, false, 1 or 0 Return a list with pages linking to the given URL.
edo true, false, 1 or 0 Return a list with pages linked from the given URL.