rest-core 0.2.3 → 0.3.0.pre.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.
Files changed (79) hide show
  1. data/.travis.yml +8 -4
  2. data/CHANGES.md +20 -0
  3. data/Gemfile +4 -3
  4. data/README.md +8 -8
  5. data/Rakefile +42 -2
  6. data/example/rails2/Gemfile +13 -0
  7. data/example/rails2/README +4 -0
  8. data/example/rails2/Rakefile +11 -0
  9. data/example/rails2/app/controllers/application_controller.rb +119 -0
  10. data/example/rails2/app/views/application/helper.html.erb +1 -0
  11. data/example/rails2/config/boot.rb +130 -0
  12. data/example/rails2/config/environment.rb +15 -0
  13. data/example/rails2/config/environments/development.rb +17 -0
  14. data/example/rails2/config/environments/production.rb +28 -0
  15. data/example/rails2/config/environments/test.rb +30 -0
  16. data/example/rails2/config/initializers/cookie_verification_secret.rb +7 -0
  17. data/example/rails2/config/initializers/new_rails_defaults.rb +21 -0
  18. data/example/rails2/config/initializers/session_store.rb +15 -0
  19. data/example/rails2/config/preinitializer.rb +23 -0
  20. data/example/rails2/config/rest-core.yaml +12 -0
  21. data/example/rails2/config/routes.rb +43 -0
  22. data/example/rails2/log +0 -0
  23. data/example/rails2/test/functional/application_controller_test.rb +183 -0
  24. data/example/rails2/test/test_helper.rb +18 -0
  25. data/example/rails2/test/unit/rails_util_test.rb +44 -0
  26. data/example/rails3/Gemfile +13 -0
  27. data/example/rails3/README +4 -0
  28. data/example/rails3/Rakefile +7 -0
  29. data/example/rails3/app/controllers/application_controller.rb +119 -0
  30. data/example/rails3/app/views/application/helper.html.erb +1 -0
  31. data/example/rails3/config/application.rb +23 -0
  32. data/example/rails3/config/environment.rb +5 -0
  33. data/example/rails3/config/environments/development.rb +26 -0
  34. data/example/rails3/config/environments/production.rb +49 -0
  35. data/example/rails3/config/environments/test.rb +30 -0
  36. data/example/rails3/config/initializers/secret_token.rb +7 -0
  37. data/example/rails3/config/initializers/session_store.rb +8 -0
  38. data/example/rails3/config/rest-core.yaml +12 -0
  39. data/example/rails3/config/routes.rb +5 -0
  40. data/example/rails3/config.ru +4 -0
  41. data/example/rails3/test/functional/application_controller_test.rb +183 -0
  42. data/example/rails3/test/test_helper.rb +18 -0
  43. data/example/rails3/test/unit/rails_util_test.rb +44 -0
  44. data/example/sinatra/config.ru +16 -0
  45. data/lib/rest-core/app/ask.rb +0 -2
  46. data/lib/rest-core/client/facebook/rails_util.rb +342 -0
  47. data/lib/rest-core/client/{rest-graph.rb → facebook.rb} +21 -28
  48. data/lib/rest-core/client/github.rb +1 -1
  49. data/lib/rest-core/client/linkedin.rb +3 -1
  50. data/lib/rest-core/client/twitter.rb +2 -2
  51. data/lib/rest-core/client.rb +2 -2
  52. data/lib/rest-core/client_oauth1.rb +3 -3
  53. data/lib/rest-core/middleware/oauth2_query.rb +3 -3
  54. data/{test/common.rb → lib/rest-core/test.rb} +7 -3
  55. data/lib/rest-core/util/config.rb +22 -17
  56. data/lib/rest-core/util/vendor.rb +31 -0
  57. data/lib/rest-core/version.rb +1 -1
  58. data/lib/rest-core.rb +5 -0
  59. data/rest-core.gemspec +135 -108
  60. data/test/client/facebook/config/rest-core.yaml +8 -0
  61. data/test/{test_api.rb → client/facebook/test_api.rb} +22 -23
  62. data/test/{test_cache.rb → client/facebook/test_cache.rb} +4 -8
  63. data/test/{test_default.rb → client/facebook/test_default.rb} +6 -10
  64. data/test/client/facebook/test_error.rb +65 -0
  65. data/test/{test_handler.rb → client/facebook/test_handler.rb} +12 -15
  66. data/test/{test_load_config.rb → client/facebook/test_load_config.rb} +6 -9
  67. data/test/{test_misc.rb → client/facebook/test_misc.rb} +15 -18
  68. data/test/{test_oauth.rb → client/facebook/test_oauth.rb} +4 -8
  69. data/test/{test_old.rb → client/facebook/test_old.rb} +16 -18
  70. data/test/{test_page.rb → client/facebook/test_page.rb} +5 -9
  71. data/test/{test_parse.rb → client/facebook/test_parse.rb} +13 -16
  72. data/test/client/facebook/test_serialize.rb +42 -0
  73. data/test/client/facebook/test_timeout.rb +22 -0
  74. data/test/test_oauth1_header.rb +1 -5
  75. metadata +99 -58
  76. data/test/config/rest-core.yaml +0 -7
  77. data/test/test_error.rb +0 -66
  78. data/test/test_serialize.rb +0 -44
  79. data/test/test_timeout.rb +0 -25
