facebook-rails-starterkit 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,13 +3,13 @@ source :rubygems
3
3
  # Facebook
4
4
  gem 'koala', '>= 1.4.0'
5
5
  gem 'hashie', '~> 1.2.0'
6
- gem 'fb_joy', '~> 0.1.2' # Joey with some fixes
6
+ # gem 'fb_joy', '~> 0.1.2' # Joey with some fixes
7
+ gem 'rails_config_loader', '~> 0.1.1'
7
8
 
8
9
  group :development, :Test do
9
10
  gem "rspec", ">= 2.8.0"
10
11
  end
11
12
 
12
-
13
13
  group :development do
14
14
  gem "rdoc", ">= 3.12"
15
15
  gem "bundler", ">= 1.1.0"
@@ -7,8 +7,6 @@ GEM
7
7
  addressable (~> 2.2)
8
8
  multipart-post (~> 1.1)
9
9
  rack (~> 1.1)
10
- fb_joy (0.1.2)
11
- hashie
12
10
  git (1.2.5)
13
11
  hashie (1.2.0)
14
12
  jeweler (1.8.3)
@@ -17,12 +15,16 @@ GEM
17
15
  rake
18
16
  rdoc
19
17
  json (1.6.6)
18
+ json_pure (1.7.1)
20
19
  koala (1.4.1)
21
20
  faraday (~> 0.7.0)
22
21
  multi_json (~> 1.3.0)
23
22
  multi_json (1.3.2)
24
23
  multipart-post (1.1.5)
25
24
  rack (1.4.1)
25
+ rails_config_loader (0.1.1)
26
+ hashie
27
+ json_pure
26
28
  rake (0.9.2.2)
27
29
  rdoc (3.12)
28
30
  json (~> 1.4)
@@ -44,10 +46,10 @@ PLATFORMS
44
46
 
45
47
  DEPENDENCIES
46
48
  bundler (>= 1.1.0)
47
- fb_joy (~> 0.1.2)
48
49
  hashie (~> 1.2.0)
49
50
  jeweler (>= 1.8.3)
50
51
  koala (>= 1.4.0)
52
+ rails_config_loader (~> 0.1.1)
51
53
  rdoc (>= 3.12)
52
54
  rspec (>= 2.8.0)
53
55
  simplecov (>= 0.5)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Facebook Rails starter kit
2
2
 
3
- This gem leverages the gem: 'joey'. Joey in turn sits on top of 'koala' and uses the 'hashie' gem for easy hash access (as returned by the Facebook graph API via Koala).
3
+ This gem uses the 'hashie' gem for easy hash access (as returned by the Facebook graph API via Koala).
4
4
 
5
5
  In an initializer or similar "boot location" for your app facebook integration:
6
6
 
@@ -18,7 +18,7 @@ class MyCool
18
18
  include Singleton
19
19
 
20
20
  # please change!
21
- def id
21
+ def identifier
22
22
  '219868431409649'
23
23
  end
24
24
 
@@ -28,7 +28,7 @@ class MyCool
28
28
  end
29
29
 
30
30
  # please change for staging/production
31
- def url
31
+ def site_url
32
32
  'http://localhost:3000'
33
33
  end
34
34
 
@@ -44,47 +44,18 @@ end
44
44
  The `facebook.yml` file in `config/apis`:
45
45
 
46
46
  ```yaml
47
- id: 219868431409649
47
+ identifier: 219868431409649
48
48
  secret: 7e5699f155df01d8e52b35c01dccd627
49
- url: http://localhost:3000
49
+ site_url: http://localhost:3000
50
50
  default_permissions: ["publish_stream", "read_stream", "email"]
51
+ callback_path: '/home/callback'
51
52
  ```
52
53
 
53
- And here how to load it into the AppConfig singleton.
54
+ Note that the `site_url` is the url of your site hosting the external Facebook app integration, fx: `www.mycoolapp.com`.
54
55
 
55
- ```ruby
56
- class MyCool
57
- class FacebookAppConfig
58
- include Singleton
59
-
60
- def loader
61
- @loader ||= ConfigLoader::Yaml.new 'apis', 'facebook.yml'
62
- end
63
-
64
- def secret
65
- loader.load.secret
66
- end
67
- end
68
- end
69
- ```
70
-
71
- Or using the special Delegator module included. This module requires a #config method to be defined like so:
72
-
73
- ```ruby
74
- class MyCool
75
- class FacebookAppConfig
76
- include Singleton
77
- include ConfigLoader::Delegator
78
-
79
- def config
80
- @config ||= ConfigLoader::Yaml.new 'apis', 'facebook.yml'
81
- end
82
- end
83
- end
84
- ```
56
+ See the `config_loader` or 'config-file-loader' gem for a nice way to load these yaml config values into a global `App` object of some kind.
85
57
 
