rest-graph 1.4.4 → 1.4.5

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/CHANGES CHANGED
@@ -1,5 +1,18 @@
1
1
  = rest-graph changes history
2
2
 
3
+ == rest-graph 1.4.5 -- 2010-08-07
4
+
5
+ * [RestGraph] Treat oauth_token as access_token as well. This came from
6
+ Facebook's new signed_request. Why didn't they choose
7
+ consistent name? Why different signature algorithm?
8
+
9
+ * [RailsUtil] Fixed a bug that didn't reject signed_request in redirect_uri.
10
+ Now code, session, and signed_request are rejected.
11
+
12
+ * [RailsUtil] Added write_handler and check_handler option to write/check
13
+ fbs with user code, instead of using sessions/cookies.
14
+ That way, you can save fbs into memcache or somewhere.
15
+
3
16
  == rest-graph 1.4.4 -- 2010-08-06
4
17
 
5
18
  * [RailsUtil] Fixed a bug that empty query appends a question mark,
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = rest-graph 1.4.3
1
+ = rest-graph 1.4.5
2
2
  by Cardinal Blue ( http://cardinalblue.com )
3
3
 
4
4
  == LINKS:
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- = rest-graph 1.4.3
1
+ = rest-graph 1.4.5
2
2
  by Cardinal Blue ( http://cardinalblue.com )
3
3
 
4
4
  == LINKS:
@@ -18,6 +18,9 @@ class ApplicationController < ActionController::Base
18
18
  before_filter :filter_diff_canvas , :only => [:diff_canvas]
19
19
  before_filter :filter_iframe_canvas, :only => [:iframe_canvas]
20
20
  before_filter :filter_cache , :only => [:cache]
21
+ before_filter :filter_hanlder , :only => [:handler_]
22
+ before_filter :filter_session , :only => [:session_]
23
+ before_filter :filter_cookies , :only => [:cookies_]
21
24
 
22
25
  def index
23
26
  render :text => rest_graph.get('me').to_json
@@ -26,6 +29,9 @@ class ApplicationController < ActionController::Base
26
29
  alias_method :options , :index
27
30
  alias_method :diff_canvas , :index
28
31
  alias_method :iframe_canvas, :index
32
+ alias_method :handler_ , :index
33
+ alias_method :session_ , :index
34
+ alias_method :cookies_ , :index
29
35
 
30
36
  def no_auto
31
37
  rest_graph.get('me')
@@ -82,4 +88,25 @@ class ApplicationController < ActionController::Base
82
88
  def filter_cache
83
89
  rest_graph_setup(:cache => Rails.cache)
84
90
  end
91
+
92
+ def filter_hanlder
93
+ rest_graph_setup(:write_handler => method(:write_handler),
94
+ :check_handler => method(:check_handler))
95
+ end
96
+
97
+ def write_handler fbs
98
+ Rails.cache[:fbs] = fbs
99
+ end
100
+
101
+ def check_handler
102
+ Rails.cache[:fbs]
103
+ end
104
+
105
+ def filter_session
106
+ rest_graph_setup(:write_session => true)
107
+ end
108
+
109
+ def filter_cookies
110
+ rest_graph_setup(:write_cookies => true)
111
+ end
85
112
  end
@@ -84,6 +84,7 @@ class ApplicationControllerTest < ActionController::TestCase
84
84
  end
85
85
 
86
86
  def test_cache
87
+ reset_webmock
87
88
  stub_request(:get, 'https://graph.facebook.com/cache').
88
89
  to_return(:body => '{"message":"ok"}')
89
90
 
@@ -91,4 +92,43 @@ class ApplicationControllerTest < ActionController::TestCase
91
92
  assert_response :success
92
93
  assert_equal '{"message":"ok"}', @response.body
93
94
  end
95
+
96
+ def test_handler
97
+ reset_webmock
98
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=aloha').
99
+ to_return(:body => '["snowman"]')
100
+
101
+ Rails.cache[:fbs] = RestGraph.new(:access_token => 'aloha').fbs
102
+ get(:handler_)
103
+ assert_response :success
104
+ assert_equal '["snowman"]', @response.body
105
+ ensure
106
+ Rails.cache.clear
107
+ end
108
+
109
+ def test_session
110
+ reset_webmock
111
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=wozilla').
112
+ to_return(:body => '["fireball"]')
113
+
114
+ @request.session['rest_graph_session'] =
115
+ RestGraph.new(:access_token => 'wozilla').fbs
116
+
117
+ get(:session_)
118
+ assert_response :success
119
+ assert_equal '["fireball"]', @response.body
120
+ end
121
+
122
+ def test_cookies
123
+ reset_webmock
124
+ stub_request(:get, 'https://graph.facebook.com/me?access_token=blizzard').
125
+ to_return(:body => '["yeti"]')
126
+
127
+ @request.cookies['rest_graph_cookies'] =
128
+ RestGraph.new(:access_token => 'blizzard').fbs
129
+
130
+ get(:cookies_)
131
+ assert_response :success
132
+ assert_equal '["yeti"]', @response.body
133
+ end
94
134
  end
@@ -35,4 +35,10 @@ class RailsUtilTest < ActiveSupport::TestCase
35
35
  assert_equal('http://test.com/',
36
36
  RestGraph::RailsUtil.rest_graph_normalized_request_uri)
37
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
+ RestGraph::RailsUtil.rest_graph_normalized_request_uri)
43
+ end
38
44
  end
data/lib/rest-graph.rb CHANGED
@@ -76,7 +76,7 @@ class RestGraph < RestGraphStruct
76
76
  end
77
77
 
78
78
  def access_token
79
- data['access_token']
79
+ data['access_token'] || data['oauth_token']
80
80
  end
81
81
 
82
82
  def access_token= token
@@ -10,6 +10,8 @@ class RestGraph
10
10
  def default_auto_authorize_scope ; '' ; end
11
11
  def default_write_session ; false; end
12
12
  def default_write_cookies ; false; end
13
+ def default_write_handler ; nil; end
14
+ def default_check_handler ; nil; end
13
15
  end
14
16
 
15
17
  module RailsCache
@@ -49,8 +51,7 @@ module RestGraph::RailsUtil
49
51
  # before, in that case, the fbs would be inside session,
50
52
  # as we just saved it there
51
53
 
52
- rest_graph_check_rg_session # prefered way to store fbs
53
- rest_graph_check_rg_cookies # in canvas, session might not work..
54
+ rest_graph_check_rg_fbs
54
55
  end
55
56
 
56
57
  # override this if you need different app_id and secret
@@ -110,8 +111,7 @@ module RestGraph::RailsUtil
110
111
 
111
112
  module_function
112
113
 
113
- # ==================== options utility =======================
114
-
114
+ # ==================== begin options utility =======================
115
115
  def rest_graph_oget key
116
116
  if rest_graph_options_ctl.has_key?(key)
117
117
  rest_graph_options_ctl[key]
@@ -129,9 +129,11 @@ module RestGraph::RailsUtil
129
129
  {:error_handler => method(:rest_graph_authorize),
130
130
  :log_handler => method(:rest_graph_log)}
131
131
  end
132
+ # ==================== end options utility =======================
132
133
 
133
- # ==================== checking utility ======================
134
134
 
135
+
136
+ # ==================== begin facebook check ======================
135
137
  # if we're not in canvas nor code passed,
136
138
  # we could check out cookies as well.
137
139
  def rest_graph_check_cookie
@@ -151,8 +153,7 @@ module RestGraph::RailsUtil
151
153
  " #{rest_graph.data.inspect}")
