rest 2.0.4 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/rest/version.rb +1 -1
- data/lib/rest/wrappers/net_http_persistent_wrapper.rb +34 -40
- data/lib/rest/wrappers/rest_client_wrapper.rb +7 -1
- data/lib/rest/wrappers/typhoeus_wrapper.rb +0 -2
- data/test/test_base.rb +2 -4
- data/test/test_rest.rb +73 -44
- data/test/tmp.rb +0 -11
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/lib/rest/version.rb
CHANGED
@@ -16,8 +16,8 @@ module Rest
|
|
16
16
|
|
17
17
|
if response.header['content-encoding'].eql?('gzip')
|
18
18
|
Rest.logger.debug 'GZIPPED'
|
19
|
-
sio = StringIO.new(
|
20
|
-
gz = Zlib::GzipReader.new(
|
19
|
+
sio = StringIO.new(response.body)
|
20
|
+
gz = Zlib::GzipReader.new(sio)
|
21
21
|
page = gz.read()
|
22
22
|
@body = page
|
23
23
|
end
|
@@ -127,20 +127,18 @@ module Rest
|
|
127
127
|
|
128
128
|
|
129
129
|
def post(url, req_hash={})
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
rescue Net::HTTPServerError => ex
|
141
|
-
raise NetHttpPersistentExceptionWrapper.new(ex)
|
130
|
+
r = nil
|
131
|
+
uri = URI(url)
|
132
|
+
post = Net::HTTP::Post.new fix_path(uri.request_uri)
|
133
|
+
add_headers(post, req_hash, default_headers)
|
134
|
+
post.body = stringed_body(req_hash[:body]) if req_hash[:body]
|
135
|
+
response = http.request uri, post
|
136
|
+
r = NetHttpPersistentResponseWrapper.new(response)
|
137
|
+
case response
|
138
|
+
when Net::HTTPClientError, Net::HTTPServerError
|
139
|
+
raise Rest::HttpError.new(r)
|
142
140
|
end
|
143
|
-
|
141
|
+
r
|
144
142
|
end
|
145
143
|
|
146
144
|
def stringed_body(body)
|
@@ -152,36 +150,32 @@ module Rest
|
|
152
150
|
end
|
153
151
|
|
154
152
|
def put(url, req_hash={})
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
rescue Net::HTTPServerError => ex
|
166
|
-
raise NetHttpPersistentExceptionWrapper.new(ex)
|
153
|
+
r = nil
|
154
|
+
uri = URI(url)
|
155
|
+
post = Net::HTTP::Put.new fix_path(uri.request_uri)
|
156
|
+
add_headers(post, req_hash, default_headers)
|
157
|
+
post.body = stringed_body(req_hash[:body]) if req_hash[:body]
|
158
|
+
response = http.request uri, post
|
159
|
+
r = NetHttpPersistentResponseWrapper.new(response)
|
160
|
+
case response
|
161
|
+
when Net::HTTPClientError, Net::HTTPServerError
|
162
|
+
raise Rest::HttpError.new(r)
|
167
163
|
end
|
168
|
-
|
164
|
+
r
|
169
165
|
end
|
170
166
|
|
171
167
|
def delete(url, req_hash={})
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
rescue Net::HTTPServerError => ex
|
182
|
-
raise NetHttpPersistentExceptionWrapper.new(ex)
|
168
|
+
r = nil
|
169
|
+
uri = URI(url)
|
170
|
+
post = Net::HTTP::Delete.new fix_path(uri.request_uri)
|
171
|
+
add_headers(post, req_hash, default_headers)
|
172
|
+
response = http.request uri, post
|
173
|
+
r = NetHttpPersistentResponseWrapper.new(response)
|
174
|
+
case response
|
175
|
+
when Net::HTTPClientError, Net::HTTPServerError
|
176
|
+
raise Rest::HttpError.new(r)
|
183
177
|
end
|
184
|
-
|
178
|
+
r
|
185
179
|
end
|
186
180
|
|
187
181
|
def close
|
@@ -62,7 +62,13 @@ module Rest
|
|
62
62
|
begin
|
63
63
|
req_hash[:method] = :post
|
64
64
|
req_hash[:url] = url
|
65
|
-
|
65
|
+
b = req_hash[:body]
|
66
|
+
if b
|
67
|
+
if b.is_a?(Hash)
|
68
|
+
b = b.to_json
|
69
|
+
end
|
70
|
+
req_hash[:payload] = b
|
71
|
+
end
|
66
72
|
r2 = RestClient::Request.execute(req_hash)
|
67
73
|
response = RestClientResponseWrapper.new(r2)
|
68
74
|
rescue RestClient::Exception => ex
|
data/test/test_base.rb
CHANGED
@@ -13,12 +13,10 @@ class TestBase < Test::Unit::TestCase
|
|
13
13
|
def setup
|
14
14
|
puts 'setup'
|
15
15
|
@rest = Rest::Client.new(:gem => :net_http_persistent, :log_level=>Logger::DEBUG)
|
16
|
-
@request_bin = "http://requestb.in/13t6hs51"
|
17
16
|
|
18
17
|
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
19
|
+
ALL_OPS = [:get, :put, :post, :delete]
|
20
|
+
|
23
21
|
|
24
22
|
end
|
data/test/test_rest.rb
CHANGED
@@ -9,7 +9,6 @@ class TestRest < TestBase
|
|
9
9
|
def setup
|
10
10
|
super
|
11
11
|
|
12
|
-
|
13
12
|
end
|
14
13
|
|
15
14
|
def test_basics
|
@@ -47,59 +46,72 @@ class TestRest < TestBase
|
|
47
46
|
end
|
48
47
|
|
49
48
|
def test_gets
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
49
|
+
ALL_OPS.each do |op|
|
50
|
+
puts "Trying #{op}"
|
51
|
+
response = @rest.__send__(op, "http://rest-test.iron.io/code/200")
|
52
|
+
assert response.code == 200
|
53
|
+
assert response.body.include?("200")
|
54
|
+
p response.headers
|
55
|
+
assert response.headers.is_a?(Hash)
|
56
|
+
end
|
57
57
|
end
|
58
58
|
|
59
|
+
|
59
60
|
def test_404
|
60
61
|
puts 'test_404'
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
62
|
+
ALL_OPS.each do |op|
|
63
|
+
puts "Trying #{op}"
|
64
|
+
begin
|
65
|
+
response = @rest.__send__(op, "http://rest-test.iron.io/code/404")
|
66
|
+
assert false, "shouldn't get here"
|
67
|
+
rescue Rest::HttpError => ex
|
68
|
+
puts "Expected error: #{ex}"
|
69
|
+
#p ex.backtrace
|
70
|
+
assert ex.is_a?(Rest::HttpError)
|
71
|
+
assert ex.response
|
72
|
+
assert ex.response.body
|
73
|
+
assert ex.code == 404
|
74
|
+
assert ex.response.body.include?("404")
|
75
|
+
assert ex.to_s.include?("404")
|
76
|
+
end
|
73
77
|
end
|
78
|
+
|
74
79
|
end
|
75
80
|
|
76
81
|
def test_400
|
77
82
|
puts 'test_400'
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
83
|
+
ALL_OPS.each do |op|
|
84
|
+
puts "Trying #{op}"
|
85
|
+
begin
|
86
|
+
response = @rest.__send__(op, "http://rest-test.iron.io/code/400")
|
87
|
+
assert false, "shouldn't get here"
|
88
|
+
rescue Rest::HttpError => ex
|
89
|
+
puts "Expected error: #{ex}"
|
90
|
+
#p ex.backtrace
|
91
|
+
assert ex.is_a?(Rest::HttpError)
|
92
|
+
assert ex.response
|
93
|
+
assert ex.response.body
|
94
|
+
assert ex.code == 400
|
95
|
+
end
|
88
96
|
end
|
97
|
+
|
89
98
|
end
|
90
99
|
|
91
100
|
def test_500
|
92
|
-
puts '
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
101
|
+
puts 'test_500'
|
102
|
+
ALL_OPS.each do |op|
|
103
|
+
puts "Trying #{op}"
|
104
|
+
begin
|
105
|
+
response = @rest.__send__(op, "http://rest-test.iron.io/code/500")
|
106
|
+
assert false, "shouldn't get here"
|
107
|
+
rescue Rest::HttpError => ex
|
108
|
+
puts "Expected error: #{ex}"
|
109
|
+
#p ex.backtrace
|
110
|
+
assert ex.is_a?(Rest::HttpError)
|
111
|
+
assert ex.response
|
112
|
+
assert ex.response.body
|
113
|
+
assert ex.code == 500
|
114
|
+
end
|
103
115
|
end
|
104
116
|
end
|
105
117
|
|
@@ -107,22 +119,39 @@ class TestRest < TestBase
|
|
107
119
|
def test_post_with_headers
|
108
120
|
|
109
121
|
@token = "abctoken"
|
122
|
+
oauth = "OAuth #{@token}"
|
110
123
|
headers = {
|
111
124
|
'Content-Type' => 'application/json',
|
112
|
-
'Authorization' =>
|
125
|
+
'Authorization' => oauth,
|
113
126
|
'User-Agent' => "someagent"
|
114
127
|
}
|
128
|
+
key = "rest-gem-post"
|
115
129
|
body = {"foo" => "bar"}
|
116
|
-
response = @rest.post("
|
130
|
+
response = @rest.post("http://rest-test.iron.io/code/200?store=#{key}",
|
117
131
|
:body => body,
|
118
132
|
:headers => headers)
|
119
133
|
p response
|
134
|
+
response = @rest.get("http://rest-test.iron.io/stored/#{key}")
|
135
|
+
parsed = JSON.parse(response.body)
|
136
|
+
p parsed
|
137
|
+
assert_equal body, JSON.parse(parsed['body'])
|
138
|
+
assert_equal oauth, parsed['headers']['Authorization'.upcase]
|
139
|
+
|
140
|
+
body2 = "hello world"
|
141
|
+
response = @rest.post("http://rest-test.iron.io/code/200?store=#{key}",
|
142
|
+
:body => body2,
|
143
|
+
:headers => headers)
|
144
|
+
p response
|
145
|
+
response = @rest.get("http://rest-test.iron.io/stored/#{key}")
|
146
|
+
parsed = JSON.parse(response.body)
|
147
|
+
assert_equal body2, parsed['body']
|
148
|
+
|
120
149
|
response = @rest.post("http://rest-test.iron.io/code/200",
|
121
150
|
:body => body,
|
122
151
|
:headers => headers)
|
123
152
|
p response
|
124
153
|
|
125
|
-
response = @rest.post("
|
154
|
+
response = @rest.post("http://rest-test.iron.io/code/200?store=#{key}",
|
126
155
|
:body => "some string body",
|
127
156
|
:headers => headers)
|
128
157
|
p response
|
data/test/tmp.rb
CHANGED
@@ -9,16 +9,5 @@ class TestTemp < TestBase
|
|
9
9
|
|
10
10
|
end
|
11
11
|
|
12
|
-
def test_gzip
|
13
|
-
|
14
|
-
options = {}
|
15
|
-
url = "http://api.stackexchange.com/2.1/users?order=desc&sort=reputation&site=stackoverflow"
|
16
|
-
rest = Rest::Client.new
|
17
|
-
|
18
|
-
res = rest.get(url, options)
|
19
|
-
|
20
|
-
puts res.body
|
21
|
-
assert res.body.include?("items")
|
22
|
-
end
|
23
12
|
end
|
24
13
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|