fb_rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [Matthew Higgins]
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.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = FbRails
2
+
3
+ FbRails makes it easy to integrate your website with Facebook.
4
+
5
+
6
+ == Setting up
7
+
8
+ Add the following to your configuration. This can go in config/application.rb, or
9
+ customized in each environment in config/environments/[name].rb:
10
+
11
+ config.facebook = {
12
+ :app_id => 'my_app_id',
13
+ :secret => 'my_app_secret'
14
+ }
15
+
16
+ or
17
+
18
+ config.facebook.app_id = 'my_app_id'
19
+ config.facebook.secret = 'my_app_secret'
20
+
21
+
22
+ == API
23
+
24
+ FbRails adds an object called 'fb', which is available to all controllers and views:
25
+
26
+ Determine if the current user is connected through Facebook:
27
+
28
+ fb.connected?
29
+
30
+ Get the Facebook user id for the current user:
31
+
32
+ fb.uid
33
+
34
+ Make a request to the Facebook Graph:
35
+
36
+ <% result = fb.graph.get '/me' %>
37
+ my name is <%= result['first_name'] %>
38
+
39
+
40
+ == Persisting the Facebook User:
41
+
42
+ Websites that interact with Facebook usually have a user model in a local database.
43
+ This user is accessed with 'fb.user':
44
+
45
+ The current user is <%= fb.user.inspect %>
46
+
47
+ For this to work, FbRails assumes there is a model called 'User' with a column 'fb_uid'.
48
+ If your user model name is different than 'User', it can be configured:
49
+ config.facebook.user_class_name = 'Author'
50
+
51
+ If the fb_uid for the current user is not in the database, fb.user is an unsaved instance.
52
+ One thing I like to do for new Facebook users is store them in a before filter:
53
+
54
+ if fb.connected? && fb.user.new_record?
55
+ data = fb.graph.get('me')
56
+ fb.user.first_name = data['first_name']
57
+ fb.user.last_name = data['last_name']
58
+ fb.user.save
59
+ end
60
+
61
+
62
+ == Testing
63
+
64
+ Facebook requests can be mocked, so that your requests do not require real HTTP
65
+ connections. This is done with FbRails::Mock:
66
+
67
+ FbRails::Mock.respond_to do |mock|
68
+ mock.add 'me', 'first_name' => 'Matt', 'last_name' => 'Higgins'
69
+ end
70
+
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test fb_rails.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for fb_rails.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'FbRails'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,11 @@
1
+ module FbRails
2
+ class Config
3
+ class_attribute :app_id, :secret, :user_class_name
4
+
5
+ class << self
6
+ def user_class
7
+ (user_class_name || 'User').constantize
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ require 'digest/md5'
2
+
3
+ module FbRails
4
+ class Connect
5
+ class << self
6
+ def cookie_name
7
+ "fbs_#{FbRails::Config.app_id}"
8
+ end
9
+ end
10
+
11
+ extend ActiveSupport::Memoizable
12
+
13
+ attr_reader :cookies
14
+ def initialize(cookies)
15
+ @cookies = cookies
16
+ end
17
+
18
+ def cookie
19
+ return if (cookie_string = cookies[self.class.cookie_name]).blank?
20
+
21
+ hash = Rack::Utils::parse_query(cookie_string.gsub(/^\"|\"$/, ''))
22
+ sorted_pairs = hash.sort
23
+
24
+ payload = ''
25
+ sorted_pairs.each do |key, value|
26
+ payload += "#{key}=#{value}" if key != 'sig'
27
+ end
28
+
29
+ md5 = Digest::MD5.hexdigest("#{payload}#{FbRails::Config.secret}")
30
+ if md5 == hash['sig']
31
+ hash
32
+ end
33
+ end
34
+ memoize :cookie
35
+
36
+ def connected?
37
+ cookie.present?
38
+ end
39
+
40
+ def uid
41
+ connected? ? cookie['uid'].to_i : nil
42
+ end
43
+
44
+ def access_token
45
+ connected? ? cookie['access_token'] : nil
46
+ end
47
+
48
+ def user
49
+ if connected?
50
+ FbRails::Config.user_class.find_or_initialize_by_fb_uid(uid)
51
+ end
52
+ end
53
+ memoize :user
54
+ end
55
+ end
56
+
@@ -0,0 +1,20 @@
1
+ module FbRails
2
+ class Fb
3
+ delegate :app_id, :secret, :to => 'FbRails::Config'
4
+ delegate :connected?, :uid, :access_token, :user, :to => :connect
5
+
6
+ attr_reader :cookies
7
+ def initialize(cookies)
8
+ @cookies = cookies
9
+ end
10
+
11
+ def graph
12
+ @graph ||= FbRails::Graph.new(connect)
13
+ end
14
+
15
+ private
16
+ def connect
17
+ @connect ||= FbRails::Connect.new(cookies)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'uri'
4
+ require 'fb_rails/log_subscriber'
5
+
6
+ module FbRails
7
+ class Graph
8
+ class << self
9
+ def facebook_uri
10
+ @facebook_uri ||= URI.parse('https://graph.facebook.com')
11
+ end
12
+ end
13
+
14
+ attr_reader :connect
15
+ def initialize(connect)
16
+ @connect = connect
17
+ end
18
+
19
+ def post(url, params = {})
20
+ request(:post, url, params)
21
+ end
22
+
23
+ def get(url, params = {})
24
+ request(:get, url, params)
25
+ end
26
+
27
+ private
28
+ def request(http_verb, url, params = {})
29
+ path = build_path(url, params)
30
+
31
+ response = ActiveSupport::Notifications.instrument('request.fb_rails') do |payload|
32
+ payload[:http_verb] = http_verb
33
+ payload[:request_uri] = path
34
+ http.send(http_verb, path)
35
+ end
36
+
37
+ ActiveSupport::JSON.decode(response.body)
38
+ end
39
+
40
+ def build_path(url, params)
41
+ params = params.merge(:access_token => connect.access_token)
42
+ param_string = params.map { |key, value| "#{key}=#{CGI.escape(value)}" }.join('&')
43
+ url = "#{url}?#{param_string}"
44
+ end
45
+
46
+ def http
47
+ Net::HTTP.new(self.class.facebook_uri.host, self.class.facebook_uri.port).tap do |result|
48
+ result.use_ssl = true
49
+ result.verify_mode = OpenSSL::SSL::VERIFY_NONE
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,16 @@
1
+ module FbRails
2
+ module Integration
3
+ module ActionController
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ helper_method :fb
8
+ end
9
+
10
+ private
11
+ def fb
12
+ @fb ||= ::FbRails::Fb.new(cookies)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module FbRails
2
+ module Integration
3
+ module ActionView
4
+ def fbml(tag, options = nil)
5
+ content_tag("fb:#{tag}", '', options)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module FbRails
2
+ module Integration
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :ActionController
6
+ autoload :ActionView
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module FbRails
2
+ class LogSubscriber < ActiveSupport::LogSubscriber
3
+ def request(event)
4
+ name = color('FbRails (%.1fms)' % event.duration, CYAN, true)
5
+ info " #{name} #{event.payload[:http_verb].to_s.upcase} #{event.payload[:request_uri]}"
6
+ end
7
+
8
+ def logger
9
+ Rails.logger
10
+ end
11
+ end
12
+ end
13
+
14
+ FbRails::LogSubscriber.attach_to :fb_rails
@@ -0,0 +1,55 @@
1
+ module FbRails
2
+ class Mock
3
+ class Response
4
+ attr_reader :body
5
+ def initialize(data)
6
+ @body = data.to_json
7
+ end
8
+ end
9
+
10
+ class Responder
11
+ attr_reader :responses
12
+ def initialize(responses)
13
+ @responses = responses
14
+ end
15
+
16
+ def add(path, data)
17
+ responses[path] = Response.new(data)
18
+ end
19
+ end
20
+
21
+ class << self
22
+ def responses
23
+ @@responses ||= {}
24
+ end
25
+
26
+ def respond_to
27
+ yield Responder.new(responses)
28
+ end
29
+
30
+ def reset!
31
+ responses.clear
32
+ end
33
+
34
+ def find_response(path)
35
+ path = path.gsub(/\?.*$/, '')
36
+ responses[path]
37
+ end
38
+ end
39
+
40
+ %w(get post).each do |method|
41
+ module_eval <<-EOE, __FILE__, __LINE__ + 1
42
+ def #{method}(path)
43
+ self.class.find_response(path)
44
+ end
45
+ EOE
46
+ end
47
+ end
48
+
49
+ class Graph
50
+ private
51
+ def http
52
+ @http ||= Mock.new
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,23 @@
1
+ module FbRails
2
+ class Railtie < Rails::Railtie
3
+ config.facebook = ActiveSupport::OrderedOptions.new
4
+
5
+ initializer "fb_rails.set_configs" do |app|
6
+ ActiveSupport.on_load :action_controller do
7
+ app.config.facebook.each do |k,v|
8
+ ::FbRails::Config.send "#{k}=", v
9
+ end
10
+ end
11
+ end
12
+
13
+ initializer "fb_rails.integration" do
14
+ ActiveSupport.on_load :action_controller do
15
+ include ::FbRails::Integration::ActionController
16
+ end
17
+
18
+ ActiveSupport.on_load :action_view do
19
+ include ::FbRails::Integration::ActionView
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/fb_rails.rb ADDED
@@ -0,0 +1,19 @@
1
+ module FbRails
2
+ extend ActiveSupport::Autoload
3
+
4
+ autoload :Config
5
+ autoload :Connect
6
+ autoload :Graph
7
+ autoload :Fb
8
+ autoload :Mock
9
+ autoload :Integration
10
+
11
+ def self.cookie(uid = 42, access_token = 'abc')
12
+ param_string = "uid=#{uid}&access_token=#{access_token}"
13
+ payload = "access_token=#{access_token}uid=#{uid}"
14
+ sig = Digest::MD5.hexdigest("#{payload}#{FbRails::Config.secret}")
15
+ {FbRails::Connect.cookie_name => "\"#{param_string}&sig=#{sig}\""}
16
+ end
17
+ end
18
+
19
+ require 'fb_rails/railtie'
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class FbRails::ConfigTest < ActiveSupport::TestCase
4
+ test 'user_class' do
5
+ assert_equal User, FbRails::Config.user_class
6
+ end
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class FbRails::ConnectTest < ActiveSupport::TestCase
4
+ test 'empty cookie' do
5
+ fb_connect = FbRails::Connect.new({})
6
+
7
+ assert !fb_connect.connected?
8
+ assert_nil fb_connect.user
9
+ assert_nil fb_connect.uid
10
+ assert_nil fb_connect.access_token
11
+ end
12
+
13
+ test 'invalid signature' do
14
+ cookies = {FbRails::Connect.cookie_name => "\"uid=42&sig=invalid\""}
15
+ fb_connect = FbRails::Connect.new(cookies)
16
+
17
+ assert !fb_connect.connected?
18
+ assert_nil fb_connect.user
19
+ end
20
+
21
+ test 'valid cookie' do
22
+ fb_connect = FbRails::Connect.new(FbRails.cookie)
23
+
24
+ assert fb_connect.connected?
25
+ assert_equal 42, fb_connect.uid
26
+ assert_equal 'abc', fb_connect.access_token.to_s
27
+ assert_not_nil fb_connect.user
28
+ assert_equal 42, fb_connect.user.fb_uid
29
+ end
30
+ end
data/test/fb_test.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class FbRails::FbTest < ActiveSupport::TestCase
4
+ test 'configuration' do
5
+ fb = FbRails::Fb.new({})
6
+
7
+ assert_equal FbRails::Config.app_id, fb.app_id
8
+ assert_equal FbRails::Config.secret, fb.secret
9
+ end
10
+
11
+ test 'connect delegation' do
12
+ fb = FbRails::Fb.new(FbRails.cookie)
13
+
14
+ assert fb.respond_to?(:connected?)
15
+ assert fb.respond_to?(:uid)
16
+ assert fb.respond_to?(:access_token)
17
+ assert fb.respond_to?(:user)
18
+ end
19
+
20
+ test 'graph' do
21
+ fb = FbRails::Fb.new({})
22
+
23
+ assert_kind_of FbRails::Graph, fb.graph
24
+ assert_equal fb.graph.object_id, fb.graph.object_id
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class FbRails::GraphTest < ActiveSupport::TestCase
4
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+ #
3
+ class FbRails::ActionControllerTest < ActionController::TestCase
4
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+ require 'active_support/log_subscriber/test_helper'
3
+ require 'fb_rails/log_subscriber'
4
+
5
+ class FbRails::LogSubscriberTest < ActiveSupport::TestCase
6
+ include ActiveSupport::LogSubscriber::TestHelper
7
+
8
+ setup do
9
+ FbRails::Mock.respond_to do |mock|
10
+ mock.add 'test', 'foo' => 'bar'
11
+ end
12
+ end
13
+
14
+ def set_logger(logger)
15
+ # raise 'set logger'
16
+ Rails.logger = logger
17
+ end
18
+
19
+ test 'request notification' do
20
+ graph = FbRails::Graph.new(FbRails::Connect.new(FbRails.cookie))
21
+ graph.get 'test'
22
+
23
+ wait
24
+ assert_equal 1, @logger.logged(:info).size
25
+ end
26
+ end
data/test/mock_test.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class FbRails::MockTest < ActiveSupport::TestCase
4
+ setup do
5
+ FbRails::Mock.respond_to do |mock|
6
+ mock.add 'test', 'foo' => 'bar'
7
+ end
8
+ end
9
+
10
+ test 'mock' do
11
+ graph = FbRails::Graph.new(FbRails::Connect.new(FbRails.cookie))
12
+
13
+ result = graph.get 'test'
14
+
15
+ assert_equal({'foo' => 'bar'}, result)
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module Sample
2
+ class TestController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ class User
2
+ def self.find_or_initialize_by_fb_uid(fb_uid)
3
+ new(fb_uid)
4
+ end
5
+
6
+ attr_accessor :fb_uid
7
+ def initialize(fb_uid = 42)
8
+ @fb_uid = fb_uid
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rails/all'
4
+
5
+ require 'fb_rails'
6
+
7
+ require 'sample/user'
8
+ require 'sample/test_controller'
9
+
10
+ FbRails::Config.app_id = 'foo'
11
+ FbRails::Config.secret = 'bar'
12
+
13
+
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fb_rails
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Matthew Higgins
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-28 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ version: 3.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: A Rails 3 gem for the latest facebook API
36
+ email: developer@matthewhiggins.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - MIT-LICENSE
45
+ - Rakefile
46
+ - README.rdoc
47
+ - lib/fb_rails/config.rb
48
+ - lib/fb_rails/connect.rb
49
+ - lib/fb_rails/fb.rb
50
+ - lib/fb_rails/graph.rb
51
+ - lib/fb_rails/integration/action_controller.rb
52
+ - lib/fb_rails/integration/action_view.rb
53
+ - lib/fb_rails/integration.rb
54
+ - lib/fb_rails/log_subscriber.rb
55
+ - lib/fb_rails/mock.rb
56
+ - lib/fb_rails/railtie.rb
57
+ - lib/fb_rails.rb
58
+ - test/config_test.rb
59
+ - test/connect_test.rb
60
+ - test/fb_test.rb
61
+ - test/graph_test.rb
62
+ - test/helpers/action_controller_test.rb
63
+ - test/log_subscriber_test.rb
64
+ - test/mock_test.rb
65
+ - test/sample/test_controller.rb
66
+ - test/sample/user.rb
67
+ - test/test_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://gotime.com
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 1
84
+ - 8
85
+ - 7
86
+ version: 1.8.7
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 1
94
+ - 3
95
+ - 5
96
+ version: 1.3.5
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.7
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Facebook on Rails
104
+ test_files: []
105
+