rest-more 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.travis.yml +2 -1
  2. data/CHANGES.md +20 -0
  3. data/Gemfile +5 -1
  4. data/README.md +26 -0
  5. data/bin/rib-rest-core +22 -0
  6. data/example/rails2/Gemfile +1 -0
  7. data/example/rails2/app/views/application/helper.html.erb +1 -1
  8. data/example/rails2/config/environment.rb +10 -0
  9. data/example/rails2/config/rest-core.yaml +2 -2
  10. data/example/rails2/test/functional/application_controller_test.rb +3 -3
  11. data/example/rails2/test/unit/rails_util_test.rb +12 -7
  12. data/example/rails3/Gemfile +1 -0
  13. data/example/rails3/app/views/application/helper.html.erb +1 -1
  14. data/example/rails3/config/rest-core.yaml +2 -2
  15. data/example/rails3/test/functional/application_controller_test.rb +3 -3
  16. data/example/rails3/test/unit/rails_util_test.rb +12 -7
  17. data/lib/rest-core/client/bing.rb +92 -0
  18. data/lib/rest-core/client/bing/rails_util.rb +13 -0
  19. data/lib/rest-core/client/facebook.rb +2 -2
  20. data/lib/rest-core/client/facebook/rails_util.rb +28 -65
  21. data/lib/rest-core/client/flurry.rb +3 -3
  22. data/lib/rest-core/client/flurry/rails_util.rb +3 -64
  23. data/lib/rest-core/client/github.rb +3 -0
  24. data/lib/rest-core/client/github/rails_util.rb +13 -0
  25. data/lib/rest-core/client/linkedin.rb +2 -0
  26. data/lib/rest-core/client/linkedin/rails_util.rb +13 -0
  27. data/lib/rest-core/client/mixi.rb +2 -0
  28. data/lib/rest-core/client/mixi/rails_util.rb +13 -0
  29. data/lib/rest-core/client/twitter.rb +2 -0
  30. data/lib/rest-core/client/twitter/rails_util.rb +13 -0
  31. data/lib/rest-core/util/rails_util_util.rb +112 -5
  32. data/lib/rest-more.rb +4 -3
  33. data/lib/rest-more/version.rb +1 -1
  34. data/lib/rib/app/rest-core.rb +15 -0
  35. data/rest-more.gemspec +13 -4
  36. data/test/client/bing/test_api.rb +34 -0
  37. data/test/client/facebook/test_api.rb +12 -12
  38. data/test/client/facebook/test_cache.rb +3 -3
  39. data/test/client/facebook/test_default.rb +5 -5
  40. data/test/client/facebook/test_error.rb +25 -25
  41. data/test/client/facebook/test_handler.rb +12 -12
  42. data/test/client/facebook/test_load_config.rb +4 -4
  43. data/test/client/facebook/test_misc.rb +16 -16
  44. data/test/client/facebook/test_oauth.rb +3 -3
  45. data/test/client/facebook/test_old.rb +11 -11
  46. data/test/client/facebook/test_page.rb +4 -4
  47. data/test/client/facebook/test_parse.rb +14 -14
  48. data/test/client/facebook/test_serialize.rb +4 -4
  49. data/test/client/facebook/test_timeout.rb +4 -4
  50. data/test/client/flurry/test_metrics.rb +1 -1
  51. data/test/client/twitter/test_api.rb +1 -1
  52. metadata +16 -7
  53. data/lib/rest-core/client/simple.rb +0 -2
  54. data/lib/rest-core/client/universal.rb +0 -18
@@ -20,41 +20,18 @@ module RestCore::Facebook::DefaultAttributes
20
20
  end
21
21
 
22
22
  module RestCore::Facebook::RailsUtil
23
- def self.init app=Rails
24
- RestCore::Config.load_for_rails(RestCore::Facebook, 'facebook', app)
25
- end
26
-
27
- module Helper
28
- def rc_facebook
29
- controller.send(:rc_facebook)
30
- end
31
- end
23
+ include RestCore::RailsUtilUtil
32
24
 
33
25
  def self.included controller
34
26
  # skip if included already, any better way to detect this?
35
27
  return if controller.respond_to?(:rc_facebook, true)
