rest-core 1.0.2 → 1.0.3

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.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # CHANGES
2
2
 
3
+ ## rest-core 1.0.3 -- 2012-08-15
4
+
5
+ ### Enhancement
6
+
7
+ * [Client] `client.head` now returns the headers instead of response body.
8
+ It doesn't make sense to return the response body, because there's no
9
+ such things in a HEAD request.
10
+
11
+ ### Bugs fixes
12
+
13
+ * [Cache] The cache object you passed in would only need to respond to
14
+ `[]` and `[]=`. If the cache object accepts an `:expires_in` option,
15
+ then it must also respond to `store`, too.
16
+
17
+ * [Oauth1Header] Fixed a long standing bug that tilde (~) shouldn't be
18
+ escaped. Many thanks to @brucehsu for discovering this!
19
+
3
20
  ## rest-core 1.0.2 -- 2012-06-05
4
21
 
5
22
  ### Enhancement
data/README.md CHANGED
@@ -80,7 +80,7 @@ You can use `RestCore::Builder` to build your own dedicated client:
80
80
  require 'rest-core'
81
81
 
82
82
  YourClient = RestCore::Builder.client do
83
- s = self.class # this is only for ruby 1.8!
83
+ s = RestCore
84
84
  use s::DefaultSite , 'https://api.github.com/users/'
85
85
  use s::JsonDecode , true
86
86
  use s::CommonLogger, method(:puts)
@@ -122,7 +122,7 @@ cool.io. Below is an example for eventmachine:
122
122
  require 'rest-core'
123
123
 
124
124
  AsynchronousClient = RestCore::Builder.client do
125
- s = self.class # this is only for ruby 1.8!
125
+ s = RestCore
126
126
  use s::DefaultSite , 'https://api.github.com/users/'
127
127
  use s::JsonDecode , true
128
128
  use s::CommonLogger, method(:puts)
@@ -318,6 +318,7 @@ To be added.
318
318
  * Mariusz Pruszynski (@snicky)
319
319
  * Mr. Big Cat (@miaout17)
320
320
  * Nicolas Fouché (@nfo)
321
+ * Szu-Kai Hsu (@brucehsu)
321
322
 
322
323
  ## LICENSE:
323
324
 
data/TODO.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # TODO
2
2
 
3
- * Auto should also pick RestClient if it's not inside a fiber
4
3
  * HTTP method in Requested log
5
4
  * middleware revisit (how to initialize?)
6
5
  * test utility
@@ -8,7 +7,6 @@
8
7
  # BUG
9
8
 
10
9
  * inheritance should work; assign builder?
11
- * no error handling in cool.io
12
10
 
13
11
  # FEATURE
14
12
 
data/example/auto.rb CHANGED
@@ -4,7 +4,7 @@ require 'eventmachine'
4
4
  require 'cool.io'
5
5
 
6
6
  YourClient = RestCore::Builder.client do
7
- s = self.class # this is only for ruby 1.8!
7
+ s = RestCore
8
8
  use s::DefaultSite , 'https://api.github.com/users/'
9
9
  use s::JsonDecode , true
10
10
  use s::CommonLogger, method(:puts)
data/example/coolio.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  require 'rest-core'
3
3
 
4
4
  AsynchronousClient = RestCore::Builder.client do
5
- s = self.class # this is only for ruby 1.8!
5
+ s = RestCore
6
6
  use s::DefaultSite , 'https://api.github.com/users/'
7
7
  use s::JsonDecode , true
8
8
  use s::CommonLogger, method(:puts)
@@ -2,7 +2,7 @@
2
2
  require 'rest-core'
3
3
 
4
4
  AsynchronousClient = RestCore::Builder.client do
5
- s = self.class # this is only for ruby 1.8!
5
+ s = RestCore
6
6
  use s::DefaultSite , 'https://api.github.com/users/'
7
7
  use s::JsonDecode , true
8
8
  use s::CommonLogger, method(:puts)
data/example/multi.rb CHANGED
@@ -6,7 +6,7 @@ RestCore::EmHttpRequest # there might be a autoload bug?
6
6
  # stack level too deep (SystemStackError)
7
7
 
8
8
  YourClient = RestCore::Builder.client do
9
- s = self.class # this is only for ruby 1.8!
9
+ s = RestCore
10
10
  use s::DefaultSite , 'https://api.github.com/users/'
11
11
  use s::JsonDecode , true
12
12
  use s::CommonLogger, method(:puts)
@@ -2,7 +2,7 @@
2
2
  require 'rest-core'
3
3
 
4
4
  YourClient = RestCore::Builder.client do
5
- s = self.class # this is only for ruby 1.8!
5
+ s = RestCore
6
6
  use s::DefaultSite , 'https://api.github.com/users/'
7
7
  use s::JsonDecode , true
8
8
  use s::CommonLogger, method(:puts)
@@ -1,7 +1,7 @@
1
1
 
2
2
  require 'rest-core/middleware'
3
3
 
4
- require 'rest-client'
4
+ require 'restclient/payload'
5
5
  require 'em-http-request'
6
6
 
7
7
  class RestCore::EmHttpRequestAsync
