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.

Continuing Facebook Applications with Ruby On Rails

July 16th, 2007

This is the continuing tutorial from the last post Tutorial on developing a Facebook platform application with Ruby On Rails where we can take the social recipe application and add some more Facebook features including posting to the feed, profile boxes, profile actions, the Facebook Query Language and sending out invitations to a user’s friends to come join in the cooking fun (oh boy). So lets get down to it, I’m presuming you have been through the first tutorial and have a working Facebook application. Even if you havn’t got it deployed today’s examples will work with an IFRAME application also.

Read the rest of this entry