36
-
28
+ super
37
29
  controller.rescue_from(RestCore::Facebook::Error::AccessToken,
38
30
  :with => :rc_facebook_on_access_token_error)
39
- controller.helper(RestCore::Facebook::RailsUtil::Helper)
40
- controller.instance_methods.select{ |method|
41
- method.to_s =~ /^rc_facebook/
42
- }.each{ |method| controller.send(:protected, method) }
43
31
  end
44
32
 
45
33
  def rc_facebook_setup options={}
46
- rc_facebook_options_ctl.merge!(
47
- RestCore::RailsUtilUtil.extract_options(
48
- RestCore::Facebook.members, options, :reject))
49
- rc_facebook_options_new.merge!(
50
- RestCore::RailsUtilUtil.extract_options(
51
- RestCore::Facebook.members, options, :select))
52
-
53
- # we'll need to reinitialize rc_facebook with the new options,
54
- # otherwise if you're calling rc_facebook before rc_facebook_setup,
55
- # you'll end up with default options without the ones you've passed
56
- # into rc_facebook_setup.
57
- rc_facebook.send(:initialize, rc_facebook_options_new)
34
+ super
58
35
 
59
36
  rc_facebook_check_params_signed_request # canvas
60
37
  rc_facebook_check_params_session # i think it would be deprecated
@@ -69,7 +46,9 @@ module RestCore::Facebook::RailsUtil
69
46
 
70
47
  rc_facebook_check_rg_fbs # check rc_facebook storage
71
48
 
72
- if rc_facebook_oget(:ensure_authorized) && !rc_facebook.authorized?
49
+ if rc_options_get(RestCore::Facebook, :ensure_authorized) &&
50
+ !rc_facebook.authorized?
51
+
73
52
  rc_facebook_authorize('ensure authorized')
74
53
  false # action halt, redirect to do authorize,
75
54
  # eagerly, as opposed to auto_authorize
@@ -78,11 +57,6 @@ module RestCore::Facebook::RailsUtil
78
57
  end
79
58
  end
80
59
 
81
- # override this if you need different app_id and secret
82
- def rc_facebook
83
- @rc_facebook ||= RestCore::Facebook.new(rc_facebook_options_new)
84
- end
85
-
86
60
  def rc_facebook_on_access_token_error error=nil
87
61
  rc_facebook_authorize(error, false)
88
62
  end
@@ -93,8 +67,9 @@ module RestCore::Facebook::RailsUtil
93
67
  if force_redirect || rc_facebook_auto_authorize?
94
68
  @rc_facebook_authorize_url = rc_facebook.authorize_url(
95
69
  {:redirect_uri => rc_facebook_normalized_request_uri,
96
- :scope => rc_facebook_oget(:auto_authorize_scope)}.
97
- merge(rc_facebook_oget(:auto_authorize_options)))
70
+ :scope =>
71
+ rc_options_get(RestCore::Facebook, :auto_authorize_scope)}.
72
+ merge(rc_options_get(RestCore::Facebook, :auto_authorize_options)))
98
73
 
99
74
  logger.debug(
100
75
  "DEBUG: Facebook: redirect to #{@rc_facebook_authorize_url}")
@@ -146,26 +121,6 @@ module RestCore::Facebook::RailsUtil
146
121
  HTML
147
122
  end
148
123
 
149
- module_function
150
-
151
- # ==================== begin options utility =======================
152
- def rc_facebook_oget key
153
- if rc_facebook_options_ctl.has_key?(key)
154
- rc_facebook_options_ctl[key]
155
- else
156
- RestCore::Facebook.send("default_#{key}")
157
- end
158
- end
159
-
160
- def rc_facebook_options_ctl
161
- @rc_facebook_options_ctl ||= {}
162
- end
163
-
164
- def rc_facebook_options_new
165
- @rc_facebook_options_new ||= {}
166
- end
167
- # ==================== end options utility =======================
168
-
169
124
 
170
125
 
171
126
  # ==================== begin facebook check ======================
@@ -235,7 +190,7 @@ module RestCore::Facebook::RailsUtil
235
190
 
236
191
  # ==================== begin check ================================
237
192
  def rc_facebook_storage_key
