payload-translator 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7e0f8bfa9b529a0adb567c8ebccdad2edd6ad64a1853a3ff49cf772ee46cf76
4
- data.tar.gz: 0e73870879ff49b8ec75976df58af3054206fdc77888588c6d863184350c8a78
3
+ metadata.gz: '08e5b5de58601bf69863d094eaddd9a8b412bb771314853182dff8d74d977bab'
4
+ data.tar.gz: 3818f35a6bb58cf00f94c87f7257cb7a698a58a919c80c5718473923e4c7f836
5
5
  SHA512:
6
- metadata.gz: 53c2c2a1a96f8c6483b415655c2566efa85c401fa0ac3fec8e637c6277fec188def4a266d5349a6f16ccf000b0b7f2e54030870318c45fbfce633ea40043cee2
7
- data.tar.gz: 2f8df32464185ef5c8c7169b931bd7961657d83814f346c3094b7b4ae9b248303ec201fcf7e8465a27cab7206069b49669d540770e0137069af7b60324087451
6
+ metadata.gz: f2a0d7a728c6d7453e7e9c8b60ebe6e3efa704d7a03430cb48aa404fc85fb187e8dfd4a2153c880b10e13ab178f38ac6d02d3e95a43663246e1b82d333f55810
7
+ data.tar.gz: 680a16cdb5af7fb40296eef5763f5b5890d340f5cac55a7191718d477803006f10b71a48ac5ae9471203bca2653b4a575ef384b8f4d597bf873c19d10d2b287b
data/README.md CHANGED
@@ -86,3 +86,83 @@ Or formatter per service
86
86
  ```ruby
87
87
  PayloadTranslator::Service(config, formatters: to_integer: ->(value) { value.to_i })
88
88
  ```
89
+
90
+ ## Configure adapters configuration
91
+
92
+ Store adapter configuration
93
+
94
+ ```ruby
95
+ PayloadTranslator.configure do |config|
96
+ config.adapters_configurations = {
97
+ internal_to_external: {
98
+ "payload" => {
99
+ "id" => { "$field" => "_id" }
100
+ }
101
+ }
102
+ }
103
+ end
104
+ ```
105
+
106
+ ```ruby
107
+ translator = PayloadTranslator::Service.new(:internal_to_external)
108
+ ```
109
+
110
+ ### Multiple translator
111
+
112
+ ```ruby
113
+ translator = PayloadTranslator::ServiceMultiple.new([:internal_to_external, :external_to_internal])
114
+
115
+ translated_response = translator.translate({"user_id" => 4 }) do |translated_payload|
116
+ response = Net::HTTP.post_form(
117
+ URI("https://jsonplaceholder.typicode.com/todos"),
118
+ translated_payload
119
+ ).body
120
+
121
+ JSON.parse(response)
122
+ end
123
+ ```
124
+
125
+ ### Complex adapters
126
+
127
+ Translate all array items, use `$field_for_all_items` special propery of the first elemtn of the array to apply the same translation from the same field
128
+ Config:
129
+
130
+ ```yaml
131
+ payload:
132
+ countries:
133
+ - $field_for_all_items: countries
134
+ name:
135
+ $field: 'name'
136
+ ```
137
+
138
+ Input:
139
+
140
+ ```json
141
+ {
142
+ "countries": [
143
+ {
144
+ "name": "US",
145
+ "code": "US_CODE"
146
+ },
147
+ {
148
+ "name": "AU",
149
+ "code": "AU_CODE"
150
+ }
151
+ ]
152
+ }
153
+ ```
154
+
155
+ Result:
156
+
157
+ ```json
158
+ {
159
+ "countries": [
160
+ {
161
+ "name": "US"
162
+ },
163
+ {
164
+ "name": "AU"
165
+ }
166
+ ]
167
+ }
168
+ ```
@@ -1,4 +1,5 @@
1
1
  module PayloadTranslator
2
+ class ArrayFieldError < StandardError; end
2
3
  class FieldResolver
3
4
  attr_reader :config, :handlers, :formatters, :configuration, :payload
4
5
 
@@ -11,7 +12,9 @@ module PayloadTranslator
11
12
 
12
13
  def resolve(payload)
13
14
  @payload = payload
14
- if deep_object?
15
+ if is_a_array?
16
+ resolve_array
17
+ elsif deep_object?
15
18
  resolve_deep_object
16
19
  elsif config["$fnc"]
17
20
  resolve_fnc
@@ -50,17 +53,32 @@ module PayloadTranslator
50
53
  end
51
54
  end
52
55
 
53
- def search_value(field_or_fields)
54
- if field_or_fields.is_a?(Array)
55
- field = field_or_fields.find { |field| payload[field] }
56
- payload.fetch(field) { config["$default"] }
57
- elsif field_or_fields =~ /\./
58
- payload.dig(*field_or_fields.split(".")) || config["$default"]
59
- else
60
- payload.fetch(field_or_fields) { config["$default"] }
56
+ def resolve_array_all_items(config)
57
+ @config = config
58
+ [].tap do |result|
59
+ sub_payload = search_value(config["$field_for_all_items"])
60
+ raise ArrayFieldError.new("Field $field_for_all_items should be an Array an is: #{sub_payload}") unless sub_payload.is_a?(Array)
61
+ sub_payload.map.with_index do |sub_payload_item, index|
62
+ field_config = config.reject{|key| key == "$field_for_all_items"}
63
+ result[index] = FieldResolver.new(field_config, configuration).resolve(sub_payload_item)
64
+ end
65
+ end
66
+ end
67
+
68
+ def resolve_array
69
+ return resolve_array_all_items(config.first) if config.first["$field_for_all_items"]
70
+
71
+ [].tap do |result|
72
+ config.each_with_index do |field_config, index|
73
+ result[index] = FieldResolver.new(field_config, configuration).resolve(payload)
74
+ end
61
75
  end
62
76
  end
63
77
 
78
+ def search_value(field_or_fields)
79
+ Payload.search_value(payload, field_or_fields, config["$default"])
80
+ end
81
+
64
82
  def with_formatter
65
83
  return yield unless config["$formatter"]
66
84
  formatter = formatters.fetch(config["$formatter"].to_sym)
@@ -68,6 +86,8 @@ module PayloadTranslator
68
86
  end
69
87
 
70
88
  def fetch_field
89
+ return config.fetch("$default") if !config["$field_fnc"] && !config["$field"]
90
+
71
91
  config.fetch("$field") do
72
92
  call_fnc(config.fetch("$field_fnc"))
73
93
  end
@@ -85,6 +105,10 @@ module PayloadTranslator
85
105
  end
86
106
  end
87
107
 
108
+ def is_a_array?
109
+ config.is_a?(Array)
110
+ end
111
+
88
112
  def deep_object?
89
113
  !config.keys.any? {|key| key =~ /\$/ }
90
114
  end
@@ -0,0 +1,16 @@
1
+ module PayloadTranslator
2
+ class Payload
3
+ def self.fetch_field(payload, field_or_fields)
4
+ return field_or_fields unless field_or_fields.is_a?(Array)
5
+
6
+ field_or_fields.find { |field| search_value(payload, field) }
7
+ end
8
+
9
+ def self.search_value(payload, field, default = nil)
10
+ field = fetch_field(payload, field)
11
+ return payload.dig(*field.split(".")) if field =~ /\./
12
+
13
+ payload.fetch(field, default)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module PayloadTranslator
2
+ class ServiceMultiple < Service
3
+ attr_reader :translators
4
+ def initialize(adapter_configs_or_names, handlers: {}, formatters: {})
5
+ @translators = adapter_configs_or_names.map do |adapter_config_or_name|
6
+ Service.new(adapter_config_or_name, handlers: handlers, formatters: formatters)
7
+ end
8
+ end
9
+
10
+ def translate(payload, &block)
11
+ tranlated_payload = translators[0].translate(payload)
12
+ translators[1].translate(yield(tranlated_payload))
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+
@@ -13,5 +13,5 @@ module PayloadTranslator
13
13
  configuration
14
14
  end
15
15
 
16
- VERSION = "0.2.0"
16
+ VERSION = "0.4.0"
17
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: payload-translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Savignano
@@ -35,7 +35,9 @@ files:
35
35
  - lib/payload_translator.rb
36
36
  - lib/payload_translator/config.rb
37
37
  - lib/payload_translator/field_resolver.rb
38
+ - lib/payload_translator/payload.rb
38
39
  - lib/payload_translator/service.rb
40
+ - lib/payload_translator/service_multiple.rb
39
41
  homepage: https://github.com/devmasx/payload-translator
40
42
  licenses:
41
43
  - MIT