152
154
 
153
155
  if rest_graph.authorized?
154
- rest_graph_write_rg_session
155
- rest_graph_write_rg_cookies
156
+ rest_graph_write_rg_fbs
156
157
  else
157
158
  logger.warn(
158
159
  "WARN: RestGraph: bad signed_request: #{params[:signed_request]}")
@@ -171,8 +172,7 @@ module RestGraph::RailsUtil
171
172
  " #{rest_graph.data.inspect}")
172
173
 
173
174
  if rest_graph.authorized?
174
- rest_graph_write_rg_session
175
- rest_graph_write_rg_cookies
175
+ rest_graph_write_rg_fbs
176
176
  else
177
177
  logger.warn("WARN: RestGraph: bad session: #{params[:session]}")
178
178
  end
@@ -189,42 +189,72 @@ module RestGraph::RailsUtil
189
189
  "#{rest_graph_normalized_request_uri}, " \
190
190
  "parsed: #{rest_graph.data.inspect}")
191
191
 
192
- if rest_graph.authorized?
193
- rest_graph_write_rg_session
194
- rest_graph_write_rg_cookies
195
- end
192
+ rest_graph_write_rg_fbs if rest_graph.authorized?
193
+ end
194
+ # ==================== end facebook check ======================
195
+
196
+
197
+
198
+ # ==================== begin check ================================
199
+ def rest_graph_check_rg_fbs
200
+ rest_graph_check_rg_handler # custom method to store fbs
201
+ rest_graph_check_rg_session # prefered way to store fbs
202
+ rest_graph_check_rg_cookies # in canvas, session might not work..
203
+ end
204
+
205
+ def rest_graph_check_rg_handler
206
+ return if rest_graph.authorized? || !rest_graph_oget(:check_handler)
207
+ rest_graph.parse_fbs!(rest_graph_oget(:check_handler).call)
208
+ logger.debug("DEBUG: RestGraph: called check_handler, parsed:" \
209
+ " #{rest_graph.data.inspect}")
196
210
  end
