json_data_extractor 0.0.12 → 0.0.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b205c1e3094b426d39161a47261d0d0168244cff62b3c99b557d7f0e286a322
4
- data.tar.gz: 3bf7cc322309d9e0c7bb8997eaae41346fbb97e42280a78b5e663c8307ec5311
3
+ metadata.gz: 2a35a6bf0ed27b3f5a0ef9a13e96ff21ac28ff873772696aef65069821dbf23e
4
+ data.tar.gz: 9dba998316601c445cebe5872f04e18c68575196b9ed52e4128f01b47139049a
5
5
  SHA512:
6
- metadata.gz: 7973f7ca4b7575333282cbb0033f6d326b30003b8c36a7f9b7f0be4e7ee6194bbeb712e5abe2446d77e8ab8ffd5b7bf48c9d125a280da4c7d54aa62d11d2c0a8
7
- data.tar.gz: 23507463438d1a106da73c0b561912ac16963aa49160563d876883586792c89ca2a2b0531ab236fffb11a45dc2eba0573f012727b28b518cb72ea3b7db35d558
6
+ metadata.gz: 204620707292cb9e1f805db3bbc72b44e6dca7f1dc86dceefa8b9b1126f437fa70bc152af38bb502741d620884f141b46664a6a6c80fb135bc20f394b8d62300
7
+ data.tar.gz: '0482bfd00032c8e07c0a97ce88c6bbfba824bc6ea7eb6ec7aecd329c92982afae2853333f096695d68f52f737a01cca253a9f199616a22e565211b60b9d0f6eb'
data/README.md CHANGED
@@ -120,6 +120,35 @@ The resulting json will be:
120
120
 
121
121
  ```
122
122
 
123
+
124
+ ### Handling Default Values
125
+
126
+ With JsonDataExtractor, you can specify default values in your schema for keys that might be absent in the input JSON. Use the `path` and `default` keys in the schema for this purpose.
127
+
128
+ ```ruby
129
+ schema = {
130
+ absent_value: { path: nil },
131
+ default: { path: '$.some_real_path', default: 'foo' },
132
+ default_with_lambda: { path: '$.table', default: -> { 'DEFAULT' } },
133
+ absent_with_default: { path: nil, default: 'bar' }
134
+ }
135
+ ```
136
+
137
+ - `absent_value`: Will be `nil` in the output as there's no corresponding key in the input JSON and no default is provided.
138
+ - `default`: Will either take the value from `$.some_real_path` in the input JSON or 'foo' if the path does not exist.
139
+ - `default_with_lambda`: Will take the value from `$..table` in the input JSON or 'DEFAULT' if the path does not exist.
140
+ - `absent_with_default`: Will be 'bar' in the output as there's no corresponding key in the input JSON but a default is provided.
141
+
142
+ #### Simplified Syntax for Absent Values
143
+
144
+ For keys that you expect to be absent in the input JSON but still want to include in the output with a `nil` value, you can use a simplified syntax by setting the schema value to `nil`.
145
+
146
+ ```ruby
147
+ schema = {
148
+ absent_value: nil
149
+ }
150
+ ```
151
+
123
152
  ### Modifiers
124
153
 
125
154
  Modifiers can be supplied on object creation and/or added later by calling `#add_modifier` method.
@@ -191,7 +220,7 @@ schema = {
191
220
  map: FUEL_TYPES
192
221
  }
193
222
  }
194
- result = described_class.new(data).extract(schema) # => {"fuel":["Petrol","Diesel",nil,"Unknown"]}
223
+ result = JsonDataExtractor.new(data).extract(schema) # => {"fuel":["Petrol","Diesel",nil,"Unknown"]}
195
224
  ```
196
225
 
197
226
  A map is essentially a dictionary that defines key-value pairs, where the keys represent the source
@@ -20,9 +20,11 @@ class JsonDataExtractor
20
20
  def extract(schema)
21
21
  results = {}
22
22
  schema.each do |key, val|
23
+ default_value = nil
23
24
  if val.is_a?(Hash)
24
25
  val.transform_keys!(&:to_sym)
25
26
  path = val[:path]
27
+ default_value = val[:default]
26
28
  maps = Array([val[:maps] || val[:map]]).flatten.compact.map do |map|
27
29
  if map.is_a?(Hash)
28
30
  map
@@ -48,11 +50,12 @@ class JsonDataExtractor
48
50
  maps = []
49
51
  end
50
52
 
51
- extracted_data = JsonPath.on(@data, path)
53
+ extracted_data = JsonPath.on(@data, path) if path
52
54
 
53
- if extracted_data.empty?
54
- results[key] = nil
55
+ if extracted_data.nil? || extracted_data.empty?
56
+ results[key] = default_value.is_a?(Proc) ? default_value.call : (default_value || nil)
55
57
  else
58
+ extracted_data.map! { |val| val.nil? ? default_value : val }
56
59
  transformed_data = apply_modifiers(extracted_data, modifiers)
57
60
  results[key] = apply_maps(transformed_data, maps)
58
61
 
@@ -107,7 +110,7 @@ class JsonDataExtractor
107
110
  elsif value.respond_to?(modifier)
108
111
  value.send(modifier)
109
112
  elsif self.class.configuration.strict_modifiers
110
- raise ArgumentError, "Invalid modifier: :#{modifier}"
113
+ raise ArgumentError, "Modifier: <:#{modifier}> cannot be applied to value <#{value.inspect}>"
111
114
  else
112
115
  value
113
116
  end
data/lib/src/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class JsonDataExtractor
2
- VERSION = '0.0.12'
2
+ VERSION = '0.0.14'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_data_extractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Buslaev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-07 00:00:00.000000000 Z
11
+ date: 2023-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler