Google's OpenSocial with Ruby on Rails
November 7th, 2007
Given the fair number of web applications we have developed with some aspect of social networking involved, naturally we were pretty excited about Google's OpenSocial announcement.
Looking into the OpenSocial documentation you can see that it exposes 3 different types of resources: people, that person's friends, and their activities. This structure maps nicely onto Rail's concept of nested resources, where we could have a top-level users controller with friends and activities nested underneath it.
The OpenSocial API looks quite well thought out, and built upon RESTful ideas. However it doesn't fit completely with the Rails ideals. Firstly the URL's for accessing the resources aren't the standard Rails routes and secondly it doesn't include a format extension, which means we can't use a responds_to block out of the box.
However both of these problems are easily solved by adding a few mappings to the routes file and adding our own custom mimetype. The example below shows the routing for mapping the users controller to the OpenSocial people resource.
# config/environment.rb
Mime::Type.register "application/x-opensocial", :opensocial
# config/routes.rb
map.opensocial_person '/feeds/people/:id', :controller => 'users', \
:action => 'show', :format => 'opensocial',\
:conditions => {:method => :get}
map.opensocial_person '/feeds/people/:id', :controller => 'users',\
:action => 'update', :format => 'opensocial', \
:conditions => {:method => :put}
map.opensocial_person '/feeds/people/:id', :controller => 'users', \
:action => 'destroy', :format => 'opensocial', \
:conditions => {:method => :delete}
# app/controllers/users_controller.rb
respond_to do |format|
...
format.opensocial
end
Now you can render show.opensocial.erb or show.opensocial.builder to create atom feeds that the OpenSocial API requires.
To avoid repeating it throughout a number of applications I wrapped it up into a little plugin that just sets the routes for those three resources, like so.
# config/routes.rb
map.resources :users do |user|
user.resources :activities
user.resources :relationships
end
map.opensocial_people :users do |user|
user.opensocial_friends :relationships
user.opensocial_activities :activities
end
Download Plugin
8 Responses to “Google's OpenSocial with Ruby on Rails”
Sorry, comments are closed for this article.






Hi Matt,
Thanks for this plugin .. I will surely give it a try soon once I start writing my open social app :)
Just trying to get a grip on all the standards used for writing the app (such as Google Gadget stuff and the API)
Regards Prateek Dayal Muziboo.com
Let me ask you a very newbie question: What purpose does linking Rails with OpenSocial serve? OpenSocial widgets can’t be Rails-based, right? So how does one work with the other?
Thanks for sharing. We are oh so dry :)
How is this plugin used with the current javascript api?
This doesn’t have anything to do with the Javascript API.
The technique allows you to expose your data (users, and their friends and activities) in such a way that allows DRY development using rails conventions. This means that potentially you could host OpenSocial applications on your rails-based social network.
thanks for explaining that.
I am newbie to ROR and OpenSocial …
Is there any example on how to use this plugin exactly with OpenSocial? (like working example) ...
Thanks for your help …
Good work olly.
We are working on a plugin for making a Rails application an OpenSocial container. After taking a look at this plugin, it might make sense to leverage this for supporting the Javascript API calls. I’ll keep you updated on the progress and see how we can coordinate.