Add helper to load config
This commit is contained in:
parent
6a47e3c67a
commit
6f52778c38
@ -2,11 +2,35 @@
|
|||||||
require "mogli"
|
require "mogli"
|
||||||
|
|
||||||
module Facebooker2
|
module Facebooker2
|
||||||
|
class NotConfigured < Exception; end
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :api_key, :secret, :app_id
|
attr_accessor :api_key, :secret, :app_id
|
||||||
end
|
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)
|
def self.cast_to_facebook_id(object)
|
||||||
if object.kind_of?(Mogli::Profile)
|
if object.kind_of?(Mogli::Profile)
|
||||||
object.id
|
object.id
|
||||||
|
@ -4,6 +4,12 @@ module Facebooker2
|
|||||||
module Rails
|
module Rails
|
||||||
module Controller
|
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
|
def current_facebook_user
|
||||||
fetch_client_and_user_from_cookie
|
fetch_client_and_user_from_cookie
|
||||||
@_current_facebook_user
|
@_current_facebook_user
|
||||||
@ -19,17 +25,24 @@ module Facebooker2
|
|||||||
app_id = Facebooker2.app_id
|
app_id = Facebooker2.app_id
|
||||||
if (hash_data = fb_cookie_hash_for_app_id(app_id)) and
|
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)
|
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)
|
client = Mogli::Client.new(hash_data["access_token"],hash_data["expires"].to_i)
|
||||||
@_current_facebook_user = Mogli::User.new(:id=>hash_data["uid"])
|
user = Mogli::User.new(:id=>hash_data["uid"])
|
||||||
@_current_facebook_user.client = @_current_facebook_client
|
user.client = @_current_facebook_client
|
||||||
|
fb_sign_in_user_and_client(user,client)
|
||||||
end
|
end
|
||||||
@_fb_user_fetched = true
|
@_fb_user_fetched = true
|
||||||
end
|
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)
|
def fb_cookie_hash_for_app_id(app_id)
|
||||||
return nil unless fb_cookie_for_app_id?(app_id)
|
return nil unless fb_cookie_for_app_id?(app_id)
|
||||||
hash={}
|
hash={}
|
||||||
data = fb_cookie_for_app_id(app_id)
|
data = fb_cookie_for_app_id(app_id).gsub(/"/,"")
|
||||||
data.split("&").each do |str|
|
data.split("&").each do |str|
|
||||||
parts = str.split("=")
|
parts = str.split("=")
|
||||||
hash[parts.first] = parts.last
|
hash[parts.first] = parts.last
|
||||||
|
@ -6,7 +6,7 @@ module Facebooker2
|
|||||||
opts = Hash.new(true).merge!(options)
|
opts = Hash.new(true).merge!(options)
|
||||||
cookie = opts[:cookie]
|
cookie = opts[:cookie]
|
||||||
status = opts[:status]
|
status = opts[:status]
|
||||||
status = opts[:xfbml]
|
xfbml = opts[:xfbml]
|
||||||
js = <<-JAVASCRIPT
|
js = <<-JAVASCRIPT
|
||||||
<div id="fb-root"></div>
|
<div id="fb-root"></div>
|
||||||
<script>
|
<script>
|
||||||
|
3
spec/config/facebooker.yml
Normal file
3
spec/config/facebooker.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
spec:
|
||||||
|
app_id: 1234fromyaml
|
||||||
|
secret: fromyaml
|
@ -16,6 +16,35 @@ describe Facebooker2 do
|
|||||||
Facebooker2.app_id = "12345"
|
Facebooker2.app_id = "12345"
|
||||||
Facebooker2.app_id.should == "12345"
|
Facebooker2.app_id.should == "12345"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should allow setting the configuration in bulk" do
|
||||||
|
Facebooker2.configuration = {:app_id=>1234,:secret=>"secret"}
|
||||||
|
Facebooker2.app_id.should == 1234
|
||||||
|
Facebooker2.secret.should == "secret"
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an exception if you access a nil app_id" do
|
||||||
|
Facebooker2.app_id = nil
|
||||||
|
lambda do
|
||||||
|
Facebooker2.app_id
|
||||||
|
end.should raise_error(Facebooker2::NotConfigured)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can load the configuration via facebooker.yml" do
|
||||||
|
::Rails=mock("rails",:env=>"spec",:root=>File.dirname(__FILE__))
|
||||||
|
Facebooker2.load_facebooker_yaml
|
||||||
|
Facebooker2.app_id.should == "1234fromyaml"
|
||||||
|
Facebooker2.secret.should == "fromyaml"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "raises an error if there is no configuration for the env" do
|
||||||
|
::Rails=mock("rails",:env=>"invalid",:root=>File.dirname(__FILE__))
|
||||||
|
lambda do
|
||||||
|
Facebooker2.load_facebooker_yaml
|
||||||
|
end.should raise_error(Facebooker2::NotConfigured)
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "Casting to facebook_id" do
|
describe "Casting to facebook_id" do
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
class FakeController
|
class FakeController < ActionController::Base
|
||||||
include Facebooker2::Rails::Controller
|
include Facebooker2::Rails::Controller
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -9,12 +9,19 @@ describe Facebooker2::Rails::Controller do
|
|||||||
Facebooker2.app_id = "12345"
|
Facebooker2.app_id = "12345"
|
||||||
Facebooker2.secret = "42ca6de519d53f6e0420247a4d108d90"
|
Facebooker2.secret = "42ca6de519d53f6e0420247a4d108d90"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
let :controller do
|
||||||
|
controller = FakeController.new
|
||||||
|
controller.stub!(:cookies).and_return("fbs_12345"=>"\"access_token=114355055262088|57f0206b01ad48bf84ac86f1-12451752|63WyZjRQbzowpN8ibdIfrsg80OA.&expires=0&secret=1e3375dcc4527e7ead0f82c095421690&session_key=57f0206b01ad48bf84ac86f1-12451752&sig=4337fcdee4cc68bb70ec495c0eebf89c&uid=12451752\"")
|
||||||
|
controller
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
it "should include helpers" do
|
||||||
|
controller.master_helper_module.ancestors.should include(Facebooker2::Rails::Helpers)
|
||||||
|
end
|
||||||
|
|
||||||
describe "Cookie handling" do
|
describe "Cookie handling" do
|
||||||
let :controller do
|
|
||||||
controller = FakeController.new
|
|
||||||
controller.stub!(:cookies).and_return("fbs_12345"=>"access_token=114355055262088|57f0206b01ad48bf84ac86f1-12451752|63WyZjRQbzowpN8ibdIfrsg80OA.&expires=0&secret=1e3375dcc4527e7ead0f82c095421690&session_key=57f0206b01ad48bf84ac86f1-12451752&sig=4337fcdee4cc68bb70ec495c0eebf89c&uid=12451752")
|
|
||||||
controller
|
|
||||||
end
|
|
||||||
|
|
||||||
it "knows if a cookie exists for this app" do
|
it "knows if a cookie exists for this app" do
|
||||||
controller.fb_cookie_for_app_id?(12345).should be_true
|
controller.fb_cookie_for_app_id?(12345).should be_true
|
||||||
@ -75,12 +82,20 @@ describe Facebooker2::Rails::Controller do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "Methods" do
|
describe "Methods" do
|
||||||
it "has a current_facebook_user" do
|
|
||||||
|
|
||||||
|
it "allows you to sign in a user" do
|
||||||
|
controller.fb_sign_in_user_and_client(Mogli::User.new,Mogli::Client.new)
|
||||||
|
end
|
||||||
|
it "has a current_facebook_user" do
|
||||||
|
user = mock("user")
|
||||||
|
controller.fb_sign_in_user_and_client(user,Mogli::Client.new)
|
||||||
|
controller.current_facebook_user.should == user
|
||||||
end
|
end
|
||||||
|
|
||||||
it "has a current_facebook_client" do
|
it "has a current_facebook_client" do
|
||||||
|
client = mock("client")
|
||||||
|
controller.fb_sign_in_user_and_client(Mogli::User.new,client)
|
||||||
|
controller.current_facebook_client.should == client
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user