response_mapper 0.1.0 → 0.1.1
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/.rubocop.yml +1 -1
- data/README.md +4 -0
- data/examples/examples.rb +45 -0
- data/lib/response_mapper.rb +6 -6
- data/response_mapper.gemspec +4 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 500f3948bcf3d5089cb999bdf6e47b3549ec5e4a
|
4
|
+
data.tar.gz: 6ecaba7e3dccd2c653818f5ddbe4c8a1f76759f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: deaa2ed34d84d029e56c0f42ac0766f54caaa3cfee77816c5dc48581a06258e31e00703f20dda3e32a8fff1456d3b661c611730310eadc33acd70bc1872670c9
|
7
|
+
data.tar.gz: 81a381969cdc7396308589e6f4d6a6da26cc94a7824df14d5f5dd9a431162a5ad322f9e8862bb70aad027059f38a47cf54f46b0422ef3a6567287d9c2721812b
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -39,6 +39,8 @@ Entity::Order.new(order_attributes)
|
|
39
39
|
`ResponseMapper` maps and symbolizes keys even for nested arrays and hashes.
|
40
40
|
It will work for more complex responses.
|
41
41
|
|
42
|
+
See [more examples here](https://github.com/smakagon/response_mapper/blob/master/examples/examples.rb).
|
43
|
+
|
42
44
|
## Installation
|
43
45
|
|
44
46
|
Add this line to your application's Gemfile:
|
@@ -78,6 +80,8 @@ If `mapping` is not a Hash (or empty Hash) - ResponseMapper will raise `Response
|
|
78
80
|
`sybmolize_keys` is set to `true` by default.
|
79
81
|
If your data contains hashes with strings as keys, they will be symbolized and then mapped.
|
80
82
|
|
83
|
+
See [more examples here](https://github.com/smakagon/response_mapper/blob/master/examples/examples.rb).
|
84
|
+
|
81
85
|
## Contributing
|
82
86
|
|
83
87
|
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.
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'response_mapper'
|
2
|
+
require 'dry-struct'
|
3
|
+
|
4
|
+
# With basic Hash:
|
5
|
+
response = { "foo" => "bar" }
|
6
|
+
ResponseMapper.map(data: response, mapping: { foo: :name })
|
7
|
+
# { name: "bar" }
|
8
|
+
|
9
|
+
# With Array of strings:
|
10
|
+
response = ["order_number", "order_items"]
|
11
|
+
ResponseMapper.map(data: response, mapping: { "order_number" => "id" })
|
12
|
+
# ["id", "order_items"]
|
13
|
+
|
14
|
+
# With Array of Hashes it still symbolizes and maps all keys:
|
15
|
+
response = [
|
16
|
+
{ "order_number" => 1, "order_items" => [{"order_id" => 11, "title" => "Foo" }] },
|
17
|
+
{ "order_number" => 2, "order_items" => [{"order_id" => 21, "title" => "Bar" }] },
|
18
|
+
]
|
19
|
+
|
20
|
+
mapping = { order_number: :id, order_items: :items, order_id: :id }
|
21
|
+
ResponseMapper.map(data: response, mapping: mapping)
|
22
|
+
# [
|
23
|
+
# { id: 1, items: [{ id: 11, title: "Foo" }]},
|
24
|
+
# { id: 2, items: [{ id: 21, title: "Bar" }]}
|
25
|
+
# ]
|
26
|
+
|
27
|
+
|
28
|
+
# Dry-Struct Example
|
29
|
+
module Types
|
30
|
+
include Dry::Types.module
|
31
|
+
end
|
32
|
+
|
33
|
+
class Order < Dry::Struct::Value
|
34
|
+
attribute :id, Types::Strict::Int
|
35
|
+
attribute :amount, Types::Strict::Int
|
36
|
+
end
|
37
|
+
|
38
|
+
response = { "order_number" => 1, "total_amount" => 200 }
|
39
|
+
mapping = { order_number: :id, total_amount: :amount }
|
40
|
+
|
41
|
+
order_attributes = ResponseMapper.map(data: response, mapping: mapping)
|
42
|
+
|
43
|
+
order = Order.new(order_attributes)
|
44
|
+
order.id # => 1
|
45
|
+
order.amount # => 200
|
data/lib/response_mapper.rb
CHANGED
@@ -2,17 +2,17 @@
|
|
2
2
|
|
3
3
|
# Allows to map API response to domain language of your application
|
4
4
|
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
5
|
+
# ResponseMapper.map(
|
6
|
+
# data: { order_number: 1, order_items: [1,2,3] }
|
7
|
+
# mapping: { order_number: :id, order_items: :items }
|
8
|
+
# )
|
9
9
|
#
|
10
10
|
# Returns nice Hash with proper naming
|
11
|
-
# which could be used to instantiate
|
11
|
+
# which could be used to instantiate Order entity:
|
12
12
|
# { id: 1, items: [1,2,3] }
|
13
13
|
#
|
14
14
|
class ResponseMapper
|
15
|
-
VERSION = '0.1.
|
15
|
+
VERSION = '0.1.1'
|
16
16
|
|
17
17
|
Error = Class.new(StandardError)
|
18
18
|
|
data/response_mapper.gemspec
CHANGED
@@ -13,7 +13,10 @@ Gem::Specification.new do |spec|
|
|
13
13
|
|
14
14
|
# rubocop:disable LineLength
|
15
15
|
spec.summary = 'Allows to map API response to domain language of your application'
|
16
|
-
spec.description = '
|
16
|
+
spec.description = 'These days we all deal with many different APIs. \
|
17
|
+
It can be either third-party services, or our own microservices. \
|
18
|
+
Not all of them are well-designed and sometimes their attributes named in a really weird way.\
|
19
|
+
ResponseMapper allows to map attributes from API response to your domain language.'
|
17
20
|
spec.homepage = 'https://github.com/smakagon/response_mapper'
|
18
21
|
spec.license = 'MIT'
|
19
22
|
# rubocop:enable LineLength
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergii Makagon
|
@@ -80,8 +80,11 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.10'
|
83
|
-
description:
|
84
|
-
|
83
|
+
description: |-
|
84
|
+
These days we all deal with many different APIs. \
|
85
|
+
It can be either third-party services, or our own microservices. \
|
86
|
+
Not all of them are well-designed and sometimes their attributes named in a really weird way.\
|
87
|
+
ResponseMapper allows to map attributes from API response to your domain language.
|
85
88
|
email:
|
86
89
|
- makagon87@gmail.com
|
87
90
|
executables: []
|
@@ -98,6 +101,7 @@ files:
|
|
98
101
|
- Rakefile
|
99
102
|
- bin/console
|
100
103
|
- bin/setup
|
104
|
+
- examples/examples.rb
|
101
105
|
- lib/response_mapper.rb
|
102
106
|
- response_mapper.gemspec
|
103
107
|
homepage: https://github.com/smakagon/response_mapper
|