@@ -1,7 +1,7 @@
1
1
 
2
2
  require 'rest-core/middleware'
3
3
 
4
- require 'rest-client'
4
+ require 'restclient/payload'
5
5
  require 'em-http-request'
6
6
  require 'fiber'
7
7
 
@@ -1,6 +1,6 @@
1
1
 
2
2
  RestCore::Universal = RestCore::Builder.client do
3
- s = self.class # this is only for ruby 1.8!
3
+ s = RestCore
4
4
  use s::Timeout , 0
5
5
 
6
6
  use s::DefaultSite , nil
@@ -89,21 +89,6 @@ module RestCore::Client
89
89
  DRY => true}.merge(opts))))
90
90
  end
91
91
 
92
- # extra options:
93
- # json_decode: Bool # decode with json or not in this API request
94
- # # default: json_decode in rest-graph instance
95
- # timeout: Int # the timeout for this API request
96
- # # default: timeout in rest-graph instance
97
- # secret: Bool # use secret_acccess_token or not
98
- # # default: false
99
- # cache: Bool # use cache or not; if it's false, update cache, too
100
- # # default: true
101
- # expires_in: Int # control when would the cache be expired
102
- # # default: nil
103
- # async: Bool # use eventmachine for http client or not
104
- # # default: false, but true in aget family
105
- # headers: Hash # additional hash you want to pass
106
- # # default: {}
107
92
  def get path, query={}, opts={}, &cb
108
93
  request(
109
94
  {REQUEST_METHOD => :get ,
@@ -122,7 +107,7 @@ module RestCore::Client
122
107
  request(
123
108
  {REQUEST_METHOD => :head ,
124
109
  REQUEST_PATH => path ,
125
- REQUEST_QUERY => query }.merge(opts), &cb)
110
+ REQUEST_QUERY => query }.merge(opts), RESPONSE_HEADERS, &cb)
126
111
  end
127
112
 
128
113
  def options path, query={}, opts={}, &cb
@@ -156,13 +141,13 @@ module RestCore::Client
156
141
  REQUEST_PAYLOAD => payload}.merge(opts), &cb)
157
142
  end
158
143
 
159
- def request env, app=app
144
+ def request env, key=RESPONSE_BODY, app=app
160
145
  if block_given?
161
146
  request_full(env, app){ |response|
162
- yield(response[RESPONSE_BODY])
147
+ yield(response[key])
163
148
  }
164
149
  else
165
- request_full(env, app)[RESPONSE_BODY]
150
+ request_full(env, app)[key]
166
151
  end
167
152
  end
168
153
 
@@ -195,7 +180,7 @@ module RestCore::Client
195
180
 
196
181
 
197
182
 
198
- protected
183
+ private
199
184
  def string_keys hash
200
185
  hash.inject({}){ |r, (k, v)|
201
186
  if v.kind_of?(Hash)
@@ -75,6 +75,7 @@ class RestCore::Cache
75
75
  value = response[RESPONSE_BODY]
76
76
 
77
77
  if expires_in(env).kind_of?(Fixnum) &&
78
+ cache(env).respond_to?(:store) &&
78
79
  cache(env).method(:store).arity == -3
79
80
  cache(env).store(cache_key(env), value,
80
81
  :expires_in => expires_in(env))
@@ -2,10 +2,12 @@
2
2
  require 'rest-core/middleware'
3
3
  require 'rest-core/util/hmac'
4
4
 
5
- require 'cgi'
5
+ require 'uri'
6
6
  require 'openssl'
7
7
 
8
8
  class RestCore::Oauth1Header
9
+ UNRESERVED = /[^a-zA-Z0-9\-\.\_\~]/
10
+
9
11
  def self.members
10
12
  [:request_token_path, :access_token_path, :authorize_path,
11
13
  :consumer_key, :consumer_secret,
@@ -112,6 +114,6 @@ class RestCore::Oauth1Header
112
114
  end
113
115
 
114
116
  def escape string
115
- CGI.escape(string).gsub('+', '%20')
117
+ URI.escape(string, UNRESERVED)
116
118
  end
117
119
  end
@@ -1,4 +1,4 @@
1
1
 
2
2
  module RestCore
3
- VERSION = '1.0.2'
3
+ VERSION = '1.0.3'
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 = "1.0.2"
5
+ s.version = "1.0.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [
9
9
  "Cardinal Blue",
10
10
  "Lin Jen-Shin (godfat)"]
11
- s.date = "2012-06-05"
11
+ s.date = "2012-08-15"
12
12
  s.description = "Modular Ruby clients interface for REST APIs\n\nThere has been an explosion in the number of REST APIs available today.\nTo address the need for a way to access these APIs easily and elegantly,\nwe have developed [rest-core][], which consists of composable middleware\nthat allows you to build a REST client for any REST API. Or in the case of\ncommon APIs such as Facebook, Github, and Twitter, you can simply use the\ndedicated clients provided by [rest-more][].\n\n[rest-core]: https://github.com/cardinalblue/rest-core\n[rest-more]: https://github.com/cardinalblue/rest-more"
13
13
  s.email = ["dev (XD) cardinalblue.com"]
14
14
  s.files = [
data/test/test_cache.rb CHANGED
@@ -66,4 +66,22 @@ describe RC::Cache do
66
66
  EM.stop }}}
