decontaminate 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/decontaminate/decoder/scalar.rb +9 -5
- data/lib/decontaminate/decontaminator.rb +1 -1
- data/lib/decontaminate/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29c7ecced8931be41ad5db4879c6387d734f7acf
|
4
|
+
data.tar.gz: bb3bf6fe5d28be34cbf82853ebb211aa52cf41b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c675f357de093cef4949c21ad4456128ad6ed9464b408e5960a3676b515425b6bff5e9bb86b53537a3c5d33067001b79bbb52f37a7c875d76f0e0768218f5143
|
7
|
+
data.tar.gz: 1b8f16c664a0b38864f2b3511f8d3bf01f7d840b524ddbc0b36568dcb7a5e68540e15d187b843a1a61fb3c6b2bd0d34e24558e7dfbc43024869465d477f3b823
|
data/README.md
CHANGED
@@ -85,6 +85,16 @@ end
|
|
85
85
|
|
86
86
|
The `hash` method accepts a block, which works just like the class body, but all paths are scoped to the path passed to `hash`. The `key` argument is optional, just like with `scalar`.
|
87
87
|
|
88
|
+
Sometimes it may be useful to create an additional hash in the output as an organizational tool, even though there is no equivalent nesting in the input XML. In this case, the XPath argument may be omitted, specifying only `key:`.
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
hash key: 'info' do
|
92
|
+
scalar 'Email'
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
This will fetch a value from the `Email` node on the root, but it will be stored in a property within a separate hash, keyed in the result with `'info'`.
|
97
|
+
|
88
98
|
### Array Data
|
89
99
|
|
90
100
|
In addition to the `scalar` and `hash` methods, there are plural forms which allow parsing and extracting data that appears many times within a single document. These are named `scalars` and `hashes`, respectively. They work much like their singular counterparts, but the provided path should match multiple elements.
|
@@ -10,17 +10,17 @@ module Decontaminate
|
|
10
10
|
|
11
11
|
def decode(xml_node)
|
12
12
|
child = xml_node.at_xpath xpath
|
13
|
-
text = coerce_node_to_text
|
14
|
-
|
15
|
-
return unless text
|
13
|
+
text = child && coerce_node_to_text(child)
|
16
14
|
|
17
15
|
case type
|
18
16
|
when :string
|
19
17
|
text
|
20
18
|
when :integer
|
21
|
-
text.to_i
|
19
|
+
text && text.to_i
|
22
20
|
when :float
|
23
|
-
text.to_f
|
21
|
+
text && text.to_f
|
22
|
+
when :boolean
|
23
|
+
coerce_string_to_boolean text
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -33,6 +33,10 @@ module Decontaminate
|
|
33
33
|
node.at_xpath('text()').to_s
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
def coerce_string_to_boolean(str)
|
38
|
+
str == 'true' || str == '1'
|
39
|
+
end
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
@@ -35,7 +35,7 @@ module Decontaminate
|
|
35
35
|
add_decoder key, decoder
|
36
36
|
end
|
37
37
|
|
38
|
-
def hash(xpath, key: infer_key(xpath), &body)
|
38
|
+
def hash(xpath = '.', key: infer_key(xpath), &body)
|
39
39
|
decontaminator = Class.new(Decontaminate::Decontaminator, &body)
|
40
40
|
add_decoder key, Decontaminate::Decoder::Hash.new(xpath, decontaminator)
|
41
41
|
end
|