Wednesday, July 30, 2008

Rails on Facebook Book is out!


Time to announce the PeepCode book Rails on Facebook. It is a no-nonsense, full of code , comprehensive PDF book on developing Facebook applications with the Rails Plugin Facebooker.

Shane and I have been working on it over the last few months and made sure that it covers a bunch of the latest design and platform changes.

Friday, July 11, 2008

Add Custom Conditions to your Rails Routes

At MeetingWave we support both a Social Network Application and the main Web Application. On occasion we need to know which context we are in in order to direct the request to the correct controller/action pair. What we want is to be able to have a route that looks like this:

map.facebook_my_profile 'myprofile', :controller => 'member_profiles', :action => 'import_facebook_profile', :conditions => {:context => "social_network" }



Currently, at least as of Rails 2.0 , the only condition that you can use is the request type ( e.g. :conditions => {:method => "get"}) . Maybe things are different in 2.1, we haven't made the plunge yet.

Anyway here is some code, that I coerced from the Facebooker source, that will enable the route above to work:

module TTB
module RouteSetExtensions
def self.included(base)
base.alias_method_chain :extract_request_environment, :context
end

def extract_request_environment_with_context(request)
env = extract_request_environment_without_context(request)
env.merge :context => request.parameters[:context]
end
end
end

class ActionController::Routing::Route
def recognition_conditions_with_context
defaults = recognition_conditions_without_context
defaults << " env[:context] == conditions[:context] " if conditions[:context]
defaults
end
alias_method_chain :recognition_conditions, :context
end

ActionController::Routing::RouteSet.send :include, TTB::RouteSetExtensions




Someday I will get my Textmate code highlighting to work in here, sorry about that.

Tuesday, July 8, 2008

Facebooker with Bebo Support

I think I promised this quite a few months ago and now with the wonders of GitHub I have completed it.

I forked the main repo from mike and added support not only for Bebo but for running multiple Facebook apps within one Rails instance. The fork is hopefully only temporary as I think Mike will push this into the main repo once it is confiremed that it doesn't break backward compatibility. Anyone out there running on Bebo and want to kick the tires for me? If so you can grab it from :

http://github.com/digidigo/facebooker/tree/master

Basically how it works is that you can add any number of config options to your facebooker.yml file now mine looks like this:

development:
api_key: b9f5233d4b7f8216253c0d27f60603ed
secret_key: XXXXXXX
canvas_page_name: mwavedev
callback_url: http://staging.travelerstable.com:8888
bebo_api_key: pZwnITIadEri1V0fNPxEMhSen9VBCoRf5jV9
bebo_secret_key: XXXXXXX
bebo_canvas_page_name: ttdev
bebo_adapter: BeboAdapter
foo_api_key: f688cbe01c465d71e4f9bed26722309a
foo_secret_key: XXXXXX
foo_canvas_page_name: sumobaby
foo_adapter: FacebookAdapter
tunnel:
public_host_username:
public_host:
public_port: 4007
local_port: 3000



Currenlty you need to set all 4 parameters , api_key, secret_key , canvas_page_name and adapter for any additional contexts. The way the code works is that there is a before filter added to the controller that looks at fb_sig_api_key and loads an adapter instance specified by the "#{key_base}_adapter" setting for that api_key.

All of the API specific constants have been pulled out of the code base and are available via method on the Facebooker module.

For example you call Facebooker.canvas_page_name and you will get the current adapters canvas_page.

If you need to load an adapter specifically you call Facebooker.load_adapter(:config_key_base => "foo")

All the tests are passing so I think the backward compatibility is fine. And there are new tests for all the adapter specific stuff.