rest-service-client 0.1.4 → 0.2.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 +90 -8
- data/lib/restserviceclient/version.rb +1 -1
- data/lib/restserviceclient.rb +21 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ac0a3ad05b3b3b3d9e17840d32c9d8e7844a73c
|
4
|
+
data.tar.gz: ff5156657486903f275ac4ec8eb531ecc99a9866
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 141e9f10a76baa1d2a4c8e1a14b704fcb3f354b632ef692ba6a8c0639a42dcb348edbc778f09b05a91052466285d2d83bcd5f755c188cdb458819c34967b4f62
|
7
|
+
data.tar.gz: c53e460873fefdcf7f39077fe8170d64b0290931cc747407a3ce72702a5f30839be2f885b2dd86d1efcaebb4de90d276c60eba0a8174ddbec5a3eb8e760dd3eb
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# Rest Service Client
|
2
|
+
[](https://badge.fury.io/rb/rest-service-client)
|
3
|
+
|
2
4
|
|
3
5
|
## Installation
|
4
6
|
|
@@ -34,7 +36,7 @@ class MyAwesomeService
|
|
34
36
|
|
35
37
|
# Sets the default headers for every requests.
|
36
38
|
# Will be replaced using: MyAwesomeService.new.find_photo(id: 1, headers: { 'Authorization' => 'Bearer 123456' })
|
37
|
-
headers
|
39
|
+
headers Authorization: 'OAuth 1111|111'
|
38
40
|
|
39
41
|
# Sets the default params for every requests.
|
40
42
|
# Will be replaced using: MyAwesomeService.new.find_photo(id: 20)
|
@@ -92,7 +94,7 @@ p service.photo(id: 10)['id']
|
|
92
94
|
class SimpleService
|
93
95
|
include RestServiceClient
|
94
96
|
host 'https://jsonplaceholder.typicode.com'
|
95
|
-
get :photo, '/photos/:id', { id: 1 }
|
97
|
+
get :photo, '/photos/:id', params: { id: 1 }
|
96
98
|
end
|
97
99
|
|
98
100
|
service = SimpleService.new
|
@@ -104,8 +106,7 @@ p service.photo(id: 2)['id'] # 2
|
|
104
106
|
class SimpleService
|
105
107
|
include RestServiceClient
|
106
108
|
host 'https://jsonplaceholder.typicode.com'
|
107
|
-
|
108
|
-
get :first_photo, '/photos/1', {}, { Authentication: 'my-token' }
|
109
|
+
get :first_photo, '/photos/1', headers: { Authentication: 'my-token' }
|
109
110
|
end
|
110
111
|
|
111
112
|
service = SimpleService.new
|
@@ -189,8 +190,12 @@ class SimpleService
|
|
189
190
|
include RestServiceClient
|
190
191
|
debug true
|
191
192
|
host 'https://jsonplaceholder.typicode.com'
|
192
|
-
headers key1: 'value1', 'key2'
|
193
|
-
get :photo,
|
193
|
+
headers key1: 'value1', 'key2' => 'value2'
|
194
|
+
get :photo,
|
195
|
+
'/photos/:id',
|
196
|
+
params: { id: 1 },
|
197
|
+
headers: { key3: 'value3' },
|
198
|
+
payload: { data: { type: 'get_photo_by_id' }}
|
194
199
|
end
|
195
200
|
|
196
201
|
service = SimpleService.new
|
@@ -199,8 +204,8 @@ service.photo
|
|
199
204
|
______
|
200
205
|
|
|
201
206
|
| SimpleService is processing GET request to https://jsonplaceholder.typicode.com/photos/1
|
202
|
-
| Headers: {:key1=>"value1",
|
203
|
-
| Payload: {}
|
207
|
+
| Headers: {:key1=>"value1", "key2"=>"value2", :key3=>"value3"}
|
208
|
+
| Payload: {:data=>{:type=>"get_photo_by_id"}}
|
204
209
|
|
|
205
210
|
| SimpleService is processing the response on GET request to https://jsonplaceholder.typicode.com/photos/1
|
206
211
|
| Status: 200
|
@@ -215,7 +220,84 @@ service.photo
|
|
215
220
|
=end
|
216
221
|
|
217
222
|
```
|
223
|
+
## Serializer
|
224
|
+
|
225
|
+
By the default RestServiceClient uses JSON serializer and returns the arrays and hashes
|
226
|
+
with key as string
|
227
|
+
|
228
|
+
You can use custom serializer.
|
229
|
+
For do this, create a serializer class with `self.deserialize` method which
|
230
|
+
gets the one argument with response body and returns whatever you want.
|
231
|
+
|
232
|
+
And you need to add your serializer to your service using:
|
233
|
+
`serializer MyAwesomSerializer`
|
234
|
+
|
235
|
+
### Example
|
236
|
+
```ruby
|
237
|
+
class Photo
|
238
|
+
attr_accessor :id, :title, :album_id
|
239
|
+
end
|
240
|
+
|
241
|
+
class PhotoSerializer
|
242
|
+
class Object::String
|
243
|
+
def underscore
|
244
|
+
gsub(/::/, '/')
|
245
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
246
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
247
|
+
.tr('-', '_')
|
248
|
+
.downcase
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def self.deserialize(json)
|
253
|
+
data = JSON.parse(json)
|
254
|
+
|
255
|
+
return build_photo(data) if data.is_a? Hash
|
256
|
+
return data.map { |p| build_photo(p) } if data.is_a? Array
|
257
|
+
end
|
258
|
+
|
259
|
+
def self.build_photo(data)
|
260
|
+
object = Photo.new
|
261
|
+
data.each_with_object(object) do |(k, v), o|
|
262
|
+
setter = "#{k.underscore}=".to_sym
|
263
|
+
o.send(setter, v) if o.respond_to?(setter)
|
264
|
+
end
|
265
|
+
object
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
class Post
|
270
|
+
attr_accessor :id, :title, :user_id
|
271
|
+
end
|
272
|
+
|
273
|
+
class PostSerializer
|
274
|
+
def self.deserialize(json)
|
275
|
+
data = JSON.parse(json)
|
276
|
+
object = Post.new
|
277
|
+
object.id = data['id']
|
278
|
+
object.title = data['title']
|
279
|
+
object.user_id = data['userId']
|
280
|
+
object
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
|
285
|
+
class MyService
|
286
|
+
include RestServiceClient
|
287
|
+
host 'https://jsonplaceholder.typicode.com'
|
288
|
+
serializer PhotoSerializer
|
289
|
+
get :photos, '/photos'
|
290
|
+
get :find_photo, '/photos/:id'
|
291
|
+
get :find_post, '/posts/:id', serializer: PostSerializer
|
292
|
+
end
|
293
|
+
|
294
|
+
service = MyService.new
|
295
|
+
|
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
|
218
299
|
|
300
|
+
```
|
219
301
|
|
220
302
|
## Development
|
221
303
|
|
data/lib/restserviceclient.rb
CHANGED
@@ -64,33 +64,39 @@ module RestServiceClient
|
|
64
64
|
define_method :get_debug, &-> { flag }
|
65
65
|
end
|
66
66
|
|
67
|
-
def get(method_name, path = '',
|
68
|
-
add_method :get, method_name, path,
|
67
|
+
def get(method_name, path = '', options = {})
|
68
|
+
add_method :get, method_name, path, options
|
69
69
|
end
|
70
70
|
|
71
|
-
def post(method_name, path = '',
|
72
|
-
add_method :post, method_name, path,
|
71
|
+
def post(method_name, path = '', options = {})
|
72
|
+
add_method :post, method_name, path, options
|
73
73
|
end
|
74
74
|
|
75
|
-
def put(method_name, path = '',
|
76
|
-
add_method :put, method_name, path,
|
75
|
+
def put(method_name, path = '', options = {})
|
76
|
+
add_method :put, method_name, path, options
|
77
77
|
end
|
78
78
|
|
79
|
-
def patch(method_name, path = '',
|
80
|
-
add_method :patch, method_name, path,
|
79
|
+
def patch(method_name, path = '', options = {})
|
80
|
+
add_method :patch, method_name, path, options
|
81
81
|
end
|
82
82
|
|
83
|
-
def delete(method_name, path = '',
|
84
|
-
add_method :delete, method_name, path,
|
83
|
+
def delete(method_name, path = '', options = {})
|
84
|
+
add_method :delete, method_name, path, options
|
85
85
|
end
|
86
86
|
|
87
87
|
private
|
88
88
|
|
89
|
-
|
89
|
+
# default_params, default_headers, default_payload, serializer
|
90
|
+
def add_method(http_method, method_name, path, options)
|
90
91
|
define_method method_name do |headers: {}, payload: {}, parameters: {}, **params|
|
91
|
-
|
92
|
-
|
93
|
-
|
92
|
+
options_params = options.fetch(:params, {})
|
93
|
+
options_headers = options.fetch(:headers, {})
|
94
|
+
options_payload = options.fetch(:payload, {})
|
95
|
+
serializer = options.fetch(:serializer, @serializer)
|
96
|
+
|
97
|
+
params = @default_params.merge(options_params.merge(params.merge(parameters)))
|
98
|
+
headers = @default_headers.merge(options_headers.merge(headers))
|
99
|
+
payload = options_payload.merge payload
|
94
100
|
|
95
101
|
uri = path.clone
|
96
102
|
|
@@ -120,7 +126,7 @@ module RestServiceClient
|
|
120
126
|
puts
|
121
127
|
end
|
122
128
|
|
123
|
-
|
129
|
+
serializer.deserialize(response.body)
|
124
130
|
end
|
125
131
|
end
|
126
132
|
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.2.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-08-
|
11
|
+
date: 2017-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|