@@ -0,0 +1,183 @@
1
+
2
+ require 'test_helper'
3
+ require 'webmock'
4
+
5
+ WebMock.disable_net_connect!
6
+
7
+ class ApplicationControllerTest < ActionController::TestCase
8
+ include WebMock::API
9
+
10
+ def setup
11
+ body = rand(2) == 0 ? '{"error":{"type":"OAuthException"}}' :
12
+ '{"error_code":104}'
13
+
14
+ stub_request(:get, 'https://graph.facebook.com/me').
15
+ to_return(:body => body)
16
+ end
17
+
18
+ def teardown
19
+ WebMock.reset!
20
+ end
21
+
22
+ def assert_url expected
23
+ assert_equal(expected, normalize_url(assigns(:rc_facebook_authorize_url)))
24
+ if @response.status == 200 # js redirect
25
+ assert_equal(
26
+ expected,
27
+ normalize_url(
28
+ @response.body.match(/window\.top\.location\.href = '(.+?)'/)[1]))
29
+
30
+ assert_equal(
31
+ CGI.escapeHTML(expected),
32
+ normalize_url(
33
+ @response.body.match(/content="0;url=(.+?)"/)[1], '&amp;'))
34
+
35
+ assert_equal(
36
+ CGI.escapeHTML(expected),
37
+ normalize_url(
38
+ @response.body.match(/<a href="(.+?)" target="_top">/)[1], '&amp;'))
39
+ end
40
+ end
41
+
42
+ def test_index
43
+ get(:index)
44
+ assert_response :redirect
45
+
46
+ url = normalize_url(
47
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
48
+ 'scope=&redirect_uri=http%3A%2F%2Ftest.host%2F')
49
+
50
+ assert_url(url)
51
+ end
52
+
53
+ def test_canvas
54
+ get(:canvas)
55
+ assert_response :success
56
+
57
+ url = normalize_url(
58
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
59
+ 'scope=publish_stream&' \
60
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fcan%2Fcanvas')
61
+
62
+ assert_url(url)
63
+ end
64
+
65
+ def test_diff_canvas
66
+ get(:diff_canvas)
67
+ assert_response :success
68
+
69
+ url = normalize_url(
70
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
71
+ 'scope=email&' \
72
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2FToT%2Fdiff_canvas')
73
+
74
+ assert_url(url)
75
+ end
76
+
77
+ def test_iframe_canvas
78
+ get(:iframe_canvas)
79
+ assert_response :success
80
+
81
+ url = normalize_url(
82
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
83
+ 'scope=&' \
84
+ 'redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fzzz%2Fiframe_canvas')
85
+
86
+ assert_url(url)
87
+ end
88
+
89
+ def test_options
90
+ get(:options)
91
+ assert_response :redirect
92
+
93
+ url = normalize_url(
94
+ 'https://graph.facebook.com/oauth/authorize?client_id=123&' \
95
+ 'scope=bogus&' \
96
+ 'redirect_uri=http%3A%2F%2Ftest.host%2Foptions')
97
+
98
+ assert_url(url)
99
+ end
100
+
101
+ def test_protected
102
+ assert_nil @controller.public_methods.find{ |m| m.to_s =~ /^rest_graph/ }
103
+ end
104
+
105
+ def test_no_auto
106
+ get(:no_auto)
107
+ assert_response :success
108
+ assert_equal 'XD', @response.body
109
+ end
110
+
111
+ def test_app_id
112
+ get(:diff_app_id)
113
+ assert_response :success
114
+ assert_equal 'zzz', @response.body
115
+ end
116
+
117
+ def test_cache
118
+ WebMock.reset!
119
+ stub_request(:get, 'https://graph.facebook.com/cache').
120
+ to_return(:body => '{"message":"ok"}')
121
+
122
+ get(:cache)
123
+ assert_response :success
124
+ assert_equal '{"message":"ok"}', @response.body
125
+ end
126
+
127
+ def test_handler
128
+ WebMock.reset!
129
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=aloha').
130
+ to_return(:body => '["snowman"]')
131
+
132
+ Rails.cache[:fbs] = RestCore::Facebook.new(:access_token => 'aloha').fbs
133
+ get(:handler_)
134
+ assert_response :success
135
+ assert_equal '["snowman"]', @response.body
136
+ ensure
137
+ Rails.cache.clear
138
+ end
139
+
140
+ def test_session
141
+ WebMock.reset!
142
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=wozilla').
143
+ to_return(:body => '["fireball"]')
144
+
145
+ @request.session[RestCore::Facebook::RailsUtil.rc_facebook_storage_key] =
146
+ RestCore::Facebook.new(:access_token => 'wozilla').fbs
147
+
148
+ get(:session_)
149
+ assert_response :success
150
+ assert_equal '["fireball"]', @response.body
151
+ end
152
+
153
+ def test_cookies
154
+ WebMock.reset!
155
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=blizzard').
156
+ to_return(:body => '["yeti"]')
157
+
158
+ @request.cookies[RestCore::Facebook::RailsUtil.rc_facebook_storage_key] =
159
+ RestCore::Facebook.new(:access_token => 'blizzard').fbs
160
+
161
+ get(:cookies_)
162
+ assert_response :success
163
+ assert_equal '["yeti"]', @response.body
164
+ end
165
+
166
+ def test_error
167
+ get(:error)
168
+ rescue => e
169
+ assert_equal RestCore::Facebook::Error, e.class
170
+ end
171
+
172
+ def test_reinitailize
173
+ get(:reinitialize)
174
+ assert_response :success
175
+ assert_equal [nil, {'a' => 'b'}], YAML.load(@response.body)
176
+ end
177
+
178
+ def test_helper
179
+ get(:helper)
180
+ assert_response :success
181
+ assert_equal RestCore::Facebook.default_app_id, @response.body.strip
182
+ end
183
+ end
@@ -0,0 +1,18 @@
1
+
2
+ ENV["RAILS_ENV"] = "test"
3
+ require File.expand_path('../../config/environment', __FILE__)
4
+ begin
5
+ require 'rails/test_help'
6
+ rescue LoadError # for rails2
7
+ require 'test_help'
8
+ end
9
+
10
+ class ActiveSupport::TestCase
11
+ def normalize_query query, amp='&'
12
+ '?' + query[1..-1].split(amp).sort.join(amp)
13
+ end
14
+
15
+ def normalize_url url, amp='&'
16
+ url.sub(/\?.+/){ |query| normalize_query(query, amp) }
17
+ end
18
+ end
@@ -0,0 +1,44 @@
1
+
2
+ require 'test_helper'
3
+ require 'rr'
4
+
5
+ class RailsUtilTest < ActiveSupport::TestCase
6
+ include RR::Adapters::TestUnit
7
+
8
+ def setup_mock url
9
+ mock(RestCore::Facebook::RailsUtil).rc_facebook_in_canvas?{ false }
10
+ mock(RestCore::Facebook::RailsUtil).request{
11
+ mock(Object.new).url{ url }
12
+ }
13
+ end
14
+
15
+ def test_rest_graph_normalized_request_uri_0
16
+ setup_mock( 'http://test.com/?code=123&lang=en')
17
+ assert_equal('http://test.com/?lang=en',
18
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
19
+ end
20
+
21
+ def test_rest_graph_normalized_request_uri_1
22
+ setup_mock( 'http://test.com/?lang=en&code=123')
23
+ assert_equal('http://test.com/?lang=en',
24
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
25
+ end
26
+
27
+ def test_rest_graph_normalized_request_uri_2
28
+ setup_mock( 'http://test.com/?session=abc&lang=en&code=123')
29
+ assert_equal('http://test.com/?lang=en',
30
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
31
+ end
32
+
33
+ def test_rest_graph_normalized_request_uri_3
34
+ setup_mock( 'http://test.com/?code=123')
35
+ assert_equal('http://test.com/',
36
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
37
+ end
38
+
39
+ def test_rest_graph_normalized_request_uri_4
40
+ setup_mock( 'http://test.com/?signed_request=abc&code=123')
41
+ assert_equal('http://test.com/',
42
+ RestCore::Facebook::RailsUtil.rc_facebook_normalized_request_uri)
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+
2
+ require 'sinatra'
3
+ require 'rest-core'
4
+
5
+ app_id = '123'
6
+ secret = 'abc'
7
+ config = {:app_id => app_id,
8
+ :secret => secret}
9
+
10
+ post '/' do
11
+ fb = RestCore::Facebook.new(config)
12
+ fb.parse_signed_request!(params['signed_request'])
13
+ "#{fb.get('me').inspect.gsub('<', '&lt;')}\n"
14
+ end
15
+
16
+ run Sinatra::Application
@@ -1,8 +1,6 @@
1
1
 
2
2
  require 'rest-core/middleware'
3
3
 
4
- require 'restclient'
5
-
6
4
  class RestCore::Ask
7
5
  include RestCore::Middleware
8
6
  def call env
@@ -0,0 +1,342 @@
1
+
2
+ require 'cgi'
3
+ require 'uri'
4
+
5
+ class RestCore::Facebook
6
+ module DefaultAttributes
7
+ def default_canvas ; '' ; end
8
+ def default_iframe ; false; end
9
+ def default_auto_authorize ; false; end
10
+ def default_auto_authorize_options; {} ; end
11
+ def default_auto_authorize_scope ; '' ; end
12
+ def default_ensure_authorized ; false; end
13
+ def default_write_session ; false; end
14
+ def default_write_cookies ; false; end
15
+ def default_write_handler ; nil; end
16
+ def default_check_handler ; nil; end
17
+ end
18
+
19
+ module RailsCache
20
+ def [] key ; read(key) ; end
21
+ def []= key, value; write(key, value) ; end
22
+ def store key, value,
23
+ options={}; write(key, value, options); end
24
+ end
25
+ end
26
+
27
+ module RestCore::Facebook::RailsUtil
28
+ def self.init app=Rails
29
+ ActiveSupport::Cache::Store.send(:include, RestCore::Facebook::RailsCache)
30
+ RestCore::Config.load_for_rails(RestCore::Facebook, 'facebook', app)
31
+ end
32
+
33
+ module Helper
34
+ def rc_facebook
35
+ controller.send(:rc_facebook)
36
+ end
37
+ end
38
+
39
+ def self.included controller
40
+ # skip if included already, any better way to detect this?
41
+ return if controller.respond_to?(:rc_facebook, true)
42
+
43
+ controller.rescue_from(RestCore::Facebook::Error::AccessToken,
44
+ :with => :rc_facebook_on_access_token_error)
45
+ controller.helper(RestCore::Facebook::RailsUtil::Helper)
46
+ controller.instance_methods.select{ |method|
47
+ method.to_s =~ /^rc_facebook/
48
+ }.each{ |method| controller.send(:protected, method) }
49
+ end
50
+
51
+ def rc_facebook_setup options={}
52
+ rc_facebook_options_ctl.merge!(
53
+ rc_facebook_extract_options(options, :reject))
54
+ rc_facebook_options_new.merge!(
55
+ rc_facebook_extract_options(options, :select))
56
+
57
+ # we'll need to reinitialize rc_facebook with the new options,
58
+ # otherwise if you're calling rc_facebook before rc_facebook_setup,
59
+ # you'll end up with default options without the ones you've passed
60
+ # into rc_facebook_setup.
61
+ rc_facebook.send(:initialize, rc_facebook_options_new)
62
+
63
+ rc_facebook_check_params_signed_request # canvas
64
+ rc_facebook_check_params_session # i think it would be deprecated
65
+ rc_facebook_check_cookie # for js sdk (canvas or not)
66
+ rc_facebook_check_code # oauth api
67
+
68
+ # there are above 4 ways to check the user identity!
69
+ # if nor of them passed, then we can suppose the user
70
+ # didn't authorize for us, but we can check if user has authorized
71
+ # before, in that case, the fbs would be inside session,
72
+ # as we just saved it there
73
+
74
+ rc_facebook_check_rg_fbs # check rc_facebook storage
75
+
76
+ if rc_facebook_oget(:ensure_authorized) && !rc_facebook.authorized?
77
+ rc_facebook_authorize('ensure authorized')
78
+ false # action halt, redirect to do authorize,
79
+ # eagerly, as opposed to auto_authorize
80
+ else
81
+ true # keep going
82
+ end
83
+ end
84
+
85
+ # override this if you need different app_id and secret
86
+ def rc_facebook
87
+ @rc_facebook ||= RestCore::Facebook.new(rc_facebook_options_new)
88
+ end
89
+
90
+ def rc_facebook_on_access_token_error error=nil
91
+ rc_facebook_authorize(error, false)
92
+ end
93
+
94
+ def rc_facebook_authorize error=nil, force_redirect=true
95
+ logger.warn("WARN: Facebook: #{error.inspect}")
96
+
97
+ if force_redirect || rc_facebook_auto_authorize?
98
+ @rc_facebook_authorize_url = rc_facebook.authorize_url(
99
+ {:redirect_uri => rc_facebook_normalized_request_uri,
100
+ :scope => rc_facebook_oget(:auto_authorize_scope)}.
101
+ merge(rc_facebook_oget(:auto_authorize_options)))
102
+
103
+ logger.debug(
104
+ "DEBUG: Facebook: redirect to #{@rc_facebook_authorize_url}")
105
+
106
+ cookies.delete("fbs_#{rc_facebook.app_id}")
107
+ rc_facebook_authorize_redirect
108
+ end
109
+ end
110
+
111
+ # override this if you want the simple redirect_to
112
+ def rc_facebook_authorize_redirect
113
+ unless rc_facebook_in_canvas?
114
+ redirect_to @rc_facebook_authorize_url
115
+ else
116
+ rc_facebook_js_redirect(@rc_facebook_authorize_url,
117
+ rc_facebook_authorize_body)
118
+ end
119
+ end
120
+
121
+ def rc_facebook_js_redirect redirect_url, body=''
122
+ render :inline => <<-HTML
123
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
124
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
125
+ <html>
126
+ <head>
127
+ <script type="text/javascript">
128
+ window.top.location.href = '#{redirect_url}'
129
+ </script>
130
+ <noscript>
131
+ <meta http-equiv="refresh" content="0;url=#{
132
+ CGI.escapeHTML(redirect_url)}"/>
133
+ <meta http-equiv="window-target" content="_top"/>
134
+ </noscript>
135
+ </head>
136
+ <body>
137
+ #{body}
138
+ </bodt>
139
+ </html>
140
+ HTML
141
+ end
142
+
143
+ def rc_facebook_authorize_body redirect_url=@rc_facebook_authorize_url
144
+ <<-HTML
145
+ <div>
146
+ Please
147
+ <a href="#{CGI.escapeHTML(redirect_url)}" target="_top">authorize</a>
148
+ if this page is not automatically redirected.
149
+ </div>
150
+ HTML
151
+ end
152
+
153
+ module_function
154
+
155
+ # ==================== begin options utility =======================
156
+ def rc_facebook_oget key
157
+ if rc_facebook_options_ctl.has_key?(key)
158
+ rc_facebook_options_ctl[key]
159
+ else
160
+ RestCore::Facebook.send("default_#{key}")
161
+ end
162
+ end
163
+
164
+ def rc_facebook_options_ctl
165
+ @rc_facebook_options_ctl ||= {}
166
+ end
167
+
168
+ def rc_facebook_options_new
169
+ @rc_facebook_options_new ||= {:log_method => logger.method(:debug)}
170
+ end
171
+ # ==================== end options utility =======================
172
+
173
+
174
+
175
+ # ==================== begin facebook check ======================
176
+ def rc_facebook_check_params_signed_request
177
+ return if rc_facebook.authorized? || !params[:signed_request]
178
+
179
+ rc_facebook.parse_signed_request!(params[:signed_request])
180
+ logger.debug("DEBUG: Facebook: detected signed_request," \
181
+ " parsed: #{rc_facebook.data.inspect}")
182
+
183
+ if rc_facebook.authorized?
184
+ rc_facebook_write_rg_fbs
185
+ else
186
+ logger.warn(
187
+ "WARN: Facebook: bad signed_request: #{params[:signed_request]}")
188
+ end
189
+ end
190
+
191
+ # if the code is bad or not existed,
192
+ # check if there's one in session,
193
+ # meanwhile, there the sig and access_token is correct,
194
+ # that means we're in the context of canvas
195
+ def rc_facebook_check_params_session
196
+ return if rc_facebook.authorized? || !params[:session]
197
+
198
+ rc_facebook.parse_json!(params[:session])
199
+ logger.debug("DEBUG: Facebook: detected session, parsed:" \
200
+ " #{rc_facebook.data.inspect}")
201
+
202
+ if rc_facebook.authorized?
203
+ rc_facebook_write_rg_fbs
204
+ else
205
+ logger.warn("WARN: Facebook: bad session: #{params[:session]}")
206
+ end
207
+ end
208
+
209
+ # if we're not in canvas nor code passed,
210
+ # we could check out cookies as well.
211
+ def rc_facebook_check_cookie
212
+ return if rc_facebook.authorized? ||
213
+ !cookies["fbs_#{rc_facebook.app_id}"]
214
+
215
+ rc_facebook.parse_cookies!(cookies)
216
+ logger.debug("DEBUG: Facebook: detected cookies, parsed:" \
217
+ " #{rc_facebook.data.inspect}")
218
+ end
219
+
220
+ # exchange the code with access_token
221
+ def rc_facebook_check_code
222
+ return if rc_facebook.authorized? || !params[:code]
223
+
224
+ rc_facebook.authorize!(:code => params[:code],
225
+ :redirect_uri => rc_facebook_normalized_request_uri)
226
+ logger.debug(
227
+ "DEBUG: Facebook: detected code with " \
228
+ "#{rc_facebook_normalized_request_uri}," \
229
+ " parsed: #{rc_facebook.data.inspect}")
230
+
231
+ rc_facebook_write_rg_fbs if rc_facebook.authorized?
232
+ end
233
+ # ==================== end facebook check ======================
234
+
235
+
236
+
237
+ # ==================== begin check ================================
238
+ def rc_facebook_storage_key
239
+ "rc_facebook_fbs_#{rc_facebook_oget(:app_id)}"
240
+ end
241
+
242
+ def rc_facebook_check_rg_fbs
243
+ rc_facebook_check_rg_handler # custom method to store fbs
244
+ rc_facebook_check_rg_session # prefered way to store fbs
245
+ rc_facebook_check_rg_cookies # in canvas, session might not work..
246
+ end
247
+
248
+ def rc_facebook_check_rg_handler handler=rc_facebook_oget(:check_handler)
249
+ return if rc_facebook.authorized? || !handler
250
+ rc_facebook.parse_fbs!(handler.call)
251
+ logger.debug("DEBUG: Facebook: called check_handler, parsed:" \
252
+ " #{rc_facebook.data.inspect}")
253
+ end
254
+
255
+ def rc_facebook_check_rg_session
256
+ return if rc_facebook.authorized? || !rc_facebook_oget(:write_session) ||
257
+ !(fbs = session[rc_facebook_storage_key])
258
+ rc_facebook.parse_fbs!(fbs)
259
+ logger.debug("DEBUG: Facebook: detected rc_facebook session, parsed:" \
260
+ " #{rc_facebook.data.inspect}")
261
+ end
262
+
263
+ def rc_facebook_check_rg_cookies
264
+ return if rc_facebook.authorized? || !rc_facebook_oget(:write_cookies) ||
265
+ !(fbs = cookies[rc_facebook_storage_key])
266
+ rc_facebook.parse_fbs!(fbs)
267
+ logger.debug("DEBUG: Facebook: detected rc_facebook cookies, parsed:" \
268
+ " #{rc_facebook.data.inspect}")
269
+ end
270
+ # ==================== end check ================================
271
+ # ==================== begin write ================================
272
+ def rc_facebook_write_rg_fbs
273
+ cookies.delete("fbs_#{rc_facebook.app_id}")
274
+ rc_facebook_write_rg_handler
275
+ rc_facebook_write_rg_session
276
+ rc_facebook_write_rg_cookies
277
+ end
278
+
279
+ def rc_facebook_write_rg_handler handler=rc_facebook_oget(:write_handler)
280
+ return if !handler
281
+ handler.call(fbs = rc_facebook.fbs)
282
+ logger.debug("DEBUG: Facebook: called write_handler: fbs => #{fbs}")
283
+ end
284
+
285
+ def rc_facebook_write_rg_session
286
+ return if !rc_facebook_oget(:write_session)
287
+ session[rc_facebook_storage_key] = fbs = rc_facebook.fbs
288
+ logger.debug("DEBUG: Facebook: wrote session: fbs => #{fbs}")
289
+ end
290
+
291
+ def rc_facebook_write_rg_cookies
292
+ return if !rc_facebook_oget(:write_cookies)
293
+ cookies[rc_facebook_storage_key] = fbs = rc_facebook.fbs
294
+ logger.debug("DEBUG: Facebook: wrote cookies: fbs => #{fbs}")
295
+ end
296
+ # ==================== end write ================================
297
+
298
+
299
+
300
+ # ==================== begin misc ================================
301
+ def rc_facebook_normalized_request_uri
302
+ uri = if rc_facebook_in_canvas?
303
+ # rails 3 uses newer rack which has fullpath
304
+ "http://apps.facebook.com/#{rc_facebook_oget(:canvas)}" +
305
+ (request.respond_to?(:fullpath) ?
306
+ request.fullpath : request.request_uri)
307
+ else
308
+ request.url
309
+ end
310
+
311
+ rc_facebook_filter_uri(uri)
312
+ end
313
+
314
+ def rc_facebook_filter_uri uri
315
+ URI.parse(URI.encode(uri)).tap{ |uri|
316
+ uri.query = uri.query.split('&').reject{ |q|
317
+ q =~ /^(code|session|signed_request)\=/
318
+ }.join('&') if uri.query
319
+ uri.query = nil if uri.query.blank?
320
+ }.to_s
321
+ end
322
+
323
+ def rc_facebook_in_canvas?
324
+ !rc_facebook_oget(:canvas).blank?
325
+ end
326
+
327
+ def rc_facebook_auto_authorize?
328
+ !rc_facebook_oget(:auto_authorize_scope) .blank? ||
329
+ !rc_facebook_oget(:auto_authorize_options).blank? ||
330
+ rc_facebook_oget(:auto_authorize)
331
+ end
332
+
333
+ def rc_facebook_extract_options options, method
334
+ # Hash[] is for ruby 1.8.7
335
+ # map(&:to_sym) is for ruby 1.8.7
336
+ Hash[options.send(method){ |(k, v)|
337
+ RestCore::Facebook.members.map(&:to_sym).member?(k) }]
338
+ end
339
+ # ==================== end misc ================================
340
+ end
341
+
342
+ RestCore::Facebook::RailsUtil.init(Rails)