238
- "rc_facebook_fbs_#{rc_facebook_oget(:app_id)}"
193
+ "rc_facebook_fbs_#{rc_options_get(RestCore::Facebook, :app_id)}"
239
194
  end
240
195
 
241
196
  def rc_facebook_check_rg_fbs
@@ -244,7 +199,9 @@ module RestCore::Facebook::RailsUtil
244
199
  rc_facebook_check_rg_cookies # in canvas, session might not work..
245
200
  end
246
201
 
247
- def rc_facebook_check_rg_handler handler=rc_facebook_oget(:check_handler)
202
+ def rc_facebook_check_rg_handler handler=
203
+ rc_options_get(RestCore::Facebook, :check_handler)
204
+
248
205
  return if rc_facebook.authorized? || !handler
249
206
  rc_facebook.parse_fbs!(handler.call)
250
207
  logger.debug("DEBUG: Facebook: called check_handler, parsed:" \
@@ -252,16 +209,20 @@ module RestCore::Facebook::RailsUtil
252
209
  end
253
210
 
254
211
  def rc_facebook_check_rg_session
255
- return if rc_facebook.authorized? || !rc_facebook_oget(:write_session) ||
212
+ return if rc_facebook.authorized? ||
213
+ !rc_options_get(RestCore::Facebook, :write_session) ||
256
214
  !(fbs = session[rc_facebook_storage_key])
215
+
257
216
  rc_facebook.parse_fbs!(fbs)
258
217
  logger.debug("DEBUG: Facebook: detected rc_facebook session, parsed:" \
259
218
  " #{rc_facebook.data.inspect}")
260
219
  end
261
220
 
262
221
  def rc_facebook_check_rg_cookies
263
- return if rc_facebook.authorized? || !rc_facebook_oget(:write_cookies) ||
222
+ return if rc_facebook.authorized? ||
223
+ !rc_options_get(RestCore::Facebook, :write_cookies) ||
264
224
  !(fbs = cookies[rc_facebook_storage_key])
225
+
265
226
  rc_facebook.parse_fbs!(fbs)
266
227
  logger.debug("DEBUG: Facebook: detected rc_facebook cookies, parsed:" \
267
228
  " #{rc_facebook.data.inspect}")
@@ -282,13 +243,13 @@ module RestCore::Facebook::RailsUtil
282
243
  end
283
244
 
284
245
  def rc_facebook_write_rg_session
285
- return if !rc_facebook_oget(:write_session)
246
+ return if !rc_options_get(RestCore::Facebook, :write_session)
286
247
  session[rc_facebook_storage_key] = fbs = rc_facebook.fbs
287
248
  logger.debug("DEBUG: Facebook: wrote session: fbs => #{fbs}")
288
249
  end
289
250
 
290
251
  def rc_facebook_write_rg_cookies
291
- return if !rc_facebook_oget(:write_cookies)
252
+ return if !rc_options_get(RestCore::Facebook, :write_cookies)
292
253
  cookies[rc_facebook_storage_key] = fbs = rc_facebook.fbs
293
254
  logger.debug("DEBUG: Facebook: wrote cookies: fbs => #{fbs}")
294
255
  end
@@ -300,7 +261,8 @@ module RestCore::Facebook::RailsUtil
300
261
  def rc_facebook_normalized_request_uri
301
262
  uri = if rc_facebook_in_canvas?
302
263
  # rails 3 uses newer rack which has fullpath
303
- "http://apps.facebook.com/#{rc_facebook_oget(:canvas)}" +
264
+ "http://apps.facebook.com/#{
265
+ rc_options_get(RestCore::Facebook, :canvas)}" +
304
266
  (request.respond_to?(:fullpath) ?
305
267
  request.fullpath : request.request_uri)
306
268
  else
@@ -320,13 +282,14 @@ module RestCore::Facebook::RailsUtil
320
282
  end
321
283
 
322
284
  def rc_facebook_in_canvas?
323
- !rc_facebook_oget(:canvas).blank?
285
+ !rc_options_get(RestCore::Facebook, :canvas).blank?
324
286
  end
325
287
 
326
288
  def rc_facebook_auto_authorize?