67
67
  c.cache.size.should.eq 1
68
68
  end
69
+
70
+ should 'only [] and []= should be implemented' do
71
+ cache = Class.new do
72
+ def initialize ; @h = {} ; end
73
+ def [] key ; @h[key] ; end
74
+ def []= key, value; @h[key] = value * 2; end
75
+ end.new
76
+ c = RC::Builder.client do
77
+ use RC::Cache, cache, 0
78
+ run Class.new{
79
+ def call env
80
+ env.merge(RC::RESPONSE_BODY => env[RC::REQUEST_PATH])
81
+ end
82
+ }
83
+ end.new
84
+ c.get('4')
85
+ c.get('4').should.eq '44'
86
+ end
69
87
  end
data/test/test_client.rb CHANGED
@@ -8,20 +8,28 @@ describe RC::Simple do
8
8
  end
9
9
 
10
10
  should 'do simple request' do
11
- [:get, :post, :delete, :put,
12
- :head, :patch, :options].each do |method|
13
- stub_request(method, 'http://localhost/').to_return(:body => '[]')
14
- RC::Simple.new.send(method, 'http://localhost/').should.eq '[]'
11
+ url = 'http://localhost/'
12
+ [:get, :post, :delete, :put, :patch, :options].each do |method|
13
+ stub_request(method, url).to_return(:body => '[]')
14
+ RC::Simple.new.send(method, url).should.eq '[]'
15
15
  end
16
+
17
+ stub_request(:head, url).to_return(:headers => {'A' => 'B'})
18
+ RC::Simple.new.head(url).should.eq({'A' => 'B'})
16
19
  end
17
20
 
18
21
  should 'call the callback' do
19
- [:get, :post, :delete, :put,
20
- :head, :patch, :options].each do |method|
21
- stub_request(method, 'http://localhost/').to_return(:body => '123')
22
- (client = RC::Simple.new).send(method, 'http://localhost/'){ |res|
22
+ url = 'http://localhost/'
23
+ [:get, :post, :delete, :put, :patch, :options].each do |method|
24
+ stub_request(method, url).to_return(:body => '123')
25
+ (client = RC::Simple.new).send(method, url){ |res|
23
26
  res.should.eq '123' }.should.eq client
24
27
  end
28
+
29
+ stub_request(:head, url).to_return(:headers => {'A' => 'B'})
30
+ (client = RC::Simple.new).head(url){ |res|
31
+ res.should.eq({'A' => 'B'})
32
+ }.should.eq client
25
33
  end
26
34
 
27
35
  should 'have correct to_i' do
@@ -31,8 +39,7 @@ describe RC::Simple do
31
39
 
32
40
  should 'use defaults' do
33
41
  client = RestCore::Builder.client do
34
- s = self.class # this is only for ruby 1.8!
35
- use s::Timeout, 4
42
+ use RC::Timeout, 4
36
43
  end
37
44
  c = client.new
38
45
  c.timeout.should.eq 4 # default goes to middleware
@@ -8,8 +8,7 @@ describe RC::ClientOauth1 do
8
8
  end
9
9
 
10
10
  client = RC::Builder.client do
11
- s = self.class # this is only for ruby 1.8!
12
- use s::Oauth1Header
11
+ use RC::Oauth1Header
13
12
  end
14
13
 
15
14
  client.send(:include, RC::ClientOauth1)
@@ -4,9 +4,8 @@ require 'rest-core/test'
4
4
  describe RC::ErrorDetector do
5
5
  should 'lighten' do
6
6
  client = RC::Builder.client do
7
- s = self.class # this is only for ruby 1.8!
8
- use s::ErrorDetector
9
- run s::Dry
7
+ use RC::ErrorDetector
8
+ run RC::Dry
10
9
  end.new.lighten
11
10
 
12
11
  client.attributes.should.key?(:error_detector)
@@ -4,9 +4,8 @@ require 'rest-core/test'
4
4
  describe RC::ErrorDetectorHttp do
5
5
  should 'lighten' do
6
6
  client = RC::Builder.client do
7
- s = self.class # this is only for ruby 1.8!
8
- use s::ErrorDetectorHttp
9
- run s::Dry
7
+ use RC::ErrorDetectorHttp
8
+ run RC::Dry
10
9
  end.new.lighten
11
10
 
12
11
  client.attributes.should.key?(:error_detector)
@@ -83,5 +83,11 @@ describe RestCore::Oauth1Header do
83
83
  @env.merge!(RC::REQUEST_PAYLOAD => {'pay' => 'load'})
84
84
  check
85
85
  end
86
+
87
+ should 'not escape ~' do
88
+ @base_string << '%26tilde%3D~'
89
+ @env.merge!(RC::REQUEST_PAYLOAD => {'tilde' => '~'})
90
+ check
91
+ end
86
92
  end
87
93
  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: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-05 00:00:00.000000000 Z
13
+ date: 2012-08-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client