86
- Here a call to any method not defined on this app config, will be delegated to the loader, which returns the yaml as a Hashie for direct method access.
87
- This would allow you to:
58
+ Configuring this would allow you to do something like:
88
59
 
89
60
  `Facebook.app.secret` # => value loaded from 'secret' entry in facebook.yml
90
61
 
@@ -97,7 +68,7 @@ class CampaignController < ApplicationController
97
68
  include Facebook::Access::Helper
98
69
 
99
70
  def signup
100
- fb_login
71
+ fb_login!
101
72
  end
102
73
  end
103
74
  ```
@@ -111,17 +82,24 @@ Some of the key methods made available are:
111
82
  * signed_request
112
83
  * registration
113
84
 
114
- You can now access the Facebook graph API for the current (session) user.
85
+ You can access the Facebook graph API for the current (session) user.
86
+
87
+ For this to work, it requires a previous Facebook login which can be done fx via the Facebook login button (see fx 'facebook-social_plugins' gem). Alternatively use OAuth directly, fx by using the `fb_login!` method.
115
88
 
116
- For this to work, it requires a previous Facebook login which can be done fx via the Facebook login button (see fx 'facebook-social_plugins' gem). Alternatively use OAuth directly.
89
+ Note: You can use the `after_authenticate_new_user(user)` hook method to fx add the authenticated user to the session for the `current_user` or similar method.
90
+
91
+ ## Debugging and logging
92
+
93
+ You can set the `Facebook::Starterkit.logging_on!` in order to get some logging/debugging output while using the Starterkit. By default, logging is turned off. You can also turn it off using ``Facebook::Starterkit.logging_off!`
117
94
 
118
95
  ## Facebook Graph API
119
96
 
