facebooker2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require "digest/md5"
2
+ require "ruby-debug"
3
+ module Facebooker2
4
+ module Rails
5
+ module Controller
6
+
7
+ def self.included(controller)
8
+ controller.helper Facebooker2::Rails::Helpers
9
+ controller.helper_method :current_facebook_user
10
+ controller.helper_method :current_facebook_client
11
+ end
12
+
13
+ def current_facebook_user
14
+ fetch_client_and_user_from_cookie
15
+ @_current_facebook_user
16
+ end
17
+
18
+ def current_facebook_client
19
+ fetch_client_and_user_from_cookie
20
+ @_current_facebook_client
21
+ end
22
+
23
+ def fetch_client_and_user_from_cookie
24
+ return if @_fb_user_fetched
25
+ app_id = Facebooker2.app_id
26
+ if (hash_data = fb_cookie_hash_for_app_id(app_id)) and
27
+ fb_cookie_signature_correct?(fb_cookie_hash_for_app_id(app_id),Facebooker2.secret)
28
+ client = Mogli::Client.new(hash_data["access_token"],hash_data["expires"].to_i)
29
+ user = Mogli::User.new(:id=>hash_data["uid"])
30
+ user.client = @_current_facebook_client
31
+ fb_sign_in_user_and_client(user,client)
32
+ end
33
+ @_fb_user_fetched = true
34
+ end
35
+
36
+ def fb_sign_in_user_and_client(user,client)
37
+ @_current_facebook_user = user
38
+ @_current_facebook_client = client
39
+ @_fb_user_fetched = true
40
+ end
41
+
42
+ def fb_cookie_hash_for_app_id(app_id)
43
+ return nil unless fb_cookie_for_app_id?(app_id)
44
+ hash={}
45
+ data = fb_cookie_for_app_id(app_id).gsub(/"/,"")
46
+ data.split("&").each do |str|
47
+ parts = str.split("=")
48
+ hash[parts.first] = parts.last
49
+ end
50
+ hash
51
+ end
52
+
53
+ def fb_cookie_for_app_id?(app_id)
54
+ !fb_cookie_for_app_id(app_id).nil?
55
+ end
56
+
57
+ def fb_cookie_for_app_id(app_id)
58
+ cookies["fbs_#{app_id}"]
59
+ end
60
+
61
+ def fb_cookie_signature_correct?(hash,secret)
62
+ sorted_keys = hash.keys.reject {|k| k=="sig"}.sort
63
+ test_string = ""
64
+ sorted_keys.each do |key|
65
+ test_string += "#{key}=#{hash[key]}"
66
+ end
67
+ test_string += secret
68
+ Digest::MD5.hexdigest(test_string) == hash["sig"]
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,45 @@
1
+ module Facebooker2
2
+ module Rails
3
+ module Helpers
4
+ module FacebookConnect
5
+ #
6
+ # Render an <fb:login-button> element, similar to
7
+ # fb_login_button. Adds a js redirect to the onlogin event via rjs.
8
+ #
9
+ # ==== Examples
10
+ #
11
+ # fb_login_and_redirect '/other_page'
12
+ # => <fb:login-button onlogin="window.location.href = &quot;/other_page&quot;;"></fb:login-button>
13
+ #
14
+ # Like #fb_login_button, this also supports the :text option
15
+ #
16
+ # fb_login_and_redirect '/other_page', :text => "Login with Facebook", :v => '2'
17
+ # => <fb:login-button onlogin="window.location.href = &quot;/other_page&quot;;" v="2">Login with Facebook</fb:login-button>
18
+ #
19
+ def fb_login_and_redirect(url, options = {})
20
+ js = update_page do |page|
21
+ page.redirect_to url
22
+ end
23
+
24
+ text = options.delete(:text)
25
+
26
+ content_tag("fb:login-button",text,options.merge(:onlogin=>js))
27
+ end
28
+
29
+ #
30
+ # Logs the user out of facebook and redirects to the given URL
31
+ # args are passed to the call to link_to_function
32
+ def fb_logout_link(text,url,*args)
33
+ function= "FB.logout(function() {window.location.href = '#{url}';})"
34
+ link_to_function text, function, *args
35
+ end
36
+
37
+ def fb_server_fbml(style=nil,&proc)
38
+ style_string=" style=\"#{style}\"" if style
39
+ content = capture(&proc)
40
+ concat("<fb:serverFbml#{style_string}><script type='text/fbml'>#{content}</script></fb:serverFbml>")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ module Facebooker2
2
+ module Rails
3
+ module Helpers
4
+ module Javascript
5
+ def fb_connect_async_js(app_id,options={})
6
+ opts = Hash.new(true).merge!(options)
7
+ cookie = opts[:cookie]
8
+ status = opts[:status]
9
+ xfbml = opts[:xfbml]
10
+ js = <<-JAVASCRIPT
11
+ <div id="fb-root"></div>
12
+ <script>
13
+ window.fbAsyncInit = function() {
14
+ FB.init({
15
+ appId : '#{app_id}',
16
+ status : #{status}, // check login status
17
+ cookie : #{cookie}, // enable cookies to allow the server to access the session
18
+ xfbml : #{xfbml} // parse XFBML
19
+ });
20
+ };
21
+
22
+ (function() {
23
+ var e = document.createElement('script');
24
+ e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
25
+ e.async = true;
26
+ document.getElementById('fb-root').appendChild(e);
27
+ }());
28
+ </script>
29
+ JAVASCRIPT
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,70 @@
1
+ module Facebooker2
2
+ module Rails
3
+ module Helpers
4
+ module User
5
+ # Render an fb:name tag for the given user
6
+ # This renders the name of the user specified. You can use this tag as both subject and object of
7
+ # a sentence. <em> See </em> http://wiki.developers.facebook.com/index.php/Fb:name for full description.
8
+ # Use this tag on FBML pages instead of retrieving the user's info and rendering the name explicitly.
9
+ #
10
+ def fb_name(user, options={})
11
+ options = fb_transform_keys(options,FB_NAME_OPTION_KEYS_TO_TRANSFORM)
12
+ fb_assert_valid_keys(options, FB_NAME_VALID_OPTION_KEYS)
13
+ options.merge!(:uid => Facebooker2.cast_to_facebook_id(user))
14
+ content_tag("fb:name",nil, fb_stringify_vals(options))
15
+ end
16
+
17
+ FB_NAME_OPTION_KEYS_TO_TRANSFORM = {:first_name_only => :firstnameonly,
18
+ :last_name_only => :lastnameonly,
19
+ :show_network => :shownetwork,
20
+ :use_you => :useyou,
21
+ :if_cant_see => :ifcantsee,
22
+ :subject_id => :subjectid}
23
+ FB_NAME_VALID_OPTION_KEYS = [:firstnameonly, :linked, :lastnameonly, :possessive, :reflexive,
24
+ :shownetwork, :useyou, :ifcantsee, :capitalize, :subjectid]
25
+
26
+
27
+ def fb_profile_pic(user, options={})
28
+ options = options.dup
29
+ validate_fb_profile_pic_size(options)
30
+ options = fb_transform_keys(options,FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM)
31
+ fb_assert_valid_keys(options,FB_PROFILE_PIC_VALID_OPTION_KEYS)
32
+ options.merge!(:uid => Facebooker2.cast_to_facebook_id(user))
33
+ content_tag("fb:profile-pic", nil,fb_stringify_vals(options))
34
+ end
35
+
36
+ FB_PROFILE_PIC_OPTION_KEYS_TO_TRANSFORM = {:facebook_logo => 'facebook-logo'}
37
+ FB_PROFILE_PIC_VALID_OPTION_KEYS = [:size, :linked, 'facebook-logo', :width, :height]
38
+ VALID_FB_PROFILE_PIC_SIZES = [:thumb, :small, :normal, :square]
39
+ def validate_fb_profile_pic_size(options)
40
+ if options.has_key?(:size) && !VALID_FB_PROFILE_PIC_SIZES.include?(options[:size].to_sym)
41
+ raise(ArgumentError, "Unknown value for size: #{options[:size]}")
42
+ end
43
+ end
44
+
45
+ def fb_stringify_vals(hash)
46
+ result={}
47
+ hash.each do |key,value|
48
+ result[key]=value.to_s
49
+ end
50
+ result
51
+ end
52
+ def fb_transform_keys(options,transformation_hash)
53
+ new_hash = {}
54
+ options.each do |key,value|
55
+ new_key = transformation_hash[key]||key
56
+ new_hash[new_key]=value
57
+ end
58
+ new_hash
59
+ end
60
+ FB_ALWAYS_VALID_OPTION_KEYS = [:class, :style]
61
+
62
+ def fb_assert_valid_keys(options,*valid_keys)
63
+ unknown_keys = options.keys - [valid_keys + FB_ALWAYS_VALID_OPTION_KEYS].flatten
64
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
65
+ end
66
+
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ module Facebooker2
2
+ module Rails
3
+ module Helpers
4
+ include FacebookConnect
5
+ include Javascript
6
+ include User
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Facebooker2
2
+ module Rails
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ # Facebooker2
2
+ require "mogli"
3
+
4
+ module Facebooker2
5
+ class NotConfigured < Exception; end
6
+ class << self
7
+ attr_accessor :api_key, :secret, :app_id
8
+ end
9
+
10
+ def self.secret
11
+ @secret || raise_unconfigured_exception
12
+ end
13
+
14
+ def self.app_id
15
+ @app_id || raise_unconfigured_exception
16
+ end
17
+
18
+ def self.raise_unconfigured_exception
19
+ raise NotConfigured.new("No configuration provided for Facebooker2. Either set the app_id and secret or call Facebooker2.load_facebooker_yaml in an initializer")
20
+ end
21
+
22
+ def self.configuration=(hash)
23
+ self.api_key = hash[:api_key]
24
+ self.secret = hash[:secret]
25
+ self.app_id = hash[:app_id]
26
+ end
27
+
28
+ def self.load_facebooker_yaml
29
+ config = YAML.load(File.read(File.join(::Rails.root,"config","facebooker.yml")))[::Rails.env]
30
+ raise NotConfigured.new("Unable to load configuration for #{::Rails.env} from facebooker.yml. Is it set up?") if config.nil?
31
+ self.configuration = config.with_indifferent_access
32
+ end
33
+
34
+ def self.cast_to_facebook_id(object)
35
+ if object.kind_of?(Mogli::Profile)
36
+ object.id
37
+ elsif object.respond_to?(:facebook_id)
38
+ object.facebook_id
39
+ else
40
+ object
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+ require "facebooker2/rails/controller"
47
+ require "facebooker2/rails/helpers/facebook_connect"
48
+ require "facebooker2/rails/helpers/javascript"
49
+ require "facebooker2/rails/helpers/user"
50
+ require "facebooker2/rails/helpers"
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: facebooker2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Mangino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-05-05 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mogli
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.4
24
+ version:
25
+ description: Facebook Connect integration library for ruby and rails
26
+ email: mmangino@elevatedrails.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/facebooker2/rails/controller.rb
35
+ - lib/facebooker2/rails/helpers/facebook_connect.rb
36
+ - lib/facebooker2/rails/helpers/javascript.rb
37
+ - lib/facebooker2/rails/helpers/user.rb
38
+ - lib/facebooker2/rails/helpers.rb
39
+ - lib/facebooker2/rails.rb
40
+ - lib/facebooker2.rb
41
+ has_rdoc: true
42
+ homepage: http://developers.facebook.com/docs/api
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Facebook Connect integration library for ruby and rails
69
+ test_files: []
70
+