197
211
 
198
212
  def rest_graph_check_rg_session
199
- return if rest_graph.authorized? || !session['rest_graph_session']
200
- rest_graph.parse_fbs!(session['rest_graph_session'])
213
+ return if rest_graph.authorized? || !session[:rest_graph_session]
214
+ rest_graph.parse_fbs!(session[:rest_graph_session])
201
215
  logger.debug("DEBUG: RestGraph: detected rest-graph session, parsed:" \
202
216
  " #{rest_graph.data.inspect}")
203
217
  end
204
218
 
205
219
  def rest_graph_check_rg_cookies
206
- return if rest_graph.authorized? || !cookies['rest_graph_cookies']
207
- rest_graph.parse_fbs!(cookies['rest_graph_cookies'])
220
+ return if rest_graph.authorized? || !cookies[:rest_graph_cookies]
221
+ rest_graph.parse_fbs!(cookies[:rest_graph_cookies])
208
222
  logger.debug("DEBUG: RestGraph: detected rest-graph cookies, parsed:" \
209
223
  " #{rest_graph.data.inspect}")
210
224
  end
225
+ # ==================== end check ================================
226
+ # ==================== begin write ================================
227
+ def rest_graph_write_rg_fbs
228
+ rest_graph_write_rg_handler
229
+ rest_graph_write_rg_session
230
+ rest_graph_write_rg_cookies
231
+ end
211
232
 
212
- # ==================== others ================================
233
+ def rest_graph_write_rg_handler
234
+ return if !rest_graph_oget(:write_handler)
235
+ fbs = rest_graph.fbs
236
+ rest_graph_oget(:write_handler).call(fbs)
237
+ logger.debug("DEBUG: RestGraph: called write_handler: fbs => #{fbs}")
238
+ end
213
239
 
214
240
  def rest_graph_write_rg_session
215
241
  return if !rest_graph_oget(:write_session)
216
242
  fbs = rest_graph.fbs
217
- session['rest_graph_session'] = fbs
243
+ session[:rest_graph_session] = fbs
218
244
  logger.debug("DEBUG: RestGraph: wrote session: fbs => #{fbs}")
219
245
  end
220
246
 
221
247
  def rest_graph_write_rg_cookies
222
248
  return if !rest_graph_oget(:write_cookies)
223
249
  fbs = rest_graph.fbs
224
- cookies['rest_graph_cookies'] = fbs
250
+ cookies[:rest_graph_cookies] = fbs
225
251
  logger.debug("DEBUG: RestGraph: wrote cookies: fbs => #{fbs}")
226
252
  end
253
+ # ==================== end write ================================
254
+
255
+
227
256
 
257
+ # ==================== begin misc ================================
228
258
  def rest_graph_log event
229
259
  message = "DEBUG: RestGraph: spent #{sprintf('%f', event.duration)} "
230
260
  case event
@@ -247,7 +277,7 @@ module RestGraph::RailsUtil
247
277
  end).
248
278
  tap{ |uri|
249
279
  uri.query = uri.query.split('&').reject{ |q|
250
- q =~ /^(code|session)\=/
280
+ q =~ /^(code|session|signed_request)\=/
251
281
  }.join('&') if uri.query
252
282
  uri.query = nil if uri.query.blank?
253
283
  }.to_s
@@ -268,4 +298,5 @@ module RestGraph::RailsUtil
268
298
  return result if result.kind_of?(Hash) # RUBY_VERSION >= 1.9.1
269
299
  result.inject({}){ |r, (k, v)| r[k] = v; r }
270
300
  end
301
+ # ==================== end misc ================================
271
302
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  require 'rest-graph'
3
3
 
4
- RestGraph::VERSION = '1.4.4'
4
+ RestGraph::VERSION = '1.4.5'
@@ -130,4 +130,12 @@ describe RestGraph do
130
130
  }
131
131
  cache.should == {rg.send(:cache_key, url) => body}
132
132
  end
133
+
134
+ it 'would treat oauth_token as access_token as well' do
135
+ rg = RestGraph.new
136
+ hate_facebook = 'why the hell two different name?'
137
+ rg.data['oauth_token'] = hate_facebook
138
+ rg.authorized?.should == true
139
+ rg.access_token == hate_facebook
140
+ end
133
141
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-graph
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 4
9
- - 4
10
- version: 1.4.4
9
+ - 5
10
+ version: 1.4.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Cardinal Blue
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-08-06 00:00:00 +08:00
19
+ date: 2010-08-07 00:00:00 +08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency