payload-translator 0.1.0 → 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: 02ed271a4ef7404262e5cd5379853e948470c28d515d0e4f7ef2bbe7e05bc401
4
- data.tar.gz: 07ca8129f964945d42f033426fb90646560516ddfdfbdc73db0efc131f537605
3
+ metadata.gz: 3910cde43ca69d279bc776428ee893c9aaed70dd21d14bcf5f637ec108eef2b3
4
+ data.tar.gz: 8c31590d887d1e68541e735c5ad0d302ecc96050f272a7c93bfcc68ea2926c08
5
5
  SHA512:
6
- metadata.gz: fb0b462ede9e4bd3b68381a4e08ce963d4f685761e2b35d4871cbb84423511eeaebdf391f8c6055a0aa10649de0cebebeeafad51bfcdada0e2bf7b68615c0f97
7
- data.tar.gz: 59b5335db5bbebeb535e31cdf6c1caec6c4e9464963e2d9a0ea72d1c4dd27221a6ae25b54cd451e6e37ad4845752a6f6ab8edbfc207321f02f5c8dd4c1905b06
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,15 +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
- else
58
- 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
59
75
  end
60
76
  end
61
77
 
78
+ def search_value(field_or_fields)
79
+ Payload.search_value(payload, field_or_fields, config["$default"])
80
+ end
81
+
62
82
  def with_formatter
63
83
  return yield unless config["$formatter"]
64
84
  formatter = formatters.fetch(config["$formatter"].to_sym)
@@ -66,6 +86,8 @@ module PayloadTranslator
66
86
  end
67
87
 
68
88
  def fetch_field
89
+ return config.fetch("$default") if !config["$field_fnc"] && !config["$field"]
90
+
69
91
  config.fetch("$field") do
70
92
  call_fnc(config.fetch("$field_fnc"))
71
93
  end
@@ -83,6 +105,10 @@ module PayloadTranslator
83
105
  end
84
106
  end
85
107
 
108
+ def is_a_array?
109
+ config.is_a?(Array)
110
+ end
111
+
86
112
  def deep_object?
87
113
  !config.keys.any? {|key| key =~ /\$/ }
88
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
@@ -13,5 +13,5 @@ module PayloadTranslator
13
13
  configuration
14
14
  end
15
15
 
16
- VERSION = "0.1.0"
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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Savignano
@@ -35,6 +35,7 @@ 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
39
40
  homepage: https://github.com/devmasx/payload-translator
40
41
  licenses:
@@ -55,8 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
58
  requirements: []
58
- rubyforge_project:
59
- rubygems_version: 2.7.11
59
+ rubygems_version: 3.0.3
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: PayloadTranslator