facebook-rails-starterkit 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source :rubygems
2
+
3
+ # Facebook
4
+ gem 'koala', '~> 1.3.0'
5
+ gem 'hashie', '~> 1.2.0'
6
+ gem 'joey', '~> 0.1.1', :git => 'git://github.com/kristianmandrup/Joey.git' # wrapper on top of koala
7
+
8
+ group :development do
9
+ gem "rspec", ">= 2.8.0"
10
+ gem "rdoc", ">= 3.12"
11
+ gem "bundler", ">= 1.1.0"
12
+ gem "jeweler", ">= 1.8.3"
13
+ gem "simplecov", ">= 0.5"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ GIT
2
+ remote: git://github.com/kristianmandrup/Joey.git
3
+ revision: 65196a58c2e2ac870b917f8e8b44c443b7417d5c
4
+ specs:
5
+ joey (0.1.2)
6
+ hashie
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ addressable (2.2.7)
12
+ diff-lcs (1.1.3)
13
+ faraday (0.7.6)
14
+ addressable (~> 2.2)
15
+ multipart-post (~> 1.1)
16
+ rack (~> 1.1)
17
+ git (1.2.5)
18
+ hashie (1.2.0)
19
+ jeweler (1.8.3)
20
+ bundler (~> 1.0)
21
+ git (>= 1.2.5)
22
+ rake
23
+ rdoc
24
+ json (1.6.6)
25
+ koala (1.3.0)
26
+ faraday (~> 0.7.0)
27
+ multi_json (~> 1.0)
28
+ multi_json (1.3.2)
29
+ multipart-post (1.1.5)
30
+ rack (1.4.1)
31
+ rake (0.9.2.2)
32
+ rdoc (3.12)
33
+ json (~> 1.4)
34
+ rspec (2.9.0)
35
+ rspec-core (~> 2.9.0)
36
+ rspec-expectations (~> 2.9.0)
37
+ rspec-mocks (~> 2.9.0)
38
+ rspec-core (2.9.0)
39
+ rspec-expectations (2.9.1)
40
+ diff-lcs (~> 1.1.3)
41
+ rspec-mocks (2.9.0)
42
+ simplecov (0.6.2)
43
+ multi_json (~> 1.3)
44
+ simplecov-html (~> 0.5.3)
45
+ simplecov-html (0.5.3)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (>= 1.1.0)
52
+ hashie (~> 1.2.0)
53
+ jeweler (>= 1.8.3)
54
+ joey (~> 0.1.1)!
55
+ koala (~> 1.3.0)
56
+ rdoc (>= 3.12)
57
+ rspec (>= 2.8.0)
58
+ simplecov (>= 0.5)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,268 @@
1
+ ## Facebook Rails starter kit
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).
4
+
5
+ In an initializer or similar "boot location" for your app facebook integration:
6
+
7
+ ```ruby
8
+ Facebook.app = MyCool::FacebookAppConfig.instance
9
+ ```
10
+
11
+ You can then define an app class that implements the following API.
12
+
13
+ Note: You may wan to load these values from a yaml file, and have the yaml filename as part of your `.gitignore`. You might want to look at 'facebooker2' gem for inspiration here (fx to support multiple environments). You have to have (or create) a Facebook App that is linked to your Rails application.
14
+
15
+ ```ruby
16
+ class MyCool
17
+ class FacebookAppConfig
18
+ include Singleton
19
+
20
+ # please change!
21
+ def id
22
+ '219868431409649'
23
+ end
24
+
25
+ # please change!
26
+ def secret
27
+ '7e5699f155df01d8e52b35c01dccd627'
28
+ end
29
+
30
+ # please change for staging/production
31
+ def url
32
+ 'http://localhost:3000'
33
+ end
34
+
35
+ # please customize (see Facebook permissions)
36
+ # https://developers.facebook.com/docs/authentication/permissions/
37
+ def default_permissions
38
+ ["publish_stream", "read_stream", "email"]
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ The `facebook.yml` file in `config/apis`:
45
+
46
+ ```yaml
47
+ id: 219868431409649
48
+ secret: 7e5699f155df01d8e52b35c01dccd627
49
+ url: http://localhost:3000
50
+ default_permissions: ["publish_stream", "read_stream", "email"]
51
+ ```
52
+
53
+ And here how to load it into the AppConfig singleton.
54
+
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
+ ```
85
+
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:
88
+
89
+ `Facebook.app.secret` # => value loaded from 'secret' entry in facebook.yml
90
+
91
+ ## Facebook access controllers
92
+
93
+ The module `Facebook::Access::Helper` can be included in controllers that require Facebook access (via signin).
94
+
95
+ ```ruby
96
+ class CampaignController < ApplicationController
97
+ include Facebook::Access::Helper
98
+
99
+ def signup
100
+ fb_login
101
+ end
102
+ end
103
+ ```
104
+
105
+ Some of the key methods made available are:
106
+
107
+ * fb_retrieve_access_token
108
+ * fb_login(permissions)
109
+ * fb_app
110
+ * fb_graph
111
+ * signed_request
112
+ * registration
113
+
114
+ You can now access the Facebook graph API for the current (session) user.
115
+
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.
117
+
118
+ ## Facebook Graph API
119
+
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.
122
+
123
+ * me
124
+
125
+ The following methods are all prefixed with 'my_'
126
+
127
+ * name
128
+ * first_name
129
+ * last_name
130
+ * username
131
+ * email
132
+ * picture
133
+ * friends
134
+ * messages
135
+
136
+ Examples: `my_name` and `my_first_name` and so on.
137
+
138
+ ```ruby
139
+ fb_graph.my_username
140
+ ```
141
+
142
+ Currently only the following post API is provided
143
+
144
+ * post_on_wall(message)
145
+
146
+ ## Facebook (and alternative OmniAuth) Authentication via Devise
147
+
148
+ This module is designed to help you quickly get started with Facebook - Devise integration and possibly other OAuth providers. Please see Railscasts [omniauth-part-1](http://railscasts.com/episodes/235-omniauth-part-1) and [omniauth-part-2](http://railscasts.com/episodes/236-omniauth-part-2)
149
+
150
+ In your ApplicationController or some other class/module included in your `AuthenticationsController` you
151
+ can override the `new_user` method if you need to.
152
+
153
+ ```ruby
154
+ # override as needed
155
+ def new_user
156
+ User.new
157
+ end
158
+ ```
159
+
160
+ Then simply include the `Facebook::Auth::Devise` module in your controller.
161
+
162
+ ```ruby
163
+ class AuthenticationsController < InheritedResources::Base
164
+ include Facebook::Auth::Devise
165
+
166
+ def new_user
167
+ Profile.new
168
+ end
169
+ end
170
+ ```
171
+
172
+ This module adds controller REST methods for:
173
+
174
+ * index
175
+ * create
176
+ * destroy
177
+
178
+ The following translation keys must be defined in a locale file (or similar i18n translation):
179
+
180
+ * facebook.auth.signed_in
181
+ * facebook.auth.success
182
+ * facebook.auth.destroyed
183
+
184
+ You can easily customize this module to suit your app by overriding any of the following methods:
185
+
186
+ * user_authentication_destroyed
187
+ * current_user_authentication
188
+ * authenticated
189
+ * authenticate_user
190
+ * authenticate_new_user
191
+ * authenticated_user_saved
192
+ * authenticated_user_not_saved
193
+ * authentication
194
+
195
+ ## Mode configuration
196
+
197
+ ```ruby
198
+ # in your User class
199
+ class Profile
200
+ has_many :authentications
201
+ ```
202
+
203
+ In your console:
204
+
205
+ ```
206
+ rails g model authentication uid:string provider:string user_id:integer
207
+ ```
208
+
209
+ Model:
210
+
211
+ ```ruby
212
+ class Authentication < ActiveRecord::Base
213
+ attr_accessible :create, :destroy, :index, :provider, :uid, :user_id
214
+
215
+ belongs_to :user
216
+ end
217
+ ```
218
+
219
+ Migration:
220
+
221
+ ```ruby
222
+ class CreateAuthentications < ActiveRecord::Migration
223
+ def change
224
+ create_table :authentications do |t|
225
+ t.integer :user_id
226
+ t.string :provider
227
+ t.string :uid # the unique userid of the User supplied by the provider
228
+ t.string :index
229
+ t.string :create
230
+ t.string :destroy
231
+
232
+ t.timestamps
233
+ end
234
+ end
235
+ end
236
+ ```
237
+
238
+ ```
239
+ rake db:migrate
240
+ ```
241
+
242
+
243
+ ## Facebook Route setup
244
+
245
+ In your `routes.rb file
246
+
247
+ ```ruby
248
+ match 'facebook/registration' => 'registrations#create', :as => :new_user_registration
249
+
250
+ # See http://railscasts.com/episodes/235-omniauth-part-1
251
+ match 'auth/:provider/callback' => 'authentications#create'
252
+ ```
253
+
254
+ ## Contributing to facebook-rails-starterkit
255
+
256
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
257
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
258
+ * Fork the project.
259
+ * Start a feature/bugfix branch.
260
+ * Commit and push until you are happy with your contribution.
261
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
262
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
263
+
264
+ ## Copyright
265
+
266
+ Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
267
+ further details.
268
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "facebook-rails-starterkit"
18
+ gem.homepage = "http://github.com/kristianmandrup/facebook-rails-starterkit"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Starterkit for using Facebook with Rails}
21
+ gem.description = %Q{Make it simple to get started using Facebook with Rails!}
22
+ gem.email = "kmandrup@gmail.com"
23
+ gem.authors = ["Kristian Mandrup"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "facebook-rails-starterkit #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,77 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "facebook-rails-starterkit"
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = "2012-04-23"
13
+ s.description = "Make it simple to get started using Facebook with Rails!"
14
+ s.email = "kmandrup@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "facebook-rails-starterkit.gemspec",
29
+ "lib/config_loader/yaml.rb",
30
+ "lib/facebook-rails-starterkit.rb",
31
+ "lib/facebook/access/helper.rb",
32
+ "lib/facebook/app.rb",
33
+ "lib/facebook/auth/devise.rb",
34
+ "lib/facebook/graph_api.rb",
35
+ "spec/facebook-rails-starterkit_spec.rb",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+ s.homepage = "http://github.com/kristianmandrup/facebook-rails-starterkit"
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = "1.8.22"
42
+ s.summary = "Starterkit for using Facebook with Rails"
43
+
44
+ if s.respond_to? :specification_version then
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<koala>, ["~> 1.3.0"])
49
+ s.add_runtime_dependency(%q<hashie>, ["~> 1.2.0"])
50
+ s.add_runtime_dependency(%q<joey>, ["~> 0.1.1"])
51
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
52
+ s.add_development_dependency(%q<rdoc>, [">= 3.12"])
53
+ s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
54
+ s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
55
+ s.add_development_dependency(%q<simplecov>, [">= 0.5"])
56
+ else
57
+ s.add_dependency(%q<koala>, ["~> 1.3.0"])
58
+ s.add_dependency(%q<hashie>, ["~> 1.2.0"])
59
+ s.add_dependency(%q<joey>, ["~> 0.1.1"])
60
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
61
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
62
+ s.add_dependency(%q<bundler>, [">= 1.1.0"])
63
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
64
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<koala>, ["~> 1.3.0"])
68
+ s.add_dependency(%q<hashie>, ["~> 1.2.0"])
69
+ s.add_dependency(%q<joey>, ["~> 0.1.1"])
70
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
71
+ s.add_dependency(%q<rdoc>, [">= 3.12"])
72
+ s.add_dependency(%q<bundler>, [">= 1.1.0"])
73
+ s.add_dependency(%q<jeweler>, [">= 1.8.3"])
74
+ s.add_dependency(%q<simplecov>, [">= 0.5"])
75
+ end
76
+ end
77
+
@@ -0,0 +1,33 @@
1
+ module ConfigLoader
2
+ class Yaml
3
+ def initialize path, file_name
4
+ @path = path
5
+
6
+ file_name += '.yml' unless file_name =~ /\.ya?ml/
7
+ @file_name = file_name
8
+
9
+ Hashie::Mash.new yaml
10
+ end
11
+
12
+ protected
13
+
14
+ def yaml
15
+ @yaml ||= YAML::load File.open(file_path)
16
+ end
17
+
18
+ def config_path
19
+ @config_path ||= File.join Rails.root, 'config'
20
+ end
21
+
22
+ def file_path
23
+ @file_path = File.join config_path, path, file_name
24
+ end
25
+ end
26
+
27
+ module Delegator
28
+ def method_missing(m, *args, &block)
29
+ raise 'A #config method must be defined for the LoaderDelegator to work' unless respond_to?(:config)
30
+ config.send(m)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ require 'config_loader/yaml'
2
+ require 'facebook/app'
3
+ require 'facebook/graph_api'
4
+ require 'facebook/access/helper'
5
+
6
+ require 'facebook/auth/devise' if defined?(Devise)
@@ -0,0 +1,64 @@
1
+ module Facebook
2
+ module Access
3
+ module Helper
4
+ def fb_retrieve_access_token
5
+ session[:access_token] = session[:oauth].get_access_token(params[:code]) if params[:code]
6
+ end
7
+
8
+ def fb_login permissions
9
+ session[:oauth] = Koala::Facebook::OAuth.new(fb_app.id, fb_app.secret, fb_app.url + '/home/callback')
10
+ @auth_url = session[:oauth].url_for_oauth_code(:permissions=> permissions || fb_app.default_permissions)
11
+ end
12
+
13
+ def auth_url
14
+ @auth_url
15
+ end
16
+
17
+ def fb_app
18
+ Facebook::App.instance
19
+ end
20
+
21
+ def fb_graph
22
+ Facebook::GraphApi.new session
23
+ end
24
+
25
+ def fb_my_id
26
+ fb_graph.my_id
27
+ end
28
+
29
+ # for FB Registration plugin
30
+ # see https://developers.facebook.com/docs/plugins/registration/
31
+ def signed_request
32
+ Hashie::Mash.new decoded_signed_request
33
+ end
34
+
35
+ def registration
36
+ signed_request.registration
37
+ end
38
+
39
+ protected
40
+
41
+ # http://acknowledgement.co.uk/post/Decoding-and-Accessing-The-signed_request-Parameter-in-Rails/247
42
+ def decoded_signed_request
43
+ decoder.decode params['signed_request']
44
+ end
45
+
46
+ def decoder
47
+ @decoder ||= Decoder.new
48
+ end
49
+
50
+ class Decoder
51
+ def base64_url_decode str
52
+ encoded_str = str.gsub('-','+').gsub('_','/')
53
+ encoded_str += '=' while !(encoded_str.size % 4).zero?
54
+ Base64.decode64(encoded_str)
55
+ end
56
+
57
+ def decode str
58
+ encoded_sig, payload = str.split('.')
59
+ data = ActiveSupport::JSON.decode base64_url_decode(payload)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,33 @@
1
+ require 'singleton'
2
+
3
+ module Facebook
4
+ def self.app
5
+ Facebook::App.instance
6
+ end
7
+
8
+ def self.app= an_app
9
+ app.fb_app = an_app
10
+ end
11
+
12
+ class App
13
+ include Singleton
14
+
15
+ attr_accessor :fb_app
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
32
+ end
33
+ end
@@ -0,0 +1,72 @@
1
+ require 'facebook/access/helper'
2
+
3
+ module Facebook
4
+ module Auth
5
+ include Facebook::Access::Helper
6
+
7
+ module Devise
8
+ def index
9
+ @authentications = current_user.authentications if current_user
10
+ end
11
+
12
+ # based on OmniAuth 2 railscast
13
+ def create
14
+ authentication ? authenticated : try_authenticate_user
15
+ end
16
+
17
+ def destroy
18
+ current_user_authentication.destroy
19
+ user_authentication_destroyed
20
+ end
21
+
22
+ protected
23
+
24
+ def user_authentication_destroyed
25
+ flash[:notice] = t "#{auth_provider}.auth.destroyed"
26
+ redirect_to authentications_url
27
+ end
28
+
29
+ def current_user_authentication
30
+ @authentication ||= current_user.authentications.find(params[:id])
31
+ end
32
+
33
+ def authenticated
34
+ flash[:notice] = t "#{auth_provider}.auth.signed_in"
35
+ sign_in_and_redirect(:user, authentication.user)
36
+ end
37
+
38
+ def try_authenticate_user
39
+ current_user ? authenticate_user : authenticate_new_user
40
+ end
41
+
42
+ def authenticate_user
43
+ current_user.authentications.create(:provider => auth_provider, :uid => fb_my_id)
44
+ flash[:notice] = t "#{auth_provider}.auth.success"
45
+ redirect_to authentications_url
46
+ end
47
+
48
+ def authenticate_new_user
49
+ new_user.authentications.build(:provider => auth_provider, :uid => fb_my_id)
50
+ user.save ? authenticated_user_saved : authenticated_user_not_saved
51
+ end
52
+
53
+ def authenticated_user_saved
54
+ flash[:notice] = t "#{auth_provider}.auth.signed_in"
55
+ sign_in_and_redirect(:user, user)
56
+ end
57
+
58
+ def authenticated_user_not_saved
59
+ redirect_to new_user_registration_url
60
+ end
61
+
62
+ def authentication
63
+ @authentication ||= Authentication.find_by_provider_and_uid(auth_provider, fb_my_id)
64
+ end
65
+
66
+ # override as needed
67
+ def new_user
68
+ User.new
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,44 @@
1
+ module Facebook
2
+ class GraphApi
3
+ attr_reader :api, :access_token
4
+
5
+ def initialize session
6
+ @access_token = session[:access_token]
7
+ @api = Koala::Facebook::GraphAndRestAPI.new(access_token)
8
+ end
9
+
10
+ # also see https://developers.facebook.com/docs/reference/fql/
11
+ # The Facebook Query Language for more efficient complex queries
12
+ # Also enables Multi-query
13
+
14
+ def me
15
+ @me ||= api.me
16
+ end
17
+
18
+ # my_id, my_email ...
19
+ [:id, :email, :name, :first_name, :last_name, :username].each do |attribute|
20
+ define_method "my_#{attribute}" do
21
+ instance_variable_set(:"@#{attribute}", me.send(attribute)) unless instance_variable_get(:"@#{attribute}")
22
+ end
23
+ end
24
+
25
+ def my_friends
26
+ # or fb.me.friends
27
+ me.friends
28
+ end
29
+
30
+ def my_picture
31
+ api.get_picture my_id
32
+ end
33
+
34
+ def my_messages
35
+ api.get_object "/me/statuses", "fields"=>"message"
36
+ end
37
+
38
+ ## Post
39
+
40
+ def post_on_wall message
41
+ api.put_wall_post message
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "FacebookRailsStarterkit" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'facebook-rails-starterkit'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: facebook-rails-starterkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: koala
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: hashie
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.2.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: joey
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.1.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.8.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.8.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rdoc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '3.12'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '3.12'
94
+ - !ruby/object:Gem::Dependency
95
+ name: bundler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.1.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.1.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: jeweler
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 1.8.3
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.8.3
126
+ - !ruby/object:Gem::Dependency
127
+ name: simplecov
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0.5'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0.5'
142
+ description: Make it simple to get started using Facebook with Rails!
143
+ email: kmandrup@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files:
147
+ - LICENSE.txt
148
+ - README.md
149
+ files:
150
+ - .document
151
+ - .rspec
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - VERSION
158
+ - facebook-rails-starterkit.gemspec
159
+ - lib/config_loader/yaml.rb
160
+ - lib/facebook-rails-starterkit.rb
161
+ - lib/facebook/access/helper.rb
162
+ - lib/facebook/app.rb
163
+ - lib/facebook/auth/devise.rb
164
+ - lib/facebook/graph_api.rb
165
+ - spec/facebook-rails-starterkit_spec.rb
166
+ - spec/spec_helper.rb
167
+ homepage: http://github.com/kristianmandrup/facebook-rails-starterkit
168
+ licenses:
169
+ - MIT
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ none: false
176
+ requirements:
177
+ - - ! '>='
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ segments:
181
+ - 0
182
+ hash: -1126046914443620564
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ none: false
185
+ requirements:
186
+ - - ! '>='
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubyforge_project:
191
+ rubygems_version: 1.8.22
192
+ signing_key:
193
+ specification_version: 3
194
+ summary: Starterkit for using Facebook with Rails
195
+ test_files: []