rest-client-components 0.2.1 → 0.2.2
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/README.rdoc +5 -0
- data/VERSION +1 -1
- data/examples/parsing.rb +3 -1
- data/examples/twitter_caching.rb +21 -0
- data/lib/restclient/components.rb +19 -8
- data/rest-client-components.gemspec +3 -2
- data/spec/components_spec.rb +41 -1
- metadata +2 -1
data/README.rdoc
CHANGED
@@ -6,16 +6,19 @@ Want to add transparent HTTP caching to the rest-client[http://github.com/archil
|
|
6
6
|
require 'restclient/components'
|
7
7
|
require 'rack/cache'
|
8
8
|
RestClient.enable Rack::Cache
|
9
|
+
RestClient.get "http://some/cacheable/resource"
|
9
10
|
|
10
11
|
Want to log the requests in the commonlog format ?
|
11
12
|
require 'restclient/components'
|
12
13
|
RestClient.enable Rack::CommonLogger, STDOUT
|
14
|
+
RestClient.get "http://some/resource"
|
13
15
|
|
14
16
|
Want to enable both ?
|
15
17
|
require 'restclient/components'
|
16
18
|
require 'rack/cache'
|
17
19
|
RestClient.enable Rack::CommonLogger, STDOUT
|
18
20
|
RestClient.enable Rack::Cache
|
21
|
+
RestClient.get "http://some/cacheable/resource"
|
19
22
|
|
20
23
|
This works with any Rack middleware, thus you can reuse the wide range of existing Rack middleware to add functionalities to RestClient with very little effort.
|
21
24
|
The order in which you enable components will be respected.
|
@@ -26,6 +29,8 @@ Note that the rest-client behaviour is also respected: you'll get back a RestCli
|
|
26
29
|
status, header, body = RestClient.get('http://some/url')
|
27
30
|
In that case, you will only get exceptions for connection or timeout errors (RestClient::ServerBrokeConnection or RestClient::RequestTimeout)
|
28
31
|
|
32
|
+
See the examples folder for more details.
|
33
|
+
|
29
34
|
= Installation
|
30
35
|
|
31
36
|
gem install rest-client-components
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/examples/parsing.rb
CHANGED
@@ -10,7 +10,9 @@ module Rack
|
|
10
10
|
|
11
11
|
def call(env)
|
12
12
|
status, header, body = @app.call env
|
13
|
-
|
13
|
+
content = ""
|
14
|
+
body.each{|line| content << line}
|
15
|
+
parsed_body = ::JSON.parse content if header['Content-Type'] =~ /^application\/.*json/i
|
14
16
|
[status, header, parsed_body]
|
15
17
|
end
|
16
18
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/restclient/components'
|
2
|
+
require 'rack/cache'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
|
6
|
+
RestClient.log = 'stdout'
|
7
|
+
RestClient.enable Rack::Cache, :allow_reload => true, :allow_revalidate => true
|
8
|
+
|
9
|
+
2.times do
|
10
|
+
resp = RestClient::Resource.new("https://localhost:3443/sid/grid5000").get(:accept => "application/json")
|
11
|
+
p ["Cache-Control", resp.headers[:cache_control]]
|
12
|
+
end
|
13
|
+
|
14
|
+
__END__
|
15
|
+
RestClient.get "https://localhost:3443/sid/grid5000", headers: {"Accept-encoding"=>"gzip, deflate", "Accept"=>"application/json"}
|
16
|
+
# => 200 OK | application/vnd.fr.grid5000.api.grid+json 998 bytes
|
17
|
+
cache: [GET /sid/grid5000] miss, store
|
18
|
+
["Cache-Control", "public, must-revalidate"]
|
19
|
+
RestClient.get "https://localhost:3443/sid/grid5000", headers: {"If-none-match"=>"\"ef6d9ea6d7d00299dd59a025fa84c19b4e69fafe\"", "Accept-encoding"=>"gzip, deflate", "Accept"=>"application/json", "If-modified-since"=>"Wed, 09 Dec 2009 13:58:54 GMT"}
|
20
|
+
cache: [GET /sid/grid5000] stale, valid, store
|
21
|
+
["Cache-Control", "public, must-revalidate"]
|
@@ -14,7 +14,9 @@ module RestClient
|
|
14
14
|
raise e
|
15
15
|
else
|
16
16
|
response = RestClient::MockNetHTTPResponse.new(body, status, header)
|
17
|
-
|
17
|
+
content = ""
|
18
|
+
response.body.each{|line| content << line}
|
19
|
+
RestClient::Response.new(content, response)
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
@@ -136,17 +138,26 @@ module RestClient
|
|
136
138
|
|
137
139
|
RACK_APP = Proc.new { |env|
|
138
140
|
begin
|
139
|
-
# get the original request and execute it
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
141
|
+
# get the original request, replace headers with those of env, and execute it
|
142
|
+
request = env['restclient.request']
|
143
|
+
additional_headers = env.keys.select{|k| k=~/^HTTP_/}.inject({}){|accu, k|
|
144
|
+
accu[k.gsub("HTTP_", "")] = env[k]
|
145
|
+
accu
|
146
|
+
}
|
147
|
+
request.processed_headers.replace(request.make_headers(additional_headers))
|
148
|
+
response = request.original_execute
|
144
149
|
rescue RestClient::ExceptionWithResponse => e
|
145
150
|
env['restclient.error'] = e
|
146
151
|
# e is a Net::HTTPResponse
|
147
152
|
response = RestClient::Response.new(e.response.body, e.response)
|
148
|
-
[response.code, RestClient.debeautify_headers( response.headers ), [response.to_s]]
|
149
153
|
end
|
154
|
+
# to satisfy Rack::Lint
|
155
|
+
response.headers.delete(:status)
|
156
|
+
header = RestClient.debeautify_headers( response.headers )
|
157
|
+
body = response.to_s
|
158
|
+
# return the real content-length since RestClient does not do it when decoding gzip responses
|
159
|
+
header['Content-Length'] = body.length.to_s if header.has_key?('Content-Length')
|
160
|
+
[response.code, header, [body]]
|
150
161
|
}
|
151
162
|
|
152
|
-
end
|
163
|
+
end
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rest-client-components}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Cyril Rohr"]
|
@@ -37,7 +37,8 @@ Gem::Specification.new do |s|
|
|
37
37
|
"spec/components_spec.rb",
|
38
38
|
"spec/spec_helper.rb",
|
39
39
|
"examples/caching.rb",
|
40
|
-
"examples/parsing.rb"
|
40
|
+
"examples/parsing.rb",
|
41
|
+
"examples/twitter_caching.rb"
|
41
42
|
]
|
42
43
|
|
43
44
|
if s.respond_to? :specification_version then
|
data/spec/components_spec.rb
CHANGED
@@ -37,7 +37,6 @@ describe "Components for RestClient" do
|
|
37
37
|
|
38
38
|
describe "usage" do
|
39
39
|
before do
|
40
|
-
|
41
40
|
@expected_args = {:url=>"http://domain.tld:8888/some/cacheable/resource?q1=a&q2=b", :method=>:get, :headers=>{:additional_header=>"whatever"}}
|
42
41
|
@expected_request = RestClient::Request.new(@expected_args)
|
43
42
|
end
|
@@ -58,6 +57,47 @@ describe "Components for RestClient" do
|
|
58
57
|
body.each{|part| content << part}
|
59
58
|
content.should == "body"
|
60
59
|
end
|
60
|
+
|
61
|
+
it "should cif the requested resource is not in the cache" do
|
62
|
+
@expected_request.should_receive(:original_execute).and_return(
|
63
|
+
mock('rest-client response',
|
64
|
+
:headers => {:content_type => "text/plain, */*", :date => "Mon, 04 Jan 2010 13:37:18 GMT"},
|
65
|
+
:code => 200,
|
66
|
+
:to_s => 'body'))
|
67
|
+
status, header, body = Rack::Lint.new(Rack::CommonLogger.new(Rack::Cache.new(RestClient::RACK_APP))).call(@env.merge(
|
68
|
+
'restclient.request' => @expected_request
|
69
|
+
))
|
70
|
+
status.should == 200
|
71
|
+
header.should == {"Content-Type"=>"text/plain, */*", "X-Rack-Cache"=>"miss", "Date"=>"Mon, 04 Jan 2010 13:37:18 GMT"}
|
72
|
+
content = ""
|
73
|
+
body.each{|part| content << part}
|
74
|
+
content.should == "body"
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
it "should add HTTP headers set by Rack middleware to the list of request headers" do
|
79
|
+
module Rack
|
80
|
+
class AddRequestHeader
|
81
|
+
def initialize(app)
|
82
|
+
@app = app
|
83
|
+
end
|
84
|
+
def call(env)
|
85
|
+
env['HTTP_X_HEADER'] = "hello"
|
86
|
+
@app.call(env)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
@expected_request.should_receive(:processed_headers).and_return(processed_headers=mock("hash processed headers"))
|
91
|
+
processed_headers.should_receive(:replace).with(hash_including("X-header" => "hello"))
|
92
|
+
@expected_request.should_receive(:original_execute).and_return(
|
93
|
+
mock('rest-client response',
|
94
|
+
:headers => {:content_type => "text/plain, */*", :date => "Mon, 04 Jan 2010 13:37:18 GMT"},
|
95
|
+
:code => 200,
|
96
|
+
:to_s => 'body'))
|
97
|
+
status, header, body = Rack::Lint.new(Rack::AddRequestHeader.new(RestClient::RACK_APP)).call(@env.merge(
|
98
|
+
'restclient.request' => @expected_request
|
99
|
+
))
|
100
|
+
end
|
61
101
|
|
62
102
|
it "should return a 304 not modified response if the call to the backend returned a 304 not modified response" do
|
63
103
|
@expected_request.should_receive(:original_execute).and_raise(RestClient::NotModified.new(@mock_304_net_http_response))
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest-client-components
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Rohr
|
@@ -85,3 +85,4 @@ test_files:
|
|
85
85
|
- spec/spec_helper.rb
|
86
86
|
- examples/caching.rb
|
87
87
|
- examples/parsing.rb
|
88
|
+
- examples/twitter_caching.rb
|