adamwiggins-rest-client 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/lib/restclient.rb +7 -1
- data/lib/restclient/exceptions.rb +4 -0
- data/lib/restclient/request.rb +4 -2
- data/spec/exceptions_spec.rb +13 -2
- data/spec/request_spec.rb +18 -4
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -141,7 +141,7 @@ Patches contributed by: Chris Anderson, Greg Borenstein, Ardekantur, Pedro
|
|
141
141
|
Belo, Rafael Souza, Rick Olson, Aman Gupta, Blake Mizerany, Brian Donovan, Ivan
|
142
142
|
Makfinsky, Marc-André Cournoyer, Coda Hale, Tetsuo Watanabe, Dusty Doris,
|
143
143
|
Lennon Day-Reynolds, James Edward Gray II, Cyril Rohr, Juan Alvarez, and Adam
|
144
|
-
Jacob, and
|
144
|
+
Jacob, Paul Dlug, and Brad Ediger
|
145
145
|
|
146
146
|
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
|
147
147
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.3
|
data/lib/restclient.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
require 'uri'
|
2
|
-
require 'net/https'
|
3
2
|
require 'zlib'
|
4
3
|
require 'stringio'
|
5
4
|
|
5
|
+
begin
|
6
|
+
require 'net/https'
|
7
|
+
rescue LoadError => e
|
8
|
+
raise e unless RUBY_PLATFORM =~ /linux/
|
9
|
+
raise LoadError, "no such file to load -- net/https. Try running apt-get install libopenssl-ruby"
|
10
|
+
end
|
11
|
+
|
6
12
|
require File.dirname(__FILE__) + '/restclient/request'
|
7
13
|
require File.dirname(__FILE__) + '/restclient/mixin/response'
|
8
14
|
require File.dirname(__FILE__) + '/restclient/response'
|
@@ -18,6 +18,10 @@ module RestClient
|
|
18
18
|
def http_code
|
19
19
|
@response.code.to_i if @response
|
20
20
|
end
|
21
|
+
|
22
|
+
def http_body
|
23
|
+
RestClient::Request.decode(@response['content-encoding'], @response.body) if @response
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
# A redirect was encountered; caught by execute to retry with the new url.
|
data/lib/restclient/request.rb
CHANGED
@@ -39,6 +39,8 @@ module RestClient
|
|
39
39
|
execute_inner
|
40
40
|
rescue Redirect => e
|
41
41
|
@url = e.url
|
42
|
+
@method = :get
|
43
|
+
@payload = nil
|
42
44
|
execute
|
43
45
|
end
|
44
46
|
|
@@ -171,7 +173,7 @@ module RestClient
|
|
171
173
|
if res.code =~ /\A2\d{2}\z/
|
172
174
|
# We don't decode raw requests
|
173
175
|
unless @raw_response
|
174
|
-
decode res['content-encoding'], res.body if res.body
|
176
|
+
self.class.decode res['content-encoding'], res.body if res.body
|
175
177
|
end
|
176
178
|
elsif %w(301 302 303).include? res.code
|
177
179
|
url = res.header['Location']
|
@@ -194,7 +196,7 @@ module RestClient
|
|
194
196
|
end
|
195
197
|
end
|
196
198
|
|
197
|
-
def decode(content_encoding, body)
|
199
|
+
def self.decode(content_encoding, body)
|
198
200
|
if content_encoding == 'gzip' and not body.empty?
|
199
201
|
Zlib::GzipReader.new(StringIO.new(body)).read
|
200
202
|
elsif content_encoding == 'deflate'
|
data/spec/exceptions_spec.rb
CHANGED
@@ -12,6 +12,10 @@ describe RestClient::Exception do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
describe RestClient::RequestFailed do
|
15
|
+
before do
|
16
|
+
@response = mock('HTTP Response', :code => '502')
|
17
|
+
end
|
18
|
+
|
15
19
|
it "stores the http response on the exception" do
|
16
20
|
begin
|
17
21
|
raise RestClient::RequestFailed, :response
|
@@ -21,11 +25,18 @@ describe RestClient::RequestFailed do
|
|
21
25
|
end
|
22
26
|
|
23
27
|
it "http_code convenience method for fetching the code as an integer" do
|
24
|
-
RestClient::RequestFailed.new(
|
28
|
+
RestClient::RequestFailed.new(@response).http_code.should == 502
|
29
|
+
end
|
30
|
+
|
31
|
+
it "http_body convenience method for fetching the body (decoding when necessary)" do
|
32
|
+
@response.stub!(:[]).with('content-encoding').and_return('gzip')
|
33
|
+
@response.stub!(:body).and_return('compressed body')
|
34
|
+
RestClient::Request.should_receive(:decode).with('gzip', 'compressed body').and_return('plain body')
|
35
|
+
RestClient::RequestFailed.new(@response).http_body.should == 'plain body'
|
25
36
|
end
|
26
37
|
|
27
38
|
it "shows the status code in the message" do
|
28
|
-
RestClient::RequestFailed.new(
|
39
|
+
RestClient::RequestFailed.new(@response).to_s.should match(/502/)
|
29
40
|
end
|
30
41
|
end
|
31
42
|
|
data/spec/request_spec.rb
CHANGED
@@ -22,19 +22,19 @@ describe RestClient::Request do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "decodes an uncompressed result body by passing it straight through" do
|
25
|
-
|
25
|
+
RestClient::Request.decode(nil, 'xyz').should == 'xyz'
|
26
26
|
end
|
27
27
|
|
28
28
|
it "decodes a gzip body" do
|
29
|
-
|
29
|
+
RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
|
30
30
|
end
|
31
31
|
|
32
32
|
it "ingores gzip for empty bodies" do
|
33
|
-
|
33
|
+
RestClient::Request.decode('gzip', '').should be_empty
|
34
34
|
end
|
35
35
|
|
36
36
|
it "decodes a deflated body" do
|
37
|
-
|
37
|
+
RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
|
38
38
|
end
|
39
39
|
|
40
40
|
it "processes a successful result" do
|
@@ -222,6 +222,20 @@ describe RestClient::Request do
|
|
222
222
|
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
|
223
223
|
end
|
224
224
|
|
225
|
+
it "uses GET and clears payload when following 30x redirects" do
|
226
|
+
url = "http://example.com/redirected"
|
227
|
+
|
228
|
+
@request.should_receive(:execute_inner).once.ordered.and_raise(RestClient::Redirect.new(url))
|
229
|
+
|
230
|
+
@request.should_receive(:execute_inner).once.ordered do
|
231
|
+
@request.url.should == url
|
232
|
+
@request.method.should == :get
|
233
|
+
@request.payload.should be_nil
|
234
|
+
end
|
235
|
+
|
236
|
+
@request.execute
|
237
|
+
end
|
238
|
+
|
225
239
|
it "raises Unauthorized when the response is 401" do
|
226
240
|
res = mock('response', :code => '401')
|
227
241
|
lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adamwiggins-rest-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Wiggins
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-29 00:00:00 -07:00
|
13
13
|
default_executable: restclient
|
14
14
|
dependencies: []
|
15
15
|
|