response_mapper 0.1.2 → 0.1.3
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 +31 -19
- data/lib/response_mapper.rb +5 -5
- 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: 115b2f49f2d22afd4ded0d05fabcc8b632e5001a
|
4
|
+
data.tar.gz: f4affaa06e80e1af007e4885286f6dcb16895a78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c1c5d68bd19e4478767fed125c54c61bbbd5d9807cc7985b06e9f81d1201adadcc8c384ec8106a5d266320b22bd3a78ce85a9b2363d4a432fdad126c8fa3bc7
|
7
|
+
data.tar.gz: 82a7c4b373f35b8003f68ac56a220d6b5ec7b0c1bfb607b73871f7f7cfe49f57e28b22e336e3efb197a005b557ee9a46edb9b15c362b0bea882798cfce8d64a4
|
data/README.md
CHANGED
@@ -1,35 +1,41 @@
|
|
1
1
|
[](https://travis-ci.org/smakagon/response_mapper)
|
2
2
|
|
3
3
|
# ResponseMapper
|
4
|
+
These days we all deal with many different APIs; It can be either third-party services, or our own microservices. Not all of them are well-designed and sometimes their attributes are named inconsistently.
|
4
5
|
|
5
|
-
|
6
|
-
It can be either third-party services, or our own microservices.
|
7
|
-
Not all of them are well-designed and sometimes their attributes named in a really weird way.
|
6
|
+
`ResponseMapper` allows you to map attributes from an API response to your application’s domain language.
|
8
7
|
|
9
|
-
|
8
|
+
### An example of a common API response before ResponseMapper:
|
9
|
+
```ruby
|
10
|
+
{ "orderNumber" => 10, "orderItems" => [{ "orderItemId" 1, "itemTitle" "Book" }] }
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
### An example of the above API response after ResponseMapper:
|
14
|
+
```ruby
|
15
|
+
{ id: 10, items: [{ id: 1, title: "Book" }] }
|
16
|
+
```
|
12
17
|
|
18
|
+
## What does ResponseMapper Do?
|
19
|
+
Using the example above let’s look at a response we may get from a 3rd party API:
|
13
20
|
```ruby
|
14
21
|
response = JSON.parse(response_from_api)
|
15
|
-
# { "
|
22
|
+
# => { "orderNumber" => 10, "orderItems" => [{ "orderItemId" 1, "itemTitle" "Book" }] }
|
16
23
|
```
|
17
24
|
|
18
|
-
Once we parsed response, all keys are strings. Usually we want to do two things:
|
19
|
-
|
25
|
+
### Once we parsed response, all keys are strings. Usually we want to do two things:
|
20
26
|
1. Map response, so we can easily instantiate entity from response.
|
21
27
|
2. Symbolize keys to keep things consistent.
|
22
28
|
|
23
|
-
With
|
29
|
+
### With ResponseMapper we can do this:
|
24
30
|
|
25
31
|
```ruby
|
26
|
-
mapping = {
|
32
|
+
mapping = { orderNumber: :id, orderItems: :items, orderItemId: :id, itemTitle: :title }
|
27
33
|
|
28
34
|
order_attributes = ResponseMapper.map(data: response, mapping: mapping)
|
29
|
-
# { id: 10, items: [{ id: 1, title: "Book" }] }
|
35
|
+
# => { id: 10, items: [{ id: 1, title: "Book" }] }
|
30
36
|
```
|
31
37
|
|
32
|
-
Now we have nice Hash with symbolized keys that correspond to attributes of `Order` in our system.
|
38
|
+
Now we have a nice Hash with symbolized keys that correspond to attributes of `Order` in our system.
|
33
39
|
For example further step could be just wrap this hash into `Order` entity:
|
34
40
|
|
35
41
|
```ruby
|
@@ -58,30 +64,36 @@ Or install it yourself as:
|
|
58
64
|
$ gem install response_mapper
|
59
65
|
|
60
66
|
## Usage
|
61
|
-
|
62
67
|
`ResponseMapper` provides one class method `.map` which has two required params: `data` and `mapping`.
|
63
68
|
There is one optional parameter: `symbolize_keys` which is set to `true` by default.
|
64
69
|
|
65
|
-
|
70
|
+
ex.
|
71
|
+
```ruby
|
72
|
+
order_attributes = ResponseMapper.map(data: response, mapping: mapping)
|
73
|
+
```
|
74
|
+
|
75
|
+
### `data`
|
66
76
|
`data` can be anything, but `ResponseMapper` will try to map it only if it's a `Hash` or `Array`.
|
67
77
|
If it's not a `Hash` or `Array` - `ResponseMapper` will return `data` as is.
|
68
78
|
|
69
|
-
### mapping
|
79
|
+
### `mapping`
|
70
80
|
`mapping` should be a Hash with attributes you want to map:
|
71
81
|
|
72
|
-
```
|
73
|
-
{
|
82
|
+
```ruby
|
83
|
+
mapping = { orderNumber: :id, orderItems: :items, orderItemId: :id, itemTitle: :title }
|
74
84
|
```
|
75
85
|
|
76
|
-
It will map any
|
86
|
+
It will map any occurrence of `:order_number` in data to `:id`.
|
77
87
|
If `mapping` is not a Hash (or empty Hash) - ResponseMapper will raise `ResponseMapper::Error`.
|
78
88
|
|
79
|
-
### symbolize_keys
|
89
|
+
### `symbolize_keys`
|
80
90
|
`sybmolize_keys` is set to `true` by default.
|
81
91
|
If your data contains hashes with strings as keys, they will be symbolized and then mapped.
|
82
92
|
|
83
93
|
See [more examples here](https://github.com/smakagon/response_mapper/blob/master/examples/examples.rb).
|
84
94
|
|
95
|
+
If you want to learn more, read an [article](http://rubyblog.pro/2017/09/how-to-protect-naming-conventions-when-working-with-microservices) on how `ResponseMapper` can help to protect naming conventions when working with microservices or third-party API.
|
96
|
+
|
85
97
|
## Contributing
|
86
98
|
|
87
99
|
Bug reports and pull requests are welcome on GitHub at https://github.com/smakagon/response_mapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/response_mapper.rb
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
# { id: 1, items: [1,2,3] }
|
13
13
|
#
|
14
14
|
class ResponseMapper
|
15
|
-
VERSION = '0.1.
|
15
|
+
VERSION = '0.1.3'
|
16
16
|
|
17
17
|
Error = Class.new(StandardError)
|
18
18
|
|
@@ -67,9 +67,9 @@ class ResponseMapper
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def validate_mapping
|
70
|
-
# rubocop:disable
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
message = 'Please, provide Hash with mapping, for example: { order_number: :id }' # rubocop:disable LineLength
|
71
|
+
raise Error, message unless mapping.is_a?(Hash)
|
72
|
+
|
73
|
+
raise Error, message if mapping.empty?
|
74
74
|
end
|
75
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: response_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergii Makagon
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|