rest-service-client 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +79 -14
- data/lib/restserviceclient.rb +41 -4
- data/lib/restserviceclient/version.rb +1 -1
- data/rest-service-client.gemspec +2 -2
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52389c1e5a8b0ac92ab10c0fa513ae869f4fe4ed
|
4
|
+
data.tar.gz: f04aecbe71a578cea9eb99ded4dd50de9a1761b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b808ced71893baec3ea69ee7ff5b0f5567ec3026225bcbcacb8583e00bdb920eb84c870611deb7e6739c1d635d637feb070e0cc25a43a7a126adc697f9e3746
|
7
|
+
data.tar.gz: 5805fd955602cd8c0414df5dd2ea0f5ab5d92c7ccc50766dee113567f81fde3a0b3a3aedd0031f1fa82969673c0e71b88ec04c847dd51d3b1c867053a72d3ab1
|
data/README.md
CHANGED
@@ -77,13 +77,13 @@ end
|
|
77
77
|
|
78
78
|
service = SimpleService.new
|
79
79
|
|
80
|
-
p service.first_photo['id']
|
80
|
+
p service.first_photo.result['id']
|
81
81
|
# 1
|
82
82
|
|
83
|
-
p service.second_photo['id']
|
83
|
+
p service.second_photo.result['id']
|
84
84
|
# 2
|
85
85
|
|
86
|
-
p service.photo(id: 10)['id']
|
86
|
+
p service.photo(id: 10).result['id']
|
87
87
|
# 10
|
88
88
|
|
89
89
|
```
|
@@ -98,8 +98,8 @@ class SimpleService
|
|
98
98
|
end
|
99
99
|
|
100
100
|
service = SimpleService.new
|
101
|
-
p service.photo['id'] # 1
|
102
|
-
p service.photo(id: 2)['id'] # 2
|
101
|
+
p service.photo.result['id'] # 1
|
102
|
+
p service.photo(id: 2).result['id'] # 2
|
103
103
|
```
|
104
104
|
|
105
105
|
```ruby
|
@@ -112,7 +112,7 @@ end
|
|
112
112
|
service = SimpleService.new
|
113
113
|
# Sends the request to 'https://jsonplaceholder.typicode.com/photos/1'
|
114
114
|
# includes Headers: { 'Authentication': 'my-token' }
|
115
|
-
p service.first_photo['albumId'] # 1
|
115
|
+
p service.first_photo.result['albumId'] # 1
|
116
116
|
|
117
117
|
# Sends the request to 'https://jsonplaceholder.typicode.com/photos/1'
|
118
118
|
# includes Headers: {
|
@@ -120,7 +120,7 @@ p service.first_photo['albumId'] # 1
|
|
120
120
|
# 'AnotherHeaderKey': 'AnotherHeaderValue'
|
121
121
|
# }
|
122
122
|
another_headers = { AnotherHeaderKey: 'AnotherHeaderValue' }
|
123
|
-
photo = service.first_photo(headers: another_headers)
|
123
|
+
photo = service.first_photo(headers: another_headers).result
|
124
124
|
p photo['albumId'] # 1
|
125
125
|
|
126
126
|
```
|
@@ -134,7 +134,7 @@ class SimpleService
|
|
134
134
|
end
|
135
135
|
|
136
136
|
service = SimpleService.new
|
137
|
-
service.delete_photo
|
137
|
+
service.delete_photo(id: 1).result
|
138
138
|
```
|
139
139
|
|
140
140
|
### POST
|
@@ -152,7 +152,10 @@ photo = {
|
|
152
152
|
url: 'http://placehold.it/600/92c952',
|
153
153
|
thumbnailUrl: 'http://placehold.it/150/92c952'
|
154
154
|
}
|
155
|
-
|
155
|
+
response = service.add_photo(payload: photo)
|
156
|
+
p response.status # 201
|
157
|
+
p response.headers # {:date=>"...}
|
158
|
+
p response.result
|
156
159
|
=begin
|
157
160
|
{
|
158
161
|
"albumId"=>1,
|
@@ -176,7 +179,7 @@ end
|
|
176
179
|
|
177
180
|
service = SimpleService.new
|
178
181
|
updated_photo = service.update_photo_data id: 1, payload: { title: 'new title'}
|
179
|
-
p updated_photo['title']
|
182
|
+
p updated_photo.result['title']
|
180
183
|
# "new title"
|
181
184
|
|
182
185
|
```
|
@@ -199,7 +202,7 @@ class SimpleService
|
|
199
202
|
end
|
200
203
|
|
201
204
|
service = SimpleService.new
|
202
|
-
service.photo
|
205
|
+
service.photo.result
|
203
206
|
=begin
|
204
207
|
______
|
205
208
|
|
|
@@ -232,6 +235,22 @@ gets the one argument with response body and returns whatever you want.
|
|
232
235
|
And you need to add your serializer to your service using:
|
233
236
|
`serializer MyAwesomSerializer`
|
234
237
|
|
238
|
+
## Errors
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
class SimpleService
|
242
|
+
include RestServiceClient
|
243
|
+
debug true
|
244
|
+
host 'https://jsonplaceholder.typicode.com'
|
245
|
+
get :bad_url, '/bad_url'
|
246
|
+
end
|
247
|
+
|
248
|
+
service = SimpleService.new
|
249
|
+
response = service.bad_url
|
250
|
+
p response.status # 404
|
251
|
+
p response.message # "404 Not Found"
|
252
|
+
|
253
|
+
```
|
235
254
|
### Example
|
236
255
|
```ruby
|
237
256
|
class Photo
|
@@ -293,12 +312,58 @@ And you need to add your serializer to your service using:
|
|
293
312
|
|
294
313
|
service = MyService.new
|
295
314
|
|
296
|
-
p service.find_photo(id: 1).album_id # 1
|
297
|
-
p service.photos.find { |p| p.id == 3 }.id # 1
|
298
|
-
p service.find_post(id: 1).user_id # 1
|
315
|
+
p service.find_photo(id: 1).result.album_id # 1
|
316
|
+
p service.photos.result.find { |p| p.id == 3 }.id # 1
|
317
|
+
p service.find_post(id: 1).result.user_id # 1
|
299
318
|
|
300
319
|
```
|
301
320
|
|
321
|
+
|
322
|
+
## Response Object
|
323
|
+
|
324
|
+
#### Response
|
325
|
+
If request was completely sent, the class of response be `RestServiceClient::Response`
|
326
|
+
This object has `:status`, `:headers`, `:body` and `:result` fields.
|
327
|
+
|
328
|
+
`:status` contains the number(integer) of http status code.
|
329
|
+
|
330
|
+
`:headers` contains the http headers from response.
|
331
|
+
|
332
|
+
`:body` will be a http body as plain text.
|
333
|
+
|
334
|
+
`:result` will contain deserialized http body.
|
335
|
+
|
336
|
+
#### ResponseWithError
|
337
|
+
If the request failed, the class of response will be RestServiceClient::ResponseWithError
|
338
|
+
This object has `:status`, `:headers`, `:body`, `:result` and `:message`
|
339
|
+
All fields except of `:message` are equals to `RestServiceClient::Response` fields.
|
340
|
+
|
341
|
+
`:message` will contain the string with error message
|
342
|
+
|
343
|
+
You are able to check the response before trying to use result to avoid exceptions:
|
344
|
+
```ruby
|
345
|
+
class SimpleService
|
346
|
+
include RestServiceClient
|
347
|
+
host 'https://jsonplaceholder.typicode.com'
|
348
|
+
get :get_photo, '/photos/:id'
|
349
|
+
get :bad_url, '/bad_url'
|
350
|
+
end
|
351
|
+
|
352
|
+
service = SimpleService.new
|
353
|
+
|
354
|
+
response = service.get_photo id: 1
|
355
|
+
if response.status == 200
|
356
|
+
p "Photo with name: #{response.result['name']} has been loaded"
|
357
|
+
end
|
358
|
+
|
359
|
+
response = service.bad_url
|
360
|
+
if response.status != 200
|
361
|
+
p "Something was wrong. Reason: #{response.message}"
|
362
|
+
# "Something was wrong. Reason: 404 Not Found"
|
363
|
+
end
|
364
|
+
|
365
|
+
```
|
366
|
+
|
302
367
|
## Development
|
303
368
|
|
304
369
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/restserviceclient.rb
CHANGED
@@ -115,18 +115,36 @@ module RestServiceClient
|
|
115
115
|
puts "| Payload: #{payload}"
|
116
116
|
end
|
117
117
|
|
118
|
-
|
118
|
+
begin
|
119
|
+
result = make_request http_method, endpoint, payload, headers
|
120
|
+
response = RestServiceClient::Response.new(
|
121
|
+
result.code,
|
122
|
+
result.headers,
|
123
|
+
result.body,
|
124
|
+
serializer.deserialize(result.body)
|
125
|
+
)
|
126
|
+
rescue RestClient::Exception => e
|
127
|
+
response = RestServiceClient::ResponseWithError.new(
|
128
|
+
e.http_code,
|
129
|
+
e.http_headers,
|
130
|
+
e.http_body,
|
131
|
+
e.http_body,
|
132
|
+
e.message
|
133
|
+
)
|
134
|
+
end
|
119
135
|
|
120
136
|
if @debug
|
121
137
|
puts '|'
|
122
138
|
puts "| #{self.class.name} is processing the response on #{http_method.upcase} request to #{endpoint}"
|
123
|
-
puts "| Status: #{response.
|
124
|
-
puts "|
|
139
|
+
puts "| Status: #{response.status}"
|
140
|
+
puts "| Headers: #{response.headers}"
|
141
|
+
puts "| Result: #{response.result}"
|
142
|
+
puts "| Error message: #{response.message}" if response.is_a?(RestServiceClient::ResponseWithError)
|
125
143
|
puts '|______'
|
126
144
|
puts
|
127
145
|
end
|
128
146
|
|
129
|
-
|
147
|
+
response
|
130
148
|
end
|
131
149
|
end
|
132
150
|
end
|
@@ -136,4 +154,23 @@ module RestServiceClient
|
|
136
154
|
JSON.parse(data)
|
137
155
|
end
|
138
156
|
end
|
157
|
+
|
158
|
+
class Response
|
159
|
+
attr_reader :status, :headers, :body, :result
|
160
|
+
|
161
|
+
def initialize(status, headers, body, result)
|
162
|
+
@status = status
|
163
|
+
@headers = headers
|
164
|
+
@body = body
|
165
|
+
@result = result
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class ResponseWithError < Response
|
170
|
+
attr_reader :message
|
171
|
+
def initialize(status, headers, body, result, message)
|
172
|
+
super(status, headers, body, result)
|
173
|
+
@message = message
|
174
|
+
end
|
175
|
+
end
|
139
176
|
end
|
data/rest-service-client.gemspec
CHANGED
@@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
27
27
|
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
28
|
|
29
|
-
spec.add_dependency 'rest-client'
|
30
|
-
spec.add_dependency 'json'
|
29
|
+
spec.add_dependency 'rest-client', '~> 2.0'
|
30
|
+
spec.add_dependency 'json', '~> 2.1'
|
31
31
|
|
32
32
|
spec.required_ruby_version = '>= 2.0.0'
|
33
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest-service-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Bespalov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -56,30 +56,30 @@ dependencies:
|
|
56
56
|
name: rest-client
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '2.0'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '2.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: json
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '2.1'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '2.1'
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- gravisbesya@list.ru
|