rest-core 0.4.0.pre.1 → 0.4.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  script: 'git submodule update --init; bundle exec rake test:travis'
2
2
 
3
3
  env:
4
- - 'RESTCORE=rest-graph'
4
+ - 'RESTCORE=rest-core'
5
5
  - 'RESTCORE=rails2'
6
6
  - 'RESTCORE=rails3'
7
7
 
data/CHANGES.md CHANGED
@@ -10,6 +10,16 @@
10
10
  * [client] Now `request` method takes an env and an app to make requests,
11
11
  instead of a weird requests array.
12
12
 
13
+ * [client] Now if you really want to disable something, for example,
14
+ disabling cache when the default cache is `Rails.cache`, you'll need to
15
+ pass `false` instead of `nil`. This is because `nil` stands for using
16
+ defaults in rest-core.
17
+
18
+ * [client] Defaults priorities are changed to:
19
+ per-request > instance variable > class defaults > middleware defaults
20
+ See *test_client.rb* for more detailed definition. If you don't understand
21
+ this, don't worry, since then this won't affect you.
22
+
13
23
  ### Compatible changes:
14
24
 
15
25
  * [client] Introduced a new method `request_full` which is exactly the same
@@ -36,6 +46,7 @@
36
46
  * [wrapped] Fixed a bug that force middlewares to implement `members` method,
37
47
  which should be optional. Thanks miaout17
38
48
 
49
+ * [facebook][rails_util] Now default cache is `Rails.cache` instead of nil
39
50
  * [simple] Added a Simple client, which only wraps RestClient
40
51
  * [univeral] Added an Universal client, which could be used for anything
41
52
  * [flurry] Added a Flurry client, along with its `Flurry::RailsUtil`
@@ -46,6 +57,7 @@
46
57
  in header instead of in query string.
47
58
  * [common_logger] nil object would no longer be logged
48
59
  * [json_decode] Do nothing if we are being asked for env (dry mode)
60
+ * [cache] Now default `:expires_in` is 600 down from 3600
49
61
  * [middleware] Now not only query values would be escaped, but also keys.
50
62
 
51
63
  * [rib-rest-core] Introduced an interactive shell. You'll need [rib][] to
data/TODO.md CHANGED
@@ -3,7 +3,6 @@
3
3
  ## high
4
4
 
5
5
  * middleware revisit (how to initialize?)
6
- * what does false and nil mean in env?
7
6
 
8
7
  ## medium
9
8
 
@@ -50,10 +50,8 @@ class ApplicationController < ActionController::Base
50
50
  end
51
51
 
52
52
  def reinitialize
53
- cache_nil = rc_facebook.cache
54
53
  rc_facebook_setup(:cache => {'a' => 'b'})
55
- cache = rc_facebook.cache
56
- render :text => YAML.dump([cache_nil, cache])
54
+ render :text => YAML.dump(rc_facebook.cache)
57
55
  end
58
56
 
59
57
  def helper; end
@@ -62,6 +60,12 @@ class ApplicationController < ActionController::Base
62
60
  render :text => Timeout::Error.name
63
61
  end
64
62
 
63
+ def defaults
64
+ rc_facebook_setup
65
+ render :text => (rc_facebook.cache == Rails.cache &&
66
+ rc_facebook.log_method.receiver == Rails.logger)
67
+ end
68
+
65
69
  private
66
70
  def filter_common
67
71
  rc_facebook_setup(:auto_authorize => true, :canvas => '')
@@ -172,7 +172,7 @@ class ApplicationControllerTest < ActionController::TestCase
172
172
  def test_reinitailize
173
173
  get(:reinitialize)
174
174
  assert_response :success
175
- assert_equal [nil, {'a' => 'b'}], YAML.load(@response.body)
175
+ assert_equal({'a' => 'b'}, YAML.load(@response.body))
176
176
  end
177
177
 
178
178
  def test_helper
@@ -188,4 +188,10 @@ class ApplicationControllerTest < ActionController::TestCase
188
188
  assert_response :success
189
189
  assert_equal 'Timeout::Error', @response.body.strip
190
190
  end
191
+
192
+ def test_defaults
193
+ get(:defaults)
194
+ assert_response :success
195
+ assert_equal 'true', @response.body.strip
196
+ end
191
197
  end
@@ -50,10 +50,8 @@ class ApplicationController < ActionController::Base
50
50
  end
51
51
 
52
52
  def reinitialize
53
- cache_nil = rc_facebook.cache
54
53
  rc_facebook_setup(:cache => {'a' => 'b'})
55
- cache = rc_facebook.cache
56
- render :text => YAML.dump([cache_nil, cache])
54
+ render :text => YAML.dump(rc_facebook.cache)
57
55
  end
58
56
 
59
57
  def helper; end
@@ -62,6 +60,12 @@ class ApplicationController < ActionController::Base
62
60
  render :text => Timeout::Error.name
63
61
  end
64
62
 
63
+ def defaults
64
+ rc_facebook_setup
65
+ render :text => (rc_facebook.cache == Rails.cache &&
66
+ rc_facebook.log_method.receiver == Rails.logger)
67
+ end
68
+
65
69
  private
66
70
  def filter_common
67
71
  rc_facebook_setup(:auto_authorize => true, :canvas => '')
@@ -172,7 +172,7 @@ class ApplicationControllerTest < ActionController::TestCase
172
172
  def test_reinitailize
173
173
  get(:reinitialize)
174
174
  assert_response :success
175
- assert_equal [nil, {'a' => 'b'}], YAML.load(@response.body)
175
+ assert_equal({'a' => 'b'}, YAML.load(@response.body))
176
176
  end
177
177
 
178
178
  def test_helper
@@ -188,4 +188,10 @@ class ApplicationControllerTest < ActionController::TestCase
188
188
  assert_response :success
189
189
  assert_equal 'Timeout::Error', @response.body.strip
190
190
  end
191
+
192
+ def test_defaults
193
+ get(:defaults)
194
+ assert_response :success
195
+ assert_equal 'true', @response.body.strip
196
+ end
191
197
  end
@@ -5,16 +5,18 @@ require 'cgi'
5
5
  require 'uri'
6
6
 
7
7
  module RestCore::Facebook::DefaultAttributes
8
- def default_canvas ; '' ; end
9
- def default_iframe ; false; end
10
- def default_auto_authorize ; false; end
11
- def default_auto_authorize_options; {} ; end
12
- def default_auto_authorize_scope ; '' ; end
13
- def default_ensure_authorized ; false; end
14
- def default_write_session ; false; end
15
- def default_write_cookies ; false; end
16
- def default_write_handler ; nil; end
17
- def default_check_handler ; nil; end
8
+ def default_log_method ; Rails.logger.method(:debug); end
9
+ def default_cache ; Rails.cache ; end
10
+ def default_canvas ; '' ; end
11
+ def default_iframe ; false ; end
12
+ def default_auto_authorize ; false ; end
13
+ def default_auto_authorize_options; {} ; end
14
+ def default_auto_authorize_scope ; '' ; end
15
+ def default_ensure_authorized ; false ; end
16
+ def default_write_session ; false ; end
17
+ def default_write_cookies ; false ; end
18
+ def default_write_handler ; nil ; end
19
+ def default_check_handler ; nil ; end
18
20
  end
19
21
 
20
22
  module RestCore::Facebook::RailsUtil
@@ -160,7 +162,7 @@ module RestCore::Facebook::RailsUtil
160
162
  end
161
163
 
162
164
  def rc_facebook_options_new
163
- @rc_facebook_options_new ||= {:log_method => logger.method(:debug)}
165
+ @rc_facebook_options_new ||= {}
164
166
  end
165
167
  # ==================== end options utility =======================
166
168
 
@@ -14,7 +14,7 @@ RestCore::Facebook = RestCore::Builder.client(
14
14
  use s::Oauth2Query , nil
15
15
 
16
16
  use s::CommonLogger , nil
17
- use s::Cache , nil, 3600 do
17
+ use s::Cache , nil, 600 do
18
18
  use s::ErrorHandler, lambda{ |env|
19
19
  raise ::RestCore::Facebook::Error.call(env) }
20
20
  use s::ErrorDetector, lambda{ |env|
@@ -2,8 +2,10 @@
2
2
  require 'rest-core/util/rails_util_util'
3
3
 
4
4
  module RestCore::Flurry::DefaultAttributes
5
- def default_api_key ; nil; end
6
- def default_access_code; nil; end
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
7
9
  end
8
10
 
9
11
  module RestCore::Flurry::RailsUtil
@@ -64,7 +66,7 @@ module RestCore::Flurry::RailsUtil
64
66
  end
65
67
 
66
68
  def rc_flurry_options_new
67
- @rc_flurry_options_new ||= {:log_method => logger.method(:debug)}
69
+ @rc_flurry_options_new ||= {}
68
70
  end
69
71
  # ==================== end options utility =======================
70
72
  end
@@ -5,19 +5,18 @@ require 'time' # for Time.parse
5
5
 
6
6
  RestCore::Flurry = RestCore::Builder.client(:api_key, :access_code) do
7
7
  s = self.class # this is only for ruby 1.8!
8
+ use s::Timeout , 10
8
9
 
9
10
  use s::DefaultSite , 'http://api.flurry.com/'
10
11
  use s::DefaultHeaders, {'Accept' => 'application/json'}
11
12
  use s::DefaultQuery , {}
12
13
 
13
14
  use s::CommonLogger , nil
14
- use s::Cache , {}, 3600 do
15
+ use s::Cache , nil, 600 do
15
16
  use s::ErrorHandler, lambda{|env| raise env[s::RESPONSE_BODY]['message']}
16
17
  use s::ErrorDetectorHttp
17
18
  use s::JsonDecode , true
18
19
  end
19
-
20
- run s::RestClient
21
20
  end
22
21
 
23
22
  module RestCore::Flurry::Client
@@ -68,9 +67,9 @@ module RestCore::Flurry::Client
68
67
  # >> f.weekly(f.metrics('ActiveUsers', {}, :weeks => 4))
69
68
  # => [244548, 270227, 248513, 257149]
70
69
  def weekly array
71
- start = Time.parse(array.first.first).to_i
70
+ start = Time.parse(array.first.first, nil).to_i
72
71
  array.group_by{ |(date, value)|
73
- current = Time.parse(date).to_i
72
+ current = Time.parse(date, nil).to_i
74
73
  - (current - start) / (86400*7)
75
74
  # calling .last to discard week numbers created by group_by
76
75
  }.sort.map(&:last).map{ |week|
@@ -81,7 +80,7 @@ module RestCore::Flurry::Client
81
80
  # => [1020437, 775889, 505662, 257149]
82
81
  def sum array
83
82
  reverse = array.reverse
84
- reverse.map.with_index{ |_, index|
83
+ (0...reverse.size).map{ |index|
85
84
  reverse[1, index].inject(reverse.first, &:+)
86
85
  }.reverse
87
86
  end
@@ -10,7 +10,7 @@ RestCore::Github = RestCore::Builder.client do
10
10
  use s::Oauth2Query , 'access_token', nil
11
11
 
12
12
  use s::CommonLogger , nil
13
- use s::Cache , nil, 3600 do
13
+ use s::Cache , nil, 600 do
14
14
  use s::ErrorHandler, lambda{|env| raise env[s::RESPONSE_BODY]['message']}
15
15
  use s::ErrorDetectorHttp
16
16
  use s::JsonDecode , true
@@ -14,7 +14,7 @@ RestCore::Linkedin = RestCore::Builder.client(:data) do
14
14
  'https://www.linkedin.com/uas/oauth/authorize'
15
15
 
16
16
  use s::CommonLogger , nil
17
- use s::Cache , nil, 3600 do
17
+ use s::Cache , nil, 600 do
18
18
  use s::ErrorHandler, lambda{|env|
19
19
  if (body = env[s::RESPONSE_BODY]).kind_of?(Hash)
20
20
  raise body['message']
@@ -10,7 +10,7 @@ RestCore::Mixi = RestCore::Builder.client(
10
10
  use s::Oauth2Header , nil
11
11
 
12
12
  use s::CommonLogger , nil
13
- use s::Cache , nil, 3600 do
13
+ use s::Cache , nil, 600 do
14
14
  use s::ErrorHandler , lambda{ |env| p env }
15
15
  use s::ErrorDetectorHttp
16
16
  use s::JsonDecode , true
@@ -12,7 +12,7 @@ RestCore::Twitter = RestCore::Builder.client(:data) do
12
12
  'oauth/request_token', 'oauth/access_token', 'oauth/authorize'
13
13
 
14
14
  use s::CommonLogger , nil
15
- use s::Cache , nil, 3600 do
15
+ use s::Cache , nil, 600 do
16
16
  use s::ErrorHandler, lambda{ |env|
17
17
  raise ::RestCore::Twitter::Error.call(env) }
18
18
  use s::ErrorDetectorHttp
@@ -8,7 +8,7 @@ RestCore::Universal = RestCore::Builder.client(:data) do
8
8
  use s::DefaultQuery , {}
9
9
 
10
10
  use s::CommonLogger , method(:puts)
11
- use s::Cache , {}, 3600 do
11
+ use s::Cache , {}, 600 do
12
12
  use s::ErrorHandler, nil
13
13
  use s::ErrorDetectorHttp
14
14
  use s::JsonDecode , false
@@ -19,15 +19,15 @@ module RestCore::Client
19
19
  end
20
20
 
21
21
  def default_#{name} app=app
22
- if app.respond_to?(:#{name}) # instance value
23
- app.#{name}({})
24
- elsif app.respond_to?(:wrapped) # wrapper value
25
- default_#{name}(app.wrapped) || # walk into it
26
- default_#{name}(app.app)
27
- elsif app.respond_to?(:app) # walk into next app
28
- default_#{name}(app.app)
29
- elsif self.class.respond_to?("default_#{name}")
30
- self.class.default_#{name} # old class default style
22
+ if self.class.respond_to?("default_#{name}")
23
+ self.class.default_#{name} # old class default style
24
+ elsif app.respond_to?(:#{name})
25
+ app.#{name}({}) # middleware instance value
26
+ elsif app.respond_to?(:wrapped)
27
+ default_#{name}(app.wrapped) || # wrapper value
28
+ default_#{name}(app.app) # walk into it
29
+ elsif app.respond_to?(:app)
30
+ default_#{name}(app.app) # walk into next app
31
31
  else
32
32
  nil
33
33
  end
@@ -160,15 +160,15 @@ module RestCore::Client
160
160
  response
161
161
  end
162
162
  end
163
- # ------------------------ instance ---------------------
164
-
165
163
 
166
-
167
- protected
168
164
  def build_env env={}
169
165
  string_keys(attributes).merge(string_keys(env))
170
166
  end
167
+ # ------------------------ instance ---------------------
168
+
171
169
 
170
+
171
+ protected
172
172
  def string_keys hash
173
173
  hash.inject({}){ |r, (k, v)|
174
174
  if v.kind_of?(Hash)
@@ -1,4 +1,4 @@
1
1
 
2
2
  module RestCore
3
- VERSION = '0.4.0.pre.1'
3
+ VERSION = '0.4.0.pre.2'
4
4
  end
data/rest-core.gemspec CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "rest-core"
5
- s.version = "0.4.0.pre.1"
5
+ s.version = "0.4.0.pre.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [
9
9
  "Cardinal Blue",
10
10
  "Lin Jen-Shin (godfat)"]
11
- s.date = "2011-09-19"
11
+ s.date = "2011-09-21"
12
12
  s.description = "A modular Ruby REST client collection/infrastructure\n\nIn this era of web services and mashups, we have seen a blooming of REST\nAPIs. One might wonder, how do we use these APIs easily and elegantly?\nSince REST is very simple compared to SOAP, it is not hard to build a\ndedicated client ourselves.\n\nWe have developed [rest-core][] with composable middlewares to build a\nREST client, based on the effort from [rest-graph][]. In the cases of\ncommon APIs such as Facebook, Github, and Twitter, developers can simply\nuse the built-in dedicated clients provided by rest-core, or do it yourself\nfor any other REST APIs.\n\n[rest-core]: http://github.com/cardinalblue/rest-core\n[rest-graph]: http://github.com/cardinalblue/rest-graph"
13
13
  s.email = ["dev (XD) cardinalblue.com"]
14
14
  s.executables = ["rib-rest-core"]
@@ -13,7 +13,7 @@ describe RestCore::Flurry do
13
13
  startDate = '2011-08-23'
14
14
  endDate = '2011-09-19'
15
15
  @flurry = RestCore::Flurry.new
16
- stub(@flurry).Time.stub!.now{ Time.parse(endDate) }
16
+ stub(Time).now{ Time.parse(endDate, nil) }
17
17
  stub_request(:get,
18
18
  "http://api.flurry.com/appMetrics/ActiveUsers?" \
19
19
  "startDate=#{startDate}&endDate=#{endDate}").
data/test/test_client.rb CHANGED
@@ -11,4 +11,36 @@ describe RestCore::Simple do
11
11
  stub_request(:get, 'http://localhost/').to_return(:body => '[]')
12
12
  RestCore::Simple.new.get('http://localhost/').should.eq '[]'
13
13
  end
14
+
15
+ should 'use defaults' do
16
+ client = RestCore::Builder.client do
17
+ s = self.class # this is only for ruby 1.8!
18
+ use s::Timeout, 4
19
+ end
20
+ c = client.new
21
+ c.timeout.should.eq 4 # default goes to middleware
22
+ client.extend(Module.new do
23
+ def default_timeout
24
+ 3
25
+ end
26
+ end)
27
+ c.timeout.should.eq 4 # default is cached, so it stays the same
28
+ c.timeout = nil # clear cache
29
+ c.timeout.should.eq 3 # now default goes to module default
30
+ class << client
31
+ def default_timeout # module defaults could be overriden
32
+ super - 1
33
+ end
34
+ end
35
+ c.timeout = nil
36
+ c.timeout.should.eq 2 # so it goes to class default
37
+ c.timeout = 1 # setup instance level value
38
+ c.build_env( )['timeout'].should.eq 1 # pick instance var
39
+ c.build_env({'timeout' => 0})['timeout'].should.eq 0 # per-request var
40
+ c.timeout.should.eq 1 # won't affect underlying instance var
41
+ c.timeout = nil
42
+ c.timeout.should.eq 2 # goes back to class default
43
+ c.timeout = false
44
+ c.timeout.should.eq false # false would disable default
45
+ end
14
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.pre.1
4
+ version: 0.4.0.pre.2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-09-19 00:00:00.000000000Z
13
+ date: 2011-09-21 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
17
- requirement: &2168955760 !ruby/object:Gem::Requirement
17
+ requirement: &2152743040 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2168955760
25
+ version_requirements: *2152743040
26
26
  description: ! 'A modular Ruby REST client collection/infrastructure
27
27
 
28
28