payload-translator 0.2.1 → 0.3.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: 1daf169454e82e6d9ad3687a000be711b8ec35aa8301241da1a9b367f440364f
4
- data.tar.gz: 9cd34aac2ed951140dfbb9be2f0e4eefb449b3b58aa5e48dbfcf30023d748f71
3
+ metadata.gz: 3910cde43ca69d279bc776428ee893c9aaed70dd21d14bcf5f637ec108eef2b3
4
+ data.tar.gz: 8c31590d887d1e68541e735c5ad0d302ecc96050f272a7c93bfcc68ea2926c08
5
5
  SHA512:
6
- metadata.gz: 86e80c6da904c003d38e46dd8d142f460be0bf233fc9466d11aca7904cf127ad1917df38c8642a7a1aec50259b85389b31f8807442fc5644ecf4df7ec0e49979
7
- data.tar.gz: f512af0bfa4c7639bd6335e1ad8d0b312b1457c180231ecc023e77989358ad89f98c806a034adb9fb18770c4084a0f3aaccb66403d287d80bff8e4d32363e5cd
6
+ metadata.gz: dd9cf1d59cff551e7b5d3de3eab163303adcb61531d6c519db4b2e3a9f9b590bd5f9d4062c2744adb597985c3cd7db01a7f040af076b1352abf9373a2f84fe02
7
+ data.tar.gz: 7cf8c7d0cb1dedaa6129148480372a7aa57401d51a2e46a36bcafe43191633e8cf2115defed9dbf0c04e6f0358f34d13f41c6c405d84f04d8f9cda44dd06f783
data/README.md CHANGED
@@ -86,3 +86,68 @@ 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_extenal: {
98
+ "payload" => {
99
+ "id" => { "$field" => "_id" }
100
+ }
101
+ }
102
+ }
103
+ end
104
+ ```
105
+
106
+ ```ruby
107
+ translator = PayloadTranslator::Service.new(:internal_to_extenal)
108
+ ```
109
+
110
+ ### Complex adapters
111
+
112
+ 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
113
+ Config:
114
+
115
+ ```yaml
116
+ payload:
117
+ countries:
118
+ - $field_for_all_items: countries
119
+ name:
120
+ $field: 'name'
121
+ ```
122
+
123
+ Input:
124
+
125
+ ```json
126
+ {
127
+ "countries": [
128
+ {
129
+ "name": "US",
130
+ "code": "US_CODE"
131
+ },
132
+ {
133
+ "name": "AU",
134
+ "code": "AU_CODE"
135
+ }
136
+ ]
137
+ }
138
+ ```
139
+
140
+ Result:
141
+
142
+ ```json
143
+ {
144
+ "countries": [
145
+ {
146
+ "name": "US"
147
+ },
148
+ {
149
+ "name": "AU"
150
+ }
151
+ ]
152
+ }
153
+ ```
@@ -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,6 +53,28 @@ module PayloadTranslator
50
53
  end
51
54
  end
52
55
 
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
75
+ end
76
+ end
77
+
53
78
  def search_value(field_or_fields)
54
79
  Payload.search_value(payload, field_or_fields, config["$default"])
55
80
  end
@@ -61,6 +86,8 @@ module PayloadTranslator
61
86
  end
62
87
 
63
88
  def fetch_field
89
+ return config.fetch("$default") if !config["$field_fnc"] && !config["$field"]
90
+
64
91
  config.fetch("$field") do
65
92
  call_fnc(config.fetch("$field_fnc"))
66
93
  end
@@ -78,6 +105,10 @@ module PayloadTranslator
78
105
  end
79
106
  end
80
107
 
108
+ def is_a_array?
109
+ config.is_a?(Array)
110
+ end
111
+
81
112
  def deep_object?
82
113
  !config.keys.any? {|key| key =~ /\$/ }
83
114
  end
@@ -13,5 +13,5 @@ module PayloadTranslator
13
13
  configuration
14
14
  end
15
15
 
16
- VERSION = "0.2.1"
16
+ VERSION = "0.3.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.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Savignano