120
- The `fb_graph` method returns a class with some nice convenience methods. The
121
- graph api used is `Koala::Facebook::GraphAndRestAPI` from the `joey` gem, which uses Hashie (see `hashie gem) under the covers for easy access into the hashes returned.
97
+ The `fb_graph` method returns a class with some nice convenience methods. The graph api used is `Koala::Facebook::API` from the `koala` gem.
122
98
 
123
99
  * me
124
100
 
101
+ The me method call get_object('me') on the GraphAPI and converts the returned `Hash` into a `Hashie` for easy method access (using `method_missing`).
102
+
125
103
  The following methods are all prefixed with 'my_'
126
104
 
127
105
  * name
@@ -245,12 +223,26 @@ rake db:migrate
245
223
  In your `routes.rb file
246
224
 
247
225
  ```ruby
248
- match 'facebook/registration' => 'registrations#create', :as => :new_user_registration
226
+ match 'auth/:provider/registration' => 'registrations#create', :as => :registration
249
227
 
250
228
  # See http://railscasts.com/episodes/235-omniauth-part-1
251
229
  match 'auth/:provider/callback' => 'authentications#create'
252
230
  ```
253
231
 
232
+ The `:provider` param can be accessed in the controller via `params[:provider]`.
233
+ Note that the method `auth_provider` is already defined to return this value or default to `'facebook'`.
234
+
235
+ You can then use the route like this:
236
+
237
+ ```haml
238
+ = link_to "Register with Facebook", registration_path('facebook')
239
+ ```
240
+
241
+ Or perhaps like this:
242
+
243
+ ```haml
244
+ = button_to "", registration_path('facebook'), :class => "facebook_button"
245
+ ``
254
246
  ## Contributing to facebook-rails-starterkit
255
247
 
256
248
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -0,0 +1,6 @@
1
+ en:
2
+ facebook:
3
+ auth:
4
+ signed_in: Signed in with Facebook
5
+ success: Facebook login success
6
+ destroyed: Logged out from Facebook
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "facebook-rails-starterkit"
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
@@ -25,10 +25,11 @@ Gem::Specification.new do |s|
25
25
  "README.md",
26
26
  "Rakefile",
27
27
  "VERSION",
28
+ "config/locales/omniauth/en.yml",
28
29
  "facebook-rails-starterkit.gemspec",
29
- "lib/config_loader/yaml.rb",
30
30
  "lib/facebook-rails-starterkit.rb",
31
31
  "lib/facebook/access/helper.rb",
32
+ "lib/facebook/access/omniauth.rb",
32
33
  "lib/facebook/app.rb",
33
34
  "lib/facebook/auth/devise.rb",
34
35
  "lib/facebook/graph_api.rb",
@@ -50,7 +51,7 @@ Gem::Specification.new do |s|
50
51
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
52
  s.add_runtime_dependency(%q<koala>, [">= 1.4.0"])
52
53
  s.add_runtime_dependency(%q<hashie>, ["~> 1.2.0"])
53
- s.add_runtime_dependency(%q<fb_joy>, ["~> 0.1.2"])
54
+ s.add_runtime_dependency(%q<rails_config_loader>, ["~> 0.1.1"])
54
55
  s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
55
56
  s.add_development_dependency(%q<rdoc>, [">= 3.12"])
56
57
  s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
@@ -59,7 +60,7 @@ Gem::Specification.new do |s|
59
60
  else
60
61
  s.add_dependency(%q<koala>, [">= 1.4.0"])
61
62
  s.add_dependency(%q<hashie>, ["~> 1.2.0"])
62
- s.add_dependency(%q<fb_joy>, ["~> 0.1.2"])
63
+ s.add_dependency(%q<rails_config_loader>, ["~> 0.1.1"])
63
64
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
64
65
  s.add_dependency(%q<rdoc>, [">= 3.12"])
65
66
  s.add_dependency(%q<bundler>, [">= 1.1.0"])
@@ -69,7 +70,7 @@ Gem::Specification.new do |s|
69
70
  else
70
71
  s.add_dependency(%q<koala>, [">= 1.4.0"])
71
72
  s.add_dependency(%q<hashie>, ["~> 1.2.0"])
72
- s.add_dependency(%q<fb_joy>, ["~> 0.1.2"])
73
+ s.add_dependency(%q<rails_config_loader>, ["~> 0.1.1"])
73
74
  s.add_dependency(%q<rspec>, [">= 2.8.0"])
74
75
  s.add_dependency(%q<rdoc>, [">= 3.12"])
75
76
  s.add_dependency(%q<bundler>, [">= 1.1.0"])
@@ -1,6 +1,26 @@
1
- require 'config_loader/yaml'
2
1
  require 'facebook/app'
3
2
  require 'facebook/graph_api'
4
3
  require 'facebook/access/helper'
4
+ require 'facebook/access/omniauth'
5
5
 
6
- require 'facebook/auth/devise' if defined?(Devise)
6
+ require 'facebook/auth/devise' if defined?(Devise)
7
+
8
+ module Facebook
9
+ module Starterkit
10
+ class << self
11
+ attr_accessor :logging
12
+
13
+ def logging_on!
14
+ @logging = true
15
+ end
16
+
17
+ def logging_off!
18
+ @logging = false
19
+ end
20
+
21
+ def logging?
22
+ @logging
23
+ end
24
+ end
25
+ end
26
+ end
@@ -2,12 +2,38 @@ module Facebook
2
2
  module Access
3
3
  module Helper
4
4
  def fb_retrieve_access_token
5
- session[:access_token] = session[:oauth].get_access_token(params[:code]) if params[:code]
5
+ session[:access_token] = has_code? ? access_token_from_code : cookies_access_token
6
+ log! 'session-access_token', session[:access_token]
6
7
  end
7
8
 
8
- def fb_login permissions
9
- session[:oauth] = Koala::Facebook::OAuth.new(fb_app.id, fb_app.secret, fb_app.url + '/home/callback')
9
+ def access_token_from_code
10
+ session[:oauth].get_access_token(code)
11
+ end
12
+
13
+ def fb_login permissions = nil
14
+ session[:oauth] = oauth_api.new(fb_app.identifier, fb_app.secret, fb_app.site_url + fb_app.callback_path)
15
+ log! 'session-oath', session[:oauth]
10
16
  @auth_url = session[:oauth].url_for_oauth_code(:permissions=> permissions || fb_app.default_permissions)
17
+ log! 'auth_url', @auth_url
18
+ end
19
+
20
+ def fb_login! permissions = nil
21
+ fb_login permissions
22
+ redirect_to auth_url
23
+ end
24
+
25
+ def cookies_access_token
26
+ @cookies_access_token ||= session[:oauth].get_user_info_from_cookies(cookies)
27
+ end
28
+
29
+ def user_access_token
30
+ @user_access_token ||= user_cookie_info["access_token"]
31
+ log! 'user cookie access_token', @user_access_token
32
+ @user_access_token
33
+ end
34
+
35
+ def real_time_updates
36
+ @updates = Facebook.updates_api.new(:app_id => fb_app.identifier, :secret => fb_app.secret)
11
37
  end
12
38
 
13
39
  def auth_url
@@ -19,11 +45,17 @@ module Facebook
19
45
  end
20
46
 
21
47
  def fb_graph
22
- Facebook::GraphApi.new session
48
+ @fb_graph ||= graph_api.new session
49
+ log! 'fb_graph', @fb_graph
50
+ @fb_graph
23
51
  end
24
52
 
25
53
  def fb_my_id
26
- fb_graph.my_id
54
+ @fb_my_id ||= fb_graph.my_id
55
+ end
56
+
57
+ def auth_provider
58
+ params[:provider] || 'facebook'
27
59
  end
28
60
 
29
61
  # for FB Registration plugin
@@ -38,9 +70,41 @@ module Facebook
38
70
 
39
71
  protected
40
72
 
73
+ def has_code?
74
+ params[:code]
75
+ end
76
+
77
+ def code
78
+ params[:code]
79
+ end
80
+
81
+ def auth_sessions?
82
+ session[:access_token] && session[:oauth]
83
+ end
84
+
85
+ def log! title, msg
86
+ msg = msg.kind_of?(String) ? msg : msg.inspect
87
+ puts "#{title}: #{msg}" if auth_logging?
88
+ end
89
+
90
+ def auth_logging?
91
+ Facebook::Starterkit.logging?
92
+ end
93
+
94
+ def oauth_api
95
+ Koala::Facebook::OAuth
96
+ end
97
+
98
+ # Custom GraphAPI wrapper
99
+ def graph_api
100
+ Facebook::GraphApi
101
+ end
102
+
41
103
  # http://acknowledgement.co.uk/post/Decoding-and-Accessing-The-signed_request-Parameter-in-Rails/247
42
104
  def decoded_signed_request
43
- decoder.decode params['signed_request']
105
+ @decoded_signed_request ||= decoder.decode params['signed_request']
106
+ log! 'decoded_signed_request', @decoded_signed_request
107
+ @decoded_signed_request
44
108
  end
45
109
 
46
110
  def decoder
@@ -0,0 +1,17 @@
1
+ module Facebook
2
+ module Access
3
+ module Omniauth
4
+ def omniauth
5
+ request.env["omniauth.auth"]
6
+ end
7
+
8
+ def auth_provider
9
+ omniauth['provider']
10
+ end
11
+
12
+ def user_id
13
+ omniauth['uid']
14
+ end
15
+ end
16
+ end
17
+ end
@@ -14,20 +14,7 @@ module Facebook
14
14
 
15
15
  attr_accessor :fb_app
16
16
 
17
- def id
18
- fb_app.id
19
- end
20
-
21
- def secret_key
22
- fb_app.secret
23
- end
24
-
25
- def site_url
26
- fb_app.url
27
- end
28
-
29
- def fb_default_permissions
30
- fb_app.default_permissions
31
- end
17
+ delegate :identifier, :secret, :site_url, :default_permissions,
18
+ :callback_path, :to => :fb_app #, :allow_nil => true
32
19
  end
33
20
  end
@@ -2,16 +2,16 @@ require 'facebook/access/helper'
2
2
 
3
3
  module Facebook
4
4
  module Auth
5
- include Facebook::Access::Helper
6
-
7
5
  module Devise
6
+ include Facebook::Access::Helper
7
+
8
8
  def index
9
9
  @authentications = current_user.authentications if current_user
10
10
  end
11
11
 
12
12
  # based on OmniAuth 2 railscast
13
13
  def create
14
- authentication ? authenticated : try_authenticate_user
14
+ authenticated? ? authenticated : try_authenticate_user
15
15
  end
16
16
 
17
17
  def destroy
@@ -40,14 +40,20 @@ module Facebook
40
40
  end
41
41
 
42
42
  def authenticate_user
43
+ # fb_login!
43
44
  current_user.authentications.create(:provider => auth_provider, :uid => fb_my_id)
44
45
  flash[:notice] = t "#{auth_provider}.auth.success"
45
46
  redirect_to authentications_url
46
47
  end
47
48
 
48
49
  def authenticate_new_user
49
- new_user.authentications.build(:provider => auth_provider, :uid => fb_my_id)
50
+ fb_login!
51
+ user = new_user.authentications.build(:provider => auth_provider, :uid => fb_my_id)
50
52
  user.save ? authenticated_user_saved : authenticated_user_not_saved
53
+ after_authenticate_new_user(user)
54
+ end
55
+
56
+ def after_authenticate_new_user user
51
57
  end
52
58
 
53
59
  def authenticated_user_saved
@@ -59,6 +65,10 @@ module Facebook
59
65
  redirect_to new_user_registration_url
60
66
  end
61
67
 
68
+ def authenticated?
69
+ auth_sessions? && authentication
70
+ end
71
+
62
72
  def authentication
63
73
  @authentication ||= Authentication.find_by_provider_and_uid(auth_provider, fb_my_id)
64
74
  end
@@ -1,18 +1,35 @@
1
1
  module Facebook
2
+ def self.api
3
+ ::Koala::Facebook::API
4
+ end
5
+
6
+ def self.updates_api
7
+ ::Koala::Facebook::RealtimeUpdates
8
+ end
9
+
2
10
  class GraphApi
3
- attr_reader :api, :access_token
11
+ attr_reader :fb_api, :access_token
4
12
 
5
13
  def initialize session
6
14
  @access_token = session[:access_token]
7
- @api = Koala::Facebook::GraphAndRestAPI.new(access_token)
15
+ log! 'GraphApi access_token', @access_token
16
+ @fb_api = ::Facebook.api.new(access_token)
17
+ end
18
+
19
+ def api
20
+ @api ||= fb_api
8
21
  end
9
22
 
23
+ def self.clazz
24
+ ::Facebook.api
25
+ end
26
+
10
27
  # also see https://developers.facebook.com/docs/reference/fql/
11
28
  # The Facebook Query Language for more efficient complex queries
12
29
  # Also enables Multi-query
13
30
 
14
31
  def me
15
- @me ||= api.me
32
+ @me ||= ::Hashie::Mash.new api.get_object('me')
16
33
  end
17
34
 
18
35
  # my_id, my_email ...
@@ -39,6 +56,17 @@ module Facebook
39
56
 
40
57
  def post_on_wall message
41
58
  api.put_wall_post message
42
- end
59
+ end
60
+
61
+ protected
62
+
63
+ def log! title, msg
64
+ msg = msg.kind_of?(String) ? msg : msg.inspect
65
+ puts "#{title}: #{msg}" if logging?
66
+ end
67
+
68
+ def logging?
69
+ Facebook::Starterkit.logging?
70
+ end
43
71
  end
44
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook-rails-starterkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,13 +44,13 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: 1.2.0
46
46
  - !ruby/object:Gem::Dependency
47
- name: fb_joy
47
+ name: rails_config_loader
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 0.1.2
53
+ version: 0.1.1
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.2
61
+ version: 0.1.1
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rspec
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -155,10 +155,11 @@ files:
155
155
  - README.md
156
156
  - Rakefile
157
157
  - VERSION
158
+ - config/locales/omniauth/en.yml
158
159
  - facebook-rails-starterkit.gemspec
159
- - lib/config_loader/yaml.rb
160
160
  - lib/facebook-rails-starterkit.rb
161
161
  - lib/facebook/access/helper.rb
162
+ - lib/facebook/access/omniauth.rb
162
163
  - lib/facebook/app.rb
163
164
  - lib/facebook/auth/devise.rb
164
165
  - lib/facebook/graph_api.rb
@@ -182,7 +183,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
182
183
  version: '0'
183
184
  segments:
184
185
  - 0
185
- hash: 1771464293568223461
186
+ hash: -3307906802591948014
186
187
  required_rubygems_version: !ruby/object:Gem::Requirement
187
188
  none: false
188
189
  requirements:
@@ -1,43 +0,0 @@
1
- require 'hashie'
2
-
3
- module ConfigLoader
4
- class Yaml
5
- attr_reader :path, :file_name, :ext, :file_path, :root
6
-
7
- # will try root element if such exists
8
- def initialize file_path, root = nil
9
- @file_path = file_path
10
- @path = File.dirname file_path
11
- @file_name = File.basename file_path
12
- @ext = file_name.split(/(ya?ml$)/).last
13
- @root = (root || file_name.split('.').first).to_s
14
- @root = nil unless mashie.send(@root)
15
- @mashie = mashie.send(@root) if @root
16
- end
17
-
18
- def as_hash
19
- @as_hash ||= mashie
20
- end
21
-
22
- def as_yaml
23
- @as_yaml ||= ::YAML::load File.open(config_file_path)
24
- end
25
-
26
- protected
27
-
28
- def mashie
29
- @mashie ||= ::Hashie::Mash.new as_yaml
30
- end
31
-
32
- def config_file_path
33
- @config_file_path ||= File.join(Rails.root.to_s, 'config', file_path)
34
- end
35
- end
36
-
37
- module Delegator
38
- def method_missing(m, *args, &block)
39
- raise "A #config method must be defined in the container for ConfigLoader::Delegator, for it to delegate to #config: delegation attempted with #{m}" unless self.respond_to?(:config)
40
- config.send(m)
41
- end
42
- end
43
- end