Bringing the Rails Magic to Facebook

Posted by Eric
on Friday, November 02

While there’s a few plugin options for the Rubyist looking to write a Facebook application, none of them quite fit our needs. I opted to write one for internal use at Slantwise. Why? A fundemental difference between this code and the publicly available solutions is that its Rails-centric.

If you’re not planning on writing your app in Rails, this isn’t the library for you. The benefits to the Rails programmer is that they now have a Facebook interface that’s borderline indisguishable from other Rails code—meaning its understandable, enjoyable, and doesn’t require hours of pouring through Facebook’s API documentation.

Ruby, where art thou?

This first sample shows some of the library’s low-level functionality, and its pretty similiar to the other solutions out there. It also demonstrates the inherent problems with simply wrapping API calls.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  # Retrieves photos from the first album that's found for the current user on Facebook
  # Then publishes some of the photos as a news update in Facebook
  ...
  #Retrieve the photos
  recommended_album   = Facebook::API::Albums.get(:uid => current_user.facebook_uid).first
  @recommended_photos = Facebook::API::Photos.get(:aid => album[:aid])

  # Assign all the necessary API params for publishing to Facebook.
  # This is a more complex (and abbreviated) API call
  # A typical update call will take at least a few lines of code
  feed_body_template = render_to_string(:partial => 'facebook/_body_template')
  feed_body_general  = render_to_string(:partial => 'facebook/_body_general')
  ...
  recommended_photos_feed = { :actor => current_user.facebook_uid, 
                              :body_template => body_template,
                              :body_general   => ... ,
                              ... }
  
  #Publish the feed to Facebook
  Facebook::API::Feed.publish_templatized_action @recommended_photos_feed

Direct API calls are great in some instances, but not in most. Writing something like the following could easily take someone new to the Facebook API hours to figure out. It also takes the magic out of working in Rails—increasing development time and developer frustration and ultimately resulting in a lesser product. Let’s bring that magic back and put a smile on our developer’s face while we’re at it:

Hello, old friend

1
2
3
4
5
  
  recommended_album   = current_user.facebook.albums.find_by_name 'Halloween'
  @recommended_photos = recommended_album.photos

  update_facebook 'recommended_photos.feed'

The first line of code is a Facebook API call. It utilizes a basic ActiveRecord connection that’s adapted to run through the Facebook API, so the results behave exactly the same as any other ActiveRecord request.

The update_facebook method is a specialized version of render that looks for a template named ‘recommended_photos.feed’, fills it with data, and sends it to Facebook. In this case, the ’.feed’ extension maps to the Facebook API call Feed.publishTemplatizedActionOfUser. The rendering action is easily configurable to support any extension/API call combination.

Interested?

The plan is to release a development version of the plugin within the next week or two to iron out any glaring bugs, followed very shortly thereafter with a production version that you can freely use in your projects. I’ll announce the plugin release here when it’s available.

Get Involved

While I think I’m off to a good start, I’m looking for help. I’m on the look out for highly useful features. If you’re developing a Facebook application and have any problem areas, send me an example of some problem code and I’ll look into incorporating a solution into the plugin.

Comments

Leave a response

  1. Pete FordeNovember 02, 2007 @ 10:54 AM

    This is great news. You should set up some project infrastructure. SVN and a google group?

    I might be able to contribute to this.

  2. Evan FarrarNovember 02, 2007 @ 09:00 PM
  3. AriNovember 02, 2007 @ 10:36 PM

    I’ve been developing on and off in my freetime something very similar…

    Mine, conceptually is based around ActiveResource DSL

    Let me know if you are interested in sharing our code…

  4. Scott ThorpeNovember 05, 2007 @ 03:26 PM

    This is great. I would love to contribute in anyway possible. Please contact me.

    Thanks!

    —Scott

  5. EricNovember 05, 2007 @ 07:05 PM

    Pete: Great idea. I’ll set up some project infrastructure to make it easier for everyone to get involved. There’s now a google group at http://groups.google.com/group/facetime/ for further discussion.

    Evan: I’m not familiar with that project, but it looks interesting—seems to be focused on the Data Store API, which is something I haven’t added support for yet.

    Pete, Ari, Scott: I’m speeding up the developer release to give you more time to contribute before the production version is launched. The code will be made available towards the end of this week.

  6. Shane VitaranaNovember 05, 2007 @ 10:12 PM

    Eric- Facebooker shares a very similar philosophy to yours and includes a bunch of Rails-specific features, including view wrappers around FBML. It’s not just a wrapper around Facebook’s API. It has concrete models and abstracts out most of the Facebook specific requirements so the end product looks just like a regular Rails app.

    Will you have Album and Photo models as well? What type will Facebook::API::Albums.get return? Facebooker takes the approach of having a smart User model that retrieves stuff like albums that are specific to the user, i.e: recommended_album = facebook_session.user.albums.first.

    I urge you to give Facebooker a second look and your contributions could be a useful addition. The feed templates are usually no more than one line, due to character restrictions, so don’t you think making it a template is a little overkill? Maybe having templates and an inline version, like how RJS templates are done, would be a good idea.

    ActiveFacebook is specific to the Data Store API and I hope to check-in an intial version soon.

  7. Senthil NayagamNovember 07, 2007 @ 06:30 AM

    Hi,

    we are working on one Facebook implementation, and have faced all problems you have mentioned.

    we can also have helpers for generating FBML and pseudo-Ajax tags

    I am ready for development and testing.

    also interested if we can have some stuff for OpenSocial platoform as well

    Senthil

  8. James BebbingtonNovember 15, 2007 @ 11:16 AM

    Sounds great! Itching to have a play with this. How close are you to getting a release out?

  9. EricNovember 16, 2007 @ 02:06 PM

    Senthil, James: I’m also itching to get this released—currently working on improving the documentation and installation process to make it easier to get up and running. I will have it wrapped up by Monday and will make an announcement on the discussion list: http://groups.google.com/group/facetime/ . There will also be a little writeup and announcement here when I have a moment later in the week.

    Shane: Good questions, and thanks for providing some insight into Facebooker. While I was initially hesitant to create “yet another facebook library,” it’s proving to be the right decision for us, and perhaps it will be a good match for other developers as well, although I would encourage others to take a look at Facebooker to see if its a better fit for their needs.

    As to the questions, I’ll try to get into more depth later, but the short answer is Facebook::API::Albums.get returns a simple hash response, while requests through the user.facebook.albums association return an Album model that undergoes automatic typecasting. Also a very good point on the templating—it currently can’t handle the simplest cases without a template, which is something that really needs to be addressed. Looking forward to seeing the Date Store release!

    Sethil: Currently no plans to do anything with OpenSocial, but I am very interested in seeing a solution for views, especially javascript helpers. I really miss the visual effects, etc that come with Prototype and Scriptaculous. Facebook did say they have an animation library that’s on its way, but it sounded like it will be a while before its released.