rest 2.0.4 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rest (2.0.3)
4
+ rest (2.0.4)
5
5
  net-http-persistent
6
6
  rest-client (>= 0.3.0)
7
7
 
@@ -1,3 +1,3 @@
1
1
  module Rest
2
- VERSION = "2.0.4"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -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( response.body )
20
- gz = Zlib::GzipReader.new( sio )
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
- response = nil
131
- begin
132
- uri = URI(url)
133
- post = Net::HTTP::Post.new fix_path(uri.request_uri)
134
- add_headers(post, req_hash, default_headers)
135
- post.body = stringed_body(req_hash[:body]) if req_hash[:body]
136
- response = http.request uri, post
137
- response = NetHttpPersistentResponseWrapper.new(response)
138
- rescue Net::HTTPClientError => ex
139
- raise NetHttpPersistentExceptionWrapper.new(ex)
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
- response
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
- response = nil
156
- begin
157
- uri = URI(url)
158
- post = Net::HTTP::Put.new fix_path(uri.request_uri)
159
- add_headers(post, req_hash, default_headers)
160
- post.body = stringed_body(req_hash[:body]) if req_hash[:body]
161
- response = http.request uri, post
162
- response = NetHttpPersistentResponseWrapper.new(response)
163
- rescue Net::HTTPClientError => ex
164
- raise NetHttpPersistentExceptionWrapper.new(ex)
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
- response
164
+ r
169
165
  end
170
166
 
171
167
  def delete(url, req_hash={})
172
- response = nil
173
- begin
174
- uri = URI(url)
175
- post = Net::HTTP::Delete.new fix_path(uri.request_uri)
176
- add_headers(post, req_hash, default_headers)
177
- response = http.request uri, post
178
- response = NetHttpPersistentResponseWrapper.new(response)
179
- rescue Net::HTTPClientError => ex
180
- raise NetHttpPersistentExceptionWrapper.new(ex)
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
- response
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
- req_hash[:payload] = req_hash[:body] if req_hash[:body]
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
@@ -63,8 +63,6 @@ module Rest
63
63
  r
64
64
  end
65
65
 
66
-
67
-
68
66
  def post(url, req_hash={})
69
67
  req_hash = default_typhoeus_options.merge(req_hash)
70
68
  to_json_parts(req_hash)
@@ -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
- def bin
21
- @request_bin
22
- end
19
+ ALL_OPS = [:get, :put, :post, :delete]
20
+
23
21
 
24
22
  end
@@ -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
- response = @rest.get("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
-
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
- begin
62
- response = @rest.get("http://rest-test.iron.io/code/404")
63
- assert false, "shouldn't get here"
64
- rescue Rest::HttpError => ex
65
- puts "EX: " + ex.inspect
66
- p ex.backtrace
67
- assert ex.is_a?(Rest::HttpError)
68
- assert ex.response
69
- assert ex.response.body
70
- assert ex.code == 404
71
- assert ex.response.body.include?("404")
72
- assert ex.to_s.include?("404")
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
- begin
79
- response = @rest.get("http://rest-test.iron.io/code/400")
80
- assert false, "shouldn't get here"
81
- rescue Rest::HttpError => ex
82
- puts "EX: #{ex}"
83
- p ex.backtrace
84
- assert ex.is_a?(Rest::HttpError)
85
- assert ex.response
86
- assert ex.response.body
87
- assert ex.code == 400
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 '500'
93
- begin
94
- response = @rest.get("http://rest-test.iron.io/code/500")
95
- assert false, "shouldn't get here"
96
- rescue Rest::HttpError => ex
97
- puts "EX: " + ex.inspect
98
- p ex.backtrace
99
- assert ex.is_a?(Rest::HttpError)
100
- assert ex.response
101
- assert ex.response.body
102
- assert ex.code == 500
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' => "OAuth #{@token}",
125
+ 'Authorization' => oauth,
113
126
  'User-Agent' => "someagent"
114
127
  }
128
+ key = "rest-gem-post"
115
129
  body = {"foo" => "bar"}
116
- response = @rest.post("#{bin}",
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("#{bin}",
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
@@ -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
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-10-02 00:00:00.000000000 Z
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client