fb_rails 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,8 +18,7 @@ or
18
18
  config.facebook.app_id = 'my_app_id'
19
19
  config.facebook.secret = 'my_app_secret'
20
20
 
21
- Getting the user logged in is out of the scope of this gem. That's what
22
- FBML and the Facebook Javascript API are for. Here's an example:
21
+ Include facebook Javascript and a login button:
23
22
 
24
23
  <%= include_facebook_javascript %>
25
24
  <%= fbml 'login-button' %>
@@ -82,6 +81,22 @@ One thing I like to do for new Facebook users is store them in a before filter:
82
81
  end
83
82
 
84
83
 
84
+ == Timeouts
85
+
86
+ Calls to the Graph API can timeout. If this occurs, an FbRails::TimeoutError occurs.
87
+ Your application can rescue from these:
88
+
89
+ begin
90
+ fb.get 'me'
91
+ rescue FbRails::TimeoutError => e
92
+ ...
93
+ end
94
+
95
+ The timeout defaults to 60 seconds. This can be configured:
96
+
97
+ config.timeout = 10
98
+
99
+
85
100
  == Testing
86
101
 
87
102
  Facebook requests can be mocked, so that your tests do not require real HTTP
@@ -1,6 +1,7 @@
1
1
  module FbRails
2
2
  class Config
3
- class_attribute :app_id, :secret, :user_class_name
3
+ class_attribute :app_id, :secret, :user_class_name, :timeout
4
+ self.timeout = 60
4
5
 
5
6
  class << self
6
7
  def user_class
@@ -4,6 +4,13 @@ require 'uri'
4
4
  require 'fb_rails/log_subscriber'
5
5
 
6
6
  module FbRails
7
+ class TimeoutError < StandardError
8
+ def initialize(message)
9
+ @message = message
10
+ end
11
+ def to_s; @message ;end
12
+ end
13
+
7
14
  class Graph
8
15
  class << self
9
16
  def facebook_uri
@@ -11,27 +18,33 @@ module FbRails
11
18
  end
12
19
 
13
20
  def get(path, params = {})
14
- request(:get, "#{path}?#{params.to_param}")
21
+ json_request(:get, "#{path}?#{params.to_param}")
15
22
  end
16
23
 
17
24
  def post(path, params = {})
18
- request(:post, path, params.to_param)
25
+ json_request(:post, path, params.to_param)
19
26
  end
20
27
 
21
28
  private
22
29
  def request(http_verb, path, *arguments)
23
- response = ActiveSupport::Notifications.instrument('request.fb_rails') do |payload|
30
+ ActiveSupport::Notifications.instrument('request.fb_rails') do |payload|
24
31
  payload[:http_verb] = http_verb
25
32
  payload[:request_uri] = path
26
33
  http.send(http_verb, path, *arguments)
27
34
  end
35
+ rescue Timeout::Error => e
36
+ raise TimeoutError.new(e.message)
37
+ end
28
38
 
39
+ def json_request(http_verb, path, *arguments)
40
+ response = request(http_verb, path, *arguments)
29
41
  ActiveSupport::JSON.decode(response.body)
30
42
  end
31
43
 
32
44
  def http
33
45
  result = Net::HTTP.new(facebook_uri.host, facebook_uri.port)
34
46
  configure_https(result)
47
+ result.timeout = FbRails::Config.timeout
35
48
  result
36
49
  end
37
50
 
@@ -1,5 +1,15 @@
1
1
  module FbRails
2
2
  module TestHelper
3
+ # Creates a hash representation of the facebook cookie.
4
+ # To use this in tests, you might do the following:
5
+ #
6
+ # class MyControllerTest < ActionController::TestCase
7
+ # test 'request that requires authentication' do
8
+ # @request.cookies.merge! fb_cookie
9
+ # get :protected_action
10
+ # end
11
+ # end
12
+ #
3
13
  def fb_cookie(uid = 42, access_token = 'abc')
4
14
  value = FbRails::Cookie.encode({uid: uid, access_token: access_token}, FbRails::Config.secret)
5
15
  {FbRails::Connect.cookie_name => value}
@@ -5,4 +5,8 @@ class FbRails::ConfigTest < ActiveSupport::TestCase
5
5
  assert_equal User, FbRails::Config.user_class
6
6
  assert User.new.respond_to?(:access_token)
7
7
  end
8
+
9
+ test 'default timeout' do
10
+ assert_equal 60, FbRails::Config.timeout
11
+ end
8
12
  end
@@ -4,4 +4,14 @@ class FbRails::GraphTest < ActiveSupport::TestCase
4
4
  test 'facebook_uri' do
5
5
  assert_equal 'https', FbRails::Graph.facebook_uri.scheme
6
6
  end
7
+
8
+ test 'rescue timout error' do
9
+ http = mock
10
+ http.stubs(:get).raises(Timeout::Error)
11
+ FbRails::Graph.expects(:http).returns(http)
12
+
13
+ assert_raise FbRails::TimeoutError do
14
+ FbRails::Graph.get 'foo'
15
+ end
16
+ end
7
17
  end
@@ -14,7 +14,7 @@ class FbRails::LogSubscriberTest < ActiveSupport::TestCase
14
14
 
15
15
  FbRails::LogSubscriber.attach_to :fb_rails
16
16
  end
17
-
17
+
18
18
  test 'request notification' do
19
19
  graph = FbRails::Graph.new('my_access_token')
20
20
  graph.get 'test'
@@ -1,8 +1,10 @@
1
1
  require 'rubygems'
2
+ require 'bundler/setup'
3
+
2
4
  require 'test/unit'
3
5
  require 'rails/all'
4
-
5
6
  require 'fb_rails'
7
+ require 'mocha'
6
8
 
7
9
  require 'sample/user'
8
10
  require 'sample/test_controller'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fb_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-20 00:00:00.000000000 -07:00
13
- default_executable:
12
+ date: 2011-09-19 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: rails
17
- requirement: &2153045200 !ruby/object:Gem::Requirement
16
+ requirement: &70219700445320 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,7 +21,7 @@ dependencies:
22
21
  version: 3.0.0
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *2153045200
24
+ version_requirements: *70219700445320
26
25
  description: A Rails 3 gem for the latest facebook API
27
26
  email: developer@matthewhiggins.com
28
27
  executables: []
@@ -58,7 +57,6 @@ files:
58
57
  - test/sample/test_controller.rb
59
58
  - test/sample/user.rb
60
59
  - test/test_helper.rb
61
- has_rdoc: true
62
60
  homepage: http://github.com/matthuhiggins/fb_rails
63
61
  licenses: []
64
62
  post_install_message:
@@ -79,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
77
  version: 1.3.5
80
78
  requirements: []
81
79
  rubyforge_project:
82
- rubygems_version: 1.6.2
80
+ rubygems_version: 1.8.10
83
81
  signing_key:
84
82
  specification_version: 3
85
83
  summary: Facebook on Rails