Great Plugin for Facebook Apps

July 27th, 2007

If you’ve been working through the Facebook/Rails tutorials you might find this plugin useful.

Facebook on Rails is a sexy plugin for developing Facebook apps

It adds some useful functions to Rails for creating a Facebook application.

acts_as_fb_user


class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.column :uid, :integer, :null => false
      t.column :session_key, :string
    end

    add_index :users, :uid, :unique
  end

  def self.down
    drop_table :users
  end
end

class User < ActiveRecord::Base
  acts_as_fb_user

  def self.import(fbsession)
    user = self.find_or_initialize_by_uid(fbsession.session_user_id)

    # Assumes session_key never expires
    if fbsession.session_key != user.session_key
      user.session_key = fbsession.session_key
      user.save!
    end

    return user
  end
end


you can now do things with the user object such as get a user’s friends


>> u = User.find(1)
=> #<User:...>
>> u.friends
=> [1, 2, 3]

FBMLController

You can create FBMLControllers such as


class ApplicationController < Facebook::FBMLController
  before_filter :require_facebook_install
  before_filter :import_user

  private
  def import_user
    @user = User.import(fbsession)
  end
end

Although I still feel this doesnt need to be inherited and such just extend the ApplicationController.

API Calls.

Are now easier, no need to parse the Hpricot XML. And also you can use the fbsession in the Model objects (where it belongs)


class MyController < Facebook::FBMLController
  def friends
    @me         = Facebook::Users.get_info(fbsession.session_user_id, ['first_name', 'last_name'])
    @first_name = @me.first_name
    @last_name  = @me.last_name
    @friends    = Facebook::Friends.get
  end
end

Notifications like ActionMailer


class StampNotificationPublisher < Facebook::NotificationPublisher
  def stamp(friends)
    @to_ids = friends.map(&:uid)
    @text   = "just stamped on you" 
  end
end

I advise you check it out if you plan to write any applications for Facebook.

2 Responses to “Great Plugin for Facebook Apps”

  1. Bryan Says:

    Looks nice, but it’s way too buggy to be used in a production environment. Can’t even get it to start the server:

    uninitialized constant Facebook (NameError)

  2. Matt Says:

    You might also want to check out the RFacebook on Rails plugin:

    http://rfacebook.rubyforge.org

    It has essentially the same feature set and it uses the official RFacebook gem, rather than a frozen version like the other plugin. This way, the RFacebook on Rails plugin will always receive all bugfixes and feature updates. For example, the latest gem has a general solution for wrapping ALL Hpricot calls to avoid the XML parsing.

    The biggest difference is that you also get a nice debug panel (http://rfacebook.rubyforge.org/debugpanel.html), in addition to built-in support for using an SSH tunnel to develop from a local machine. All configuration is done from facebook.yml, and you don’t need to force your controllers to inherit from FBMLController.

    The RFacebook project has been pretty active recently, adding a couple new developers and putting out one or two releases per week. We’d definitely love to see even more Rails programmers contribute to the project.

Sorry, comments are closed for this article.