json_data_extractor 0.1.02 → 0.1.03
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -25
- data/lib/json_data_extractor/extractor.rb +7 -2
- data/lib/json_data_extractor/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 295cd6c872605a86b0656c236dd11ec26deb323aaea9f6a07478871f52fa5950
|
4
|
+
data.tar.gz: daad530af7cdb9880d28f92c8f0235c0f095466974fa6ed61d6d46e1113882f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a146df922bbe2e84b9ad7a7cffdc64f1f1fe9be243764049a41a5c13ebb7ea31ddd07525e628056b884e5ad53c3ef3780cbe3c5fed6dc46b23f695534b6d4188
|
7
|
+
data.tar.gz: ef3b8d6ff6589f72e2955c43b95ffea59459b4f7d524fbd7513c7133a540ec27be4257ffaefa08137e13abb131aa0e55a6fdec42df769cca6d44361768815532
|
data/README.md
CHANGED
@@ -146,54 +146,76 @@ schema = {
|
|
146
146
|
absent_value: nil
|
147
147
|
}
|
148
148
|
```
|
149
|
-
|
150
149
|
### Modifiers
|
151
150
|
|
152
|
-
Modifiers can be supplied on object creation and/or added later by calling `#add_modifier` method.
|
153
|
-
|
154
|
-
Modifiers
|
155
|
-
|
151
|
+
Modifiers can be supplied on object creation and/or added later by calling the `#add_modifier` method. Modifiers allow you to perform transformations on the extracted data before it is returned. They are useful for cleaning up data, formatting it, or applying any custom logic.
|
152
|
+
|
153
|
+
Modifiers can now be defined in several ways:
|
154
|
+
|
155
|
+
1. **By providing a symbol**: This symbol should correspond to the name of a method (e.g., `:to_i`) that will be called on each extracted value.
|
156
|
+
2. **By providing an anonymous lambda or block**: Use a lambda or block to define the transformation logic inline.
|
157
|
+
3. **By providing any callable object**: A class or object that implements a `call` method can be used as a modifier. This makes it flexible to use pre-defined classes, lambdas, or procs.
|
156
158
|
|
157
|
-
|
158
|
-
or lambda that should be called on each extracted value, or by providing an anonymous lambda. Here's
|
159
|
-
an example schema that uses both types of modifiers:
|
159
|
+
Here’s an example schema showcasing the use of modifiers:
|
160
160
|
|
161
161
|
```ruby
|
162
162
|
schema = {
|
163
|
-
name: '$.name',
|
164
|
-
age: { path: '$.age', modifier: :to_i },
|
165
|
-
email: {
|
163
|
+
name: '$.name', # Extract as-is
|
164
|
+
age: { path: '$.age', modifier: :to_i }, # Apply the `to_i` method
|
165
|
+
email: {
|
166
|
+
path: '$.contact.email',
|
167
|
+
modifiers: [
|
168
|
+
:downcase,
|
169
|
+
->(email) { email.gsub(/\s/, '') } # Lambda to remove whitespace
|
170
|
+
]
|
171
|
+
}
|
166
172
|
}
|
167
|
-
|
168
173
|
```
|
169
174
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
+
- **Name**: The value is simply extracted as-is.
|
176
|
+
- **Age**: The extracted value is converted to an integer using the `to_i` method.
|
177
|
+
- **Email**:
|
178
|
+
1. The value is transformed to lowercase using `downcase`.
|
179
|
+
2. Whitespace is removed using an anonymous lambda.
|
180
|
+
|
181
|
+
#### Defining Custom Modifiers
|
175
182
|
|
176
|
-
You can
|
177
|
-
JsonDataExtractor instance:
|
183
|
+
You can define your own custom modifiers using `add_modifier`. A modifier can be defined using a block, a lambda, or any callable object (such as a class that implements `call`):
|
178
184
|
|
179
185
|
```ruby
|
186
|
+
# Using a block
|
180
187
|
extractor = JsonDataExtractor.new(json_data)
|
181
188
|
extractor.add_modifier(:remove_newlines) { |value| value.gsub("\n", '') }
|
182
189
|
|
190
|
+
# Using a class with a `call` method
|
191
|
+
class ReverseString
|
192
|
+
def call(value)
|
193
|
+
value.reverse
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
extractor.add_modifier(:reverse_string, ReverseString.new)
|
198
|
+
|
199
|
+
# Lambda example
|
200
|
+
capitalize = ->(value) { value.capitalize }
|
201
|
+
extractor.add_modifier(:capitalize, capitalize)
|
202
|
+
|
203
|
+
# Apply these modifiers in a schema
|
183
204
|
schema = {
|
184
205
|
name: 'name',
|
185
|
-
bio: { path: 'bio', modifiers: [:remove_newlines] }
|
206
|
+
bio: { path: 'bio', modifiers: [:remove_newlines, :reverse_string] },
|
207
|
+
category: { path: 'category', modifier: :capitalize }
|
186
208
|
}
|
187
209
|
|
210
|
+
# Extract data
|
188
211
|
results = extractor.extract(schema)
|
189
|
-
|
190
212
|
```
|
191
213
|
|
192
|
-
|
214
|
+
#### Modifier Order
|
215
|
+
|
216
|
+
Modifiers are called in the order in which they are defined. Keep this in mind when chaining multiple modifiers for complex transformations. For example, if you want to first format a string and then clean it up (or vice versa), define the order accordingly.
|
193
217
|
|
194
|
-
|
195
|
-
schema. By default JDE raises an ArgumentError if a modifier is not applicable, but this behaviour
|
196
|
-
can be configured to ignore missing modifiers. See Configuration options for details
|
218
|
+
You can also configure the behavior of modifiers. By default, JDE raises an `ArgumentError` if a modifier cannot be applied to the extracted value. However, this strict behavior can be configured to ignore such errors. See the **Configuration** section for more details.
|
197
219
|
|
198
220
|
### Maps
|
199
221
|
|
@@ -14,9 +14,14 @@ module JsonDataExtractor
|
|
14
14
|
end
|
15
15
|
|
16
16
|
# @param modifier_name [String, Symbol]
|
17
|
-
|
17
|
+
# @param callable [#call, nil] Optional callable object
|
18
|
+
def add_modifier(modifier_name, callable = nil, &block)
|
18
19
|
modifier_name = modifier_name.to_sym unless modifier_name.is_a?(Symbol)
|
19
|
-
modifiers[modifier_name] = block
|
20
|
+
modifiers[modifier_name] = callable || block
|
21
|
+
|
22
|
+
return if modifiers[modifier_name].respond_to?(:call)
|
23
|
+
|
24
|
+
raise ArgumentError, 'Modifier must be a callable object or a block'
|
20
25
|
end
|
21
26
|
|
22
27
|
# @param schema [Hash] schema of the expected data mapping
|
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.1.
|
4
|
+
version: 0.1.03
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Buslaev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|