327
- !rc_facebook_oget(:auto_authorize_scope) .blank? ||
328
- !rc_facebook_oget(:auto_authorize_options).blank? ||
329
- rc_facebook_oget(:auto_authorize)
289
+ client = RestCore::Facebook
290
+ !rc_options_get(client, :auto_authorize_scope) .blank? ||
291
+ !rc_options_get(client, :auto_authorize_options).blank? ||
292
+ rc_options_get(client, :auto_authorize)
330
293
  end
331
294
  # ==================== end misc ================================
332
295
  end
@@ -3,7 +3,7 @@ require 'rest-core'
3
3
 
4
4
  require 'time' # for Time.parse
5
5
 
6
- RestCore::Flurry = RestCore::Builder.client(:api_key, :access_code) do
6
+ RestCore::Flurry = RestCore::Builder.client(:apiKey, :apiAccessCode) do
7
7
  s = self.class # this is only for ruby 1.8!
8
8
  use s::Timeout , 10
9
9
 
@@ -81,8 +81,8 @@ module RestCore::Flurry::Client
81
81
  end
82
82
 
83
83
  def query
84
- {'apiKey' => api_key ,
85
- 'apiAccessCode' => access_code}
84
+ {'apiKey' => apiKey ,
85
+ 'apiAccessCode' => apiAccessCode}
86
86
  end
87
87
 
88
88
  private
@@ -2,73 +2,12 @@
2
2
  require 'rest-core/util/rails_util_util'
3
3
 
4
4
  module RestCore::Flurry::DefaultAttributes
5
- def default_log_method ; Rails.logger.method(:debug); end
6
- def default_cache ; Rails.cache ; end
7
- def default_api_key ; nil ; end
8
- def default_access_code; nil ; end
5
+ def default_log_method; Rails.logger.method(:debug); end
6
+ def default_cache ; Rails.cache ; end
9
7
  end
10
8
 
11
9
  module RestCore::Flurry::RailsUtil
12
- def self.init app=Rails
13
- RestCore::Config.load_for_rails(RestCore::Flurry, 'flurry', app)
14
- end
15
-
16
- module Helper
17
- def rc_flurry
18
- controller.send(:rc_flurry)
19
- end
20
- end
21
-
22
- def self.included controller
23
- # skip if included already, any better way to detect this?
24
- return if controller.respond_to?(:rc_flurry, true)
25
-
26
- controller.helper(RestCore::Flurry::RailsUtil::Helper)
27
- controller.instance_methods.select{ |method|
28
- method.to_s =~ /^rc_flurry/
29
- }.each{ |method| controller.send(:protected, method) }
30
- end
31
-
32
- def rc_flurry_setup options={}
33
- rc_flurry_options_ctl.merge!(
34
- RestCore::RailsUtilUtil.extract_options(
35
- RestCore::Flurry.members, options, :reject))
36
- rc_flurry_options_new.merge!(
37
- RestCore::RailsUtilUtil.extract_options(
38
- RestCore::Flurry.members, options, :select))
39
-
40
- # we'll need to reinitialize rc_flurry with the new options,
41
- # otherwise if you're calling rc_flurry before rc_flurry_setup,
42
- # you'll end up with default options without the ones you've passed
43
- # into rc_flurry_setup.
44
- rc_flurry.send(:initialize, rc_flurry_options_new)
45
-
46
- true # keep going
47
- end
48
-
49
- def rc_flurry
50
- @rc_flurry ||= RestCore::Flurry.new(rc_flurry_options_new)
51
- end
52
-
53
- module_function
54
-
55
- # ==================== begin options utility =======================
56
- def rc_flurry_oget key
57
- if rc_flurry_options_ctl.has_key?(key)
58
- rc_flurry_options_ctl[key]
59
- else
60
- RestCore::Flurry.send("default_#{key}")
61
- end
62
- end
63
-
64
- def rc_flurry_options_ctl
65
- @rc_flurry_options_ctl ||= {}
66
- end
67
-
68
- def rc_flurry_options_new
69
- @rc_flurry_options_new ||= {}
70
- end
71
- # ==================== end options utility =======================
10
+ include RestCore::RailsUtilUtil
72
11
  end
73
12
 
74
13
  RestCore::Flurry::RailsUtil.init(Rails)
@@ -16,3 +16,6 @@ RestCore::Github = RestCore::Builder.client do
16
16
  use s::JsonDecode , true
17
17
  end
18
18
  end
19
+
20
+ require 'rest-core/client/github/rails_util' if
21
+ Object.const_defined?(:Rails)
@@ -0,0 +1,13 @@
1
+
2
+ require 'rest-core/util/rails_util_util'
3
+
4
+ module RestCore::Github::DefaultAttributes
5
+ def default_log_method; Rails.logger.method(:debug); end
6
+ def default_cache ; Rails.cache ; end
7
+ end
8
+
9
+ module RestCore::Github::RailsUtil
10
+ include RestCore::RailsUtilUtil
11
+ end
12
+
13
+ RestCore::Github::RailsUtil.init(Rails)
@@ -57,3 +57,5 @@ end
57
57
 
58
58
  RestCore::Linkedin.send(:include, RestCore::ClientOauth1)
59
59
  RestCore::Linkedin.send(:include, RestCore::Linkedin::Client)
60
+ require 'rest-core/client/linkedin/rails_util' if
61
+ Object.const_defined?(:Rails)
@@ -0,0 +1,13 @@
1
+
2
+ require 'rest-core/util/rails_util_util'
3
+
4
+ module RestCore::Linkedin::DefaultAttributes
5
+ def default_log_method; Rails.logger.method(:debug); end
6
+ def default_cache ; Rails.cache ; end
7
+ end
8
+
9
+ module RestCore::Linkedin::RailsUtil
10
+ include RestCore::RailsUtilUtil
11
+ end
12
+
13
+ RestCore::Linkedin::RailsUtil.init(Rails)
@@ -48,3 +48,5 @@ module RestCore::Mixi::Client
48
48
  end
49
49
 
50
50
  RestCore::Mixi.send(:include, RestCore::Mixi::Client)
51
+ require 'rest-core/client/mixi/rails_util' if
52
+ Object.const_defined?(:Rails)
@@ -0,0 +1,13 @@
1
+
2
+ require 'rest-core/util/rails_util_util'
3
+
4
+ module RestCore::Mixi::DefaultAttributes
5
+ def default_log_method; Rails.logger.method(:debug); end
6
+ def default_cache ; Rails.cache ; end
7
+ end
8
+
9
+ module RestCore::Mixi::RailsUtil
10
+ include RestCore::RailsUtilUtil
11
+ end
12
+
13
+ RestCore::Mixi::RailsUtil.init(Rails)
@@ -99,3 +99,5 @@ end
99
99
 
100
100
  RestCore::Twitter.send(:include, RestCore::ClientOauth1)
101
101
  RestCore::Twitter.send(:include, RestCore::Twitter::Client)
102
+ require 'rest-core/client/twitter/rails_util' if
103
+ Object.const_defined?(:Rails)
@@ -0,0 +1,13 @@
1
+
2
+ require 'rest-core/util/rails_util_util'
3
+
4
+ module RestCore::Twitter::DefaultAttributes
5
+ def default_log_method; Rails.logger.method(:debug); end
6
+ def default_cache ; Rails.cache ; end
7
+ end
8
+
9
+ module RestCore::Twitter::RailsUtil
10
+ include RestCore::RailsUtilUtil
11
+ end
12
+
13
+ RestCore::Twitter::RailsUtil.init(Rails)
@@ -8,11 +8,118 @@ module RestCore::RailsUtilUtil
8
8
  options={}; write(key, value, options); end
9
9
  end
10
10
 
11
- module_function
12
- def extract_options members, options, method
13
- # Hash[] is for ruby 1.8.7
14
- # map(&:to_sym) is for ruby 1.8.7
15
- Hash[options.send(method){ |(k, v)| members.map(&:to_sym).member?(k) }]
11
+ module InstanceMethod
12
+ module_function # to mark below private in controllers
13
+ def rc_setup client, options={}
14
+ rc_options_ctl(client).merge!(
15
+ rc_options_extract(client.members, options, :reject))
16
+
17
+ rc_options_new(client).merge!(
18
+ rc_options_extract(client.members, options, :select))
19
+ end
20
+
21
+ def rc_options_get client, key
22
+ if rc_options_ctl(client).has_key?(key)
23
+ rc_options_ctl(client)[key]
24
+ else
25
+ client.send("default_#{key}")
26
+ end
27
+ end
28
+
29
+ def rc_options_ctl client
30
+ @rc_options_ctl ||= {}
31
+ @rc_options_ctl[client.name] ||= {}
32
+ end
33
+
34
+ def rc_options_new client
35
+ @rc_options_new ||= {}
36
+ @rc_options_new[client.name] ||= {}
37
+ end
38
+
39
+ def rc_options_extract members, options, method
40
+ # Hash[] is for ruby 1.8.7
41
+ # map(&:to_sym) is for ruby 1.8.7
42
+ Hash[options.send(method){ |(k, v)| members.map(&:to_sym).member?(k) }]
43
+ end
44
+ end
45
+
46
+ def self.included rails_util, name=rails_util.name[/(\w+)::\w+$/, 1]
47
+ extend_rails_util(rails_util, name)
48
+ include_rails_util(rails_util, name)
49
+ setup_helper(rails_util, name)
50
+ end
51
+
52
+ def self.extend_rails_util rails_util, name
53
+ meth = name.downcase
54
+ mod = if rails_util.const_defined?(:ClassMethod)
55
+ rails_util.const_get(:ClassMethod)
56
+ else
57
+ Module.new
58
+ end
59
+ mod.module_eval(<<-RUBY, __FILE__, __LINE__)
60
+ def init app=Rails
61
+ RestCore::Config.load_for_rails(RestCore::#{name}, '#{meth}', app)
62
+ end
63
+
64
+ def included controller
65
+ # skip if included already, any better way to detect this?
66
+ return if controller.private_instance_methods.include?(:rc_#{meth})
67
+
68
+ controller.send(:include, RestCore::RailsUtilUtil::InstanceMethod)
69
+
70
+ controller.helper(RestCore::#{name}::RailsUtil::Helper)
71
+ controller.instance_methods.select{ |method|
72
+ method.to_s =~ /^rc_#{meth}/
73
+ }.each{ |method| controller.send(:private, method) }
74
+ end
75
+ RUBY
76
+ rails_util.send(:extend, mod)
77
+ rails_util.const_set(:ClassMethod, mod)
78
+ end
79
+
80
+ def self.include_rails_util rails_util, name
81
+ meth = name.downcase
82
+ mod = if rails_util.const_defined?(:InstanceMethod)
83
+ rails_util.const_get(:InstanceMethod)
84
+ else
85
+ Module.new
86
+ end
87
+ mod.module_eval(<<-RUBY, __FILE__, __LINE__)
88
+ def rc_#{meth}
89
+ client = RestCore::#{name}
90
+ @rc_#{meth} ||= client.new(rc_options_new(client))
91
+ end
92
+
93
+ def rc_#{meth}_setup options={}
94
+ client = RestCore::#{name}
95
+ rc_setup(client, options)
96
+
97
+ # we'll need to reinitialize rc_#{meth} with the new options,
98
+ # otherwise if you're calling rc_#{meth} before rc_#{meth}_setup,
99
+ # you'll end up with default options without the ones you've passed
100
+ # into rc_#{meth}_setup.
101
+ rc_#{meth}.send(:initialize, rc_options_new(client))
102
+
103
+ true # keep going
104
+ end
105
+ RUBY
106
+ rails_util.send(:include, mod)
107
+ rails_util.const_set(:InstanceMethod, mod)
108
+ end
109
+
110
+ def self.setup_helper rails_util, name
111
+ meth = name.downcase
112
+ mod = if rails_util.const_defined?(:Helper)
113
+ rails_util.const_get(:Helper)
114
+ else
115
+ Module.new
116
+ end
117
+ mod.module_eval(<<-RUBY, __FILE__, __LINE__)
118
+ def rc_#{meth}
119
+ controller.send(:rc_#{meth})
120
+ end
121
+ RUBY
122
+ rails_util.const_set(:Helper, mod)
16
123
  end
17
124
  end
18
125