|
Project Information
Links
|
Facebook on Rails is a sexy plugin for developing Facebook appsThe author (Hoan) has discontinued support for this plugin. Sexy stuff:
You still have access to the `fbsession' variable just like RFacebook. InstallationEverything you need is in the plugin. ./script/plugin install -x http://facebook-rails.googlecode.com/svn/trunk/facebook ConfigurationConfiguration is really simple. Put this in your environment.rb: Facebook.api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' Facebook.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' Facebook.canvas_path = '/myappname' Facebook.callback_path = '/fb' FBMLControllerYour ApplicationController should inherit from Facebook::FBMLController. Change your application.rb to this: class ApplicationController < Facebook::FBMLController
before_filter :require_facebook_install
before_filter :import_user
private
def import_user
@user = User.import(fbsession)
end
endNow any routes you generate will be canvas pages (ie http://apps.facebook.com/myappname/path/to/foo). To generate normal urls, supply :canvas => false to your route (ie my_url(:canvas => false)). acts_as_fb_userYour user model should be similar to this: 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
endNow acts_as_fb_user assumes the model has two fields: uid and session_key. The session_key should be an unlimited session key for your app. Given this, we can: >> u = User.find(1) => #<User:...> >> u.name => "Hoan Ton-That" >> u.affiliations.map(&:name) => ["Australian National", "Australia"] >> u.friends => [1, 2, 3] >> u.get_fbml => "FBML goes here" >> u.music => "Bach, Barrios, Piazzolla" API Calls:The current Facebook session is kept implicit in a dynamic variable. So you can write this in your controller or model (provided that the controller has set fbsession): 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
endAll the API calls follow the same pattern of Facebook::Prefix.method_call. Unlike RFacebook, these calls return the result as ruby objects (Facebook::Friends.get returns an array of Integers) instead of XML. Backward compatibility:You can still call fbsession.friends_get and it will return Hpricot. Thanks to the RFacebook devs! |