diff --git a/lib/facebooker2.rb b/lib/facebooker2.rb index 773ca0e..fae546e 100644 --- a/lib/facebooker2.rb +++ b/lib/facebooker2.rb @@ -2,10 +2,34 @@ require "mogli" module Facebooker2 - + class NotConfigured < Exception; end class << self attr_accessor :api_key, :secret, :app_id end + + def self.secret + @secret || raise_unconfigured_exception + end + + def self.app_id + @app_id || raise_unconfigured_exception + end + + def self.raise_unconfigured_exception + raise NotConfigured.new("No configuration provided for Facebooker2. Either set the app_id and secret or call Facebooker2.load_facebooker_yaml in an initializer") + end + + def self.configuration=(hash) + self.api_key = hash[:api_key] + self.secret = hash[:secret] + self.app_id = hash[:app_id] + end + + def self.load_facebooker_yaml + config = YAML.load(File.read(File.join(::Rails.root,"config","facebooker.yml")))[::Rails.env] + raise NotConfigured.new("Unable to load configuration for #{::Rails.env} from facebooker.yml. Is it set up?") if config.nil? + self.configuration = config.with_indifferent_access + end def self.cast_to_facebook_id(object) if object.kind_of?(Mogli::Profile) diff --git a/lib/facebooker2/rails/controller.rb b/lib/facebooker2/rails/controller.rb index 5834a56..c1e3755 100644 --- a/lib/facebooker2/rails/controller.rb +++ b/lib/facebooker2/rails/controller.rb @@ -4,6 +4,12 @@ module Facebooker2 module Rails module Controller + def self.included(controller) + controller.helper Facebooker2::Rails::Helpers + controller.helper_method :current_facebook_user + controller.helper_method :current_facebook_client + end + def current_facebook_user fetch_client_and_user_from_cookie @_current_facebook_user @@ -19,17 +25,24 @@ module Facebooker2 app_id = Facebooker2.app_id if (hash_data = fb_cookie_hash_for_app_id(app_id)) and fb_cookie_signature_correct?(fb_cookie_hash_for_app_id(app_id),Facebooker2.secret) - @_current_facebook_client = Mogli::Client.new(hash_data["access_token"],hash_data["expires"].to_i) - @_current_facebook_user = Mogli::User.new(:id=>hash_data["uid"]) - @_current_facebook_user.client = @_current_facebook_client + client = Mogli::Client.new(hash_data["access_token"],hash_data["expires"].to_i) + user = Mogli::User.new(:id=>hash_data["uid"]) + user.client = @_current_facebook_client + fb_sign_in_user_and_client(user,client) end @_fb_user_fetched = true end + def fb_sign_in_user_and_client(user,client) + @_current_facebook_user = user + @_current_facebook_client = client + @_fb_user_fetched = true + end + def fb_cookie_hash_for_app_id(app_id) return nil unless fb_cookie_for_app_id?(app_id) hash={} - data = fb_cookie_for_app_id(app_id) + data = fb_cookie_for_app_id(app_id).gsub(/"/,"") data.split("&").each do |str| parts = str.split("=") hash[parts.first] = parts.last diff --git a/lib/facebooker2/rails/helpers/javascript.rb b/lib/facebooker2/rails/helpers/javascript.rb index 0271a9a..6abf478 100644 --- a/lib/facebooker2/rails/helpers/javascript.rb +++ b/lib/facebooker2/rails/helpers/javascript.rb @@ -6,7 +6,7 @@ module Facebooker2 opts = Hash.new(true).merge!(options) cookie = opts[:cookie] status = opts[:status] - status = opts[:xfbml] + xfbml = opts[:xfbml] js = <<-JAVASCRIPT