json_data_extractor 0.0.12 → 0.0.13

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: 0b205c1e3094b426d39161a47261d0d0168244cff62b3c99b557d7f0e286a322
4
- data.tar.gz: 3bf7cc322309d9e0c7bb8997eaae41346fbb97e42280a78b5e663c8307ec5311
3
+ metadata.gz: bb96ee66beec96f117282087fadea7c8bf91febb79eddf8d588bfb26a4301466
4
+ data.tar.gz: b0a5221e3eb012da640233fac6fb5ff6a41715327de6cb381bd8896e184095bd
5
5
  SHA512:
6
- metadata.gz: 7973f7ca4b7575333282cbb0033f6d326b30003b8c36a7f9b7f0be4e7ee6194bbeb712e5abe2446d77e8ab8ffd5b7bf48c9d125a280da4c7d54aa62d11d2c0a8
7
- data.tar.gz: 23507463438d1a106da73c0b561912ac16963aa49160563d876883586792c89ca2a2b0531ab236fffb11a45dc2eba0573f012727b28b518cb72ea3b7db35d558
6
+ metadata.gz: 48e16964d90fc098e3d2b1c9ad5136f642546a9fa04121aea8b2ed28847224fe85ee43a576476e61fbcdf501e0ae188488c23cdd1bab6bf9d1af9781d301df60
7
+ data.tar.gz: 8b7fd89bbd4ecd36b24a542112da82baeccc789e8cf86b8ce8cbbbf3444d28c2073809ba105cb96f261d88c847ca74ba31d8cbeacfe95d74d79a22b2af913907
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
 
data/lib/src/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class JsonDataExtractor
2
- VERSION = '0.0.12'
2
+ VERSION = '0.0.13'
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.13
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-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler