json_data_extractor 0.0.10 → 0.0.11

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: be51d8200f061eb267a9c591934a29072729462d242c349c6057e48cb5e77627
4
- data.tar.gz: 88213a955399a735f2cd15546d9c0b9ad22ba38de8798e9070d5a35e4a47902f
3
+ metadata.gz: aeef2dddd8e11795c682d9db785b955164fbab87e4dc1c63843ba02a76b65905
4
+ data.tar.gz: e17a062f72e0ae56bc09b42ccaa237c12149b667b63dfb0e58a60dd3ea8e5830
5
5
  SHA512:
6
- metadata.gz: f724f3a5b3542644abe1103d8831bdefa0e6bbf2f073d8bfe928cce6f8d42992ecf570527f4adceba4b8a01f12fe0146511ae2ef80301f1a48a52b7e0f5c697a
7
- data.tar.gz: e4318e950ec778eb19558cc1a7da37234f730ba977b42ff19f5be4d5b86c8b7a4b8a7e50a85705fa38c89d41d9cb0f39493d8f6c0bfa1e79215956f2e9817528
6
+ metadata.gz: b574bc9f7c79a4405d7b86739e1bf50e0ccbc82f1016befedf3e5bbb0da040db263492ee8978cd3d83957546d47338980bcfba80770a620a9c637937675bad46
7
+ data.tar.gz: 2a83cf29049a8c4205851a83e12e0867f42ae684564c6bc7ae9e183b4399f9a2390f9be36724f137064bf8e76f74b30d977539fdb1c620972c05a949f19883f2
data/README.md CHANGED
@@ -88,14 +88,16 @@ an optional modifier field that specifies one or more modifiers to apply to the
88
88
  Modifiers are used to transform the data in some way before placing it in the output JSON.
89
89
 
90
90
  Here's an example schema that extracts the authors and categories from a JSON structure similar to
91
- the one used in the previous example (here it's in YAML just for readability):
92
-
93
- ```yaml
94
- schemas:
95
- authors:
96
- path: $.store.book[*].author
97
- modifier: downcase
98
- categories: $..category
91
+ the one used in the previous example:
92
+
93
+ ```json
94
+ {
95
+ "authors": {
96
+ "path": "$.store.book[*].author",
97
+ "modifier": "downcase"
98
+ },
99
+ "categories": "$..category"
100
+ }
99
101
  ```
100
102
 
101
103
  The resulting json will be:
@@ -161,7 +163,8 @@ results = extractor.extract(schema)
161
163
  ```
162
164
 
163
165
  Modifiers are called in the order in which they are defined, so keep that in mind when defining your
164
- schema.
166
+ schema. By default JDE raises an ArgumentError if a modifier is not applicable, but this behaviour
167
+ can be configured to ignore missing modifiers. See Configuration options for details
165
168
 
166
169
  ### Nested schemas
167
170
 
@@ -186,6 +189,26 @@ E.g. this is a valid real-life schema with nested data:
186
189
  }
187
190
  }
188
191
  ```
192
+ Nested schema can be also applied to objects, not arrays. See specs for more examples.
193
+
194
+ ## Configuration Options
195
+ The JsonDataExtractor gem provides a configuration option to control the behavior when encountering invalid modifiers.
196
+
197
+ ### Strict Modifiers
198
+ By default, the gem operates in strict mode, which means that if an invalid modifier is encountered, an `ArgumentError` will be raised. This ensures that only valid modifiers are applied to the extracted data.
199
+
200
+ To change this behavior and allow the use of invalid modifiers without raising an error, you can configure the gem to operate in non-strict mode.
201
+
202
+ ```ruby
203
+ JsonDataExtractor.configure do |config|
204
+ config.strict_modifiers = false
205
+ end
206
+ ```
207
+ When `strict_modifiers` is set to `false`, any invalid modifiers will be ignored, and the original value will be returned without applying any modification.
208
+
209
+ It is important to note that enabling non-strict mode should be done with caution, as it can lead to unexpected behavior if there are typos or incorrect modifiers specified in the schema.
210
+
211
+ By default, `strict_modifiers` is set to `true`, providing a safe and strict behavior. However, you can customize this configuration option according to your specific needs.
189
212
 
190
213
  ## TODO
191
214
 
@@ -1,4 +1,5 @@
1
1
  require 'src/version'
2
+ require 'src/configuration'
2
3
  require 'jsonpath'
3
4
 
4
5
  class JsonDataExtractor
@@ -88,8 +89,21 @@ class JsonDataExtractor
88
89
  modifiers[modifier].call(value)
89
90
  elsif value.respond_to?(modifier)
90
91
  value.send(modifier)
92
+ elsif self.class.configuration.strict_modifiers
93
+ raise ArgumentError, "Invalid modifier: :#{modifier}"
91
94
  else
92
95
  value
93
96
  end
94
97
  end
98
+
99
+
100
+ class << self
101
+ def configuration
102
+ @configuration ||= Configuration.new
103
+ end
104
+
105
+ def configure
106
+ yield(configuration)
107
+ end
108
+ end
95
109
  end
@@ -0,0 +1,9 @@
1
+ class JsonDataExtractor
2
+ class Configuration
3
+ attr_accessor :strict_modifiers
4
+
5
+ def initialize
6
+ @strict_modifiers = true
7
+ end
8
+ end
9
+ end
data/lib/src/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class JsonDataExtractor
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
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.10
4
+ version: 0.0.11
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-05-12 00:00:00.000000000 Z
11
+ date: 2023-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,7 @@ files:
116
116
  - bin/setup
117
117
  - json_data_extractor.gemspec
118
118
  - lib/json_data_extractor.rb
119
+ - lib/src/configuration.rb
119
120
  - lib/src/version.rb
120
121
  homepage: https://github.com/austerlitz/json_data_extractor
121
122
  licenses: