saxy 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/CHANGELOG.md +4 -0
- data/README.md +7 -7
- data/lib/saxy/element.rb +5 -5
- data/lib/saxy/version.rb +1 -1
- data/spec/saxy/element_spec.rb +6 -6
- data/spec/saxy/parser_spec.rb +1 -1
- data/spec/saxy_spec.rb +15 -15
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 145b19ea70344eb97cb37dab58b172d07766a654
|
4
|
+
data.tar.gz: 9b0a3482befc1f5270f8bfe5665a68b4cc0a87f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 229a5ceede847656383a9b86a8521d17030c97329233cc36a0a32f6beceb2efcacff06a9e2d030a05c81014f41b9dc3f7c31f83b5cbce933827136149e48bab8
|
7
|
+
data.tar.gz: dd980f2f3fc39731e19776316bde8f16abb8f28be32d7f036cfca65db6867fa7dd1d7362360a96e3759b2f389a6e48e6aa3ab88df568ebfb3334174e9492af6c
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -3,9 +3,9 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/saxy.svg)](https://badge.fury.io/rb/saxy)
|
4
4
|
[![Build Status](https://api.travis-ci.org/humante/saxy.svg)](http://travis-ci.org/humante/saxy)
|
5
5
|
|
6
|
-
Memory-efficient XML parser. Finds object definitions in XML and translates them into Ruby
|
6
|
+
Memory-efficient XML parser. Finds object definitions in XML and translates them into Ruby hashes.
|
7
7
|
|
8
|
-
It uses SAX parser (provided by Nokogiri gem) under the hood, which means that it doesn't load the whole XML file into memory. It goes once through it and yields
|
8
|
+
It uses SAX parser (provided by Nokogiri gem) under the hood, which means that it doesn't load the whole XML file into memory. It goes once through it and yields hashes along the way.
|
9
9
|
|
10
10
|
In result the memory footprint of the parser remains small and more or less constant irrespective of the size of the XML file, be it few KB or hundreds of GB.
|
11
11
|
|
@@ -96,9 +96,9 @@ Usage with a file path:
|
|
96
96
|
|
97
97
|
````ruby
|
98
98
|
Saxy.parse("filename.xml", "product").each do |product|
|
99
|
-
puts product[
|
100
|
-
puts product[
|
101
|
-
puts "#{product[
|
99
|
+
puts product["name"]
|
100
|
+
puts product["images"]["thumb_size"]["contents"]
|
101
|
+
puts "#{product["images"]["thumb_size"]["width"]}x#{product["images"]["thumb_size"]["height"]}"
|
102
102
|
end
|
103
103
|
|
104
104
|
# =>
|
@@ -115,7 +115,7 @@ Usage with an IO-like object `ARGF` or `$stdin`:
|
|
115
115
|
````ruby
|
116
116
|
# > cat filename.xml | ruby this_script.rb
|
117
117
|
Saxy.parse(ARGF, "product").each do |product|
|
118
|
-
puts product
|
118
|
+
puts product["name"]
|
119
119
|
end
|
120
120
|
|
121
121
|
# =>
|
@@ -141,7 +141,7 @@ Multiple definitions of child objects are grouped in arrays:
|
|
141
141
|
|
142
142
|
````ruby
|
143
143
|
webstore = Saxy.parse("filename.xml", "webstore").first
|
144
|
-
webstore[
|
144
|
+
webstore["products"]["product"].size # => 2
|
145
145
|
````
|
146
146
|
|
147
147
|
## Debugging
|
data/lib/saxy/element.rb
CHANGED
@@ -23,23 +23,23 @@ module Saxy
|
|
23
23
|
def to_h
|
24
24
|
return value unless attributes.any?
|
25
25
|
data = attributes.reduce({}) do |memo, (name, value)|
|
26
|
-
memo[name
|
26
|
+
memo[name] = value.size == 1 ? value.first : value
|
27
27
|
memo
|
28
28
|
end
|
29
|
-
data[
|
29
|
+
data["contents"] = value
|
30
30
|
data
|
31
31
|
end
|
32
32
|
|
33
33
|
def attribute_name(name)
|
34
|
-
underscore(name)
|
34
|
+
underscore(name)
|
35
35
|
end
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
39
|
def underscore(word)
|
40
40
|
word = word.dup
|
41
|
-
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
42
|
-
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
41
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
42
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
43
43
|
word.tr!("-", "_")
|
44
44
|
word.downcase!
|
45
45
|
word
|
data/lib/saxy/version.rb
CHANGED
data/spec/saxy/element_spec.rb
CHANGED
@@ -23,8 +23,8 @@ describe Saxy::Element do
|
|
23
23
|
expect(element).to receive(:attributes).at_least(:once).and_return("foo" => 1, "bar" => 2)
|
24
24
|
object = element.to_h
|
25
25
|
|
26
|
-
expect(object[
|
27
|
-
expect(object[
|
26
|
+
expect(object["foo"]).to eq(1)
|
27
|
+
expect(object["bar"]).to eq(2)
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should dump as object with value when attributes and contents are set" do
|
@@ -32,18 +32,18 @@ describe Saxy::Element do
|
|
32
32
|
element.append_value("value")
|
33
33
|
object = element.to_h
|
34
34
|
|
35
|
-
expect(object[
|
36
|
-
expect(object[
|
35
|
+
expect(object["foo"]).to eq("bar")
|
36
|
+
expect(object["contents"]).to eq("value")
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should add attributes under underscored names" do
|
40
40
|
element.set_attribute("FooBar", "baz")
|
41
|
-
expect(element.to_h[
|
41
|
+
expect(element.to_h["foo_bar"]).to eq("baz")
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should create array if adding multiple attributtes with the same name" do
|
45
45
|
element.set_attribute("foo", "bar")
|
46
46
|
element.set_attribute("foo", "baz")
|
47
|
-
expect(element.to_h[
|
47
|
+
expect(element.to_h["foo"]).to eq(["bar", "baz"])
|
48
48
|
end
|
49
49
|
end
|
data/spec/saxy/parser_spec.rb
CHANGED
@@ -161,7 +161,7 @@ describe Saxy::Parser do
|
|
161
161
|
|
162
162
|
it "should set element's attributes when opening tag with attributes" do
|
163
163
|
parser.start_element("foo", [["bar", "baz"]])
|
164
|
-
expect(parser.current_element.to_h[
|
164
|
+
expect(parser.current_element.to_h["bar"]).to eq("baz")
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
data/spec/saxy_spec.rb
CHANGED
@@ -9,26 +9,26 @@ describe Saxy do
|
|
9
9
|
arr
|
10
10
|
end
|
11
11
|
|
12
|
-
expect(products[0][
|
13
|
-
expect(products[0][
|
14
|
-
expect(products[0][
|
15
|
-
expect(products[0][
|
16
|
-
expect(products[0][
|
17
|
-
expect(products[0][
|
18
|
-
|
19
|
-
expect(products[1][
|
20
|
-
expect(products[1][
|
21
|
-
expect(products[1][
|
22
|
-
expect(products[1][
|
23
|
-
expect(products[1][
|
24
|
-
expect(products[1][
|
12
|
+
expect(products[0]["uid"]).to eq("FFCF177")
|
13
|
+
expect(products[0]["name"]).to eq("Kindle")
|
14
|
+
expect(products[0]["description"]).to eq("The world's best-selling e-reader.")
|
15
|
+
expect(products[0]["price"]).to eq("$109")
|
16
|
+
expect(products[0]["images"]["thumb"]).to eq("http://amazon.com/kindle_thumb.jpg")
|
17
|
+
expect(products[0]["images"]["large"]).to eq("http://amazon.com/kindle.jpg")
|
18
|
+
|
19
|
+
expect(products[1]["uid"]).to eq("YD26NT")
|
20
|
+
expect(products[1]["name"]).to eq("Kindle Touch")
|
21
|
+
expect(products[1]["description"]).to eq("Simple-to-use touchscreen with built-in WIFI.")
|
22
|
+
expect(products[1]["price"]).to eq("$79")
|
23
|
+
expect(products[1]["images"]["thumb"]).to eq("http://amazon.com/kindle_touch_thumb.jpg")
|
24
|
+
expect(products[1]["images"]["large"]).to eq("http://amazon.com/kindle_touch.jpg")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should group multiple definitions of child objects into arrays" do
|
28
28
|
webstore = Saxy.parse(fixture_file("webstore.xml"), "webstore").first
|
29
29
|
|
30
|
-
expect(webstore[
|
31
|
-
expect(webstore[
|
30
|
+
expect(webstore["products"]["product"]).to be_an(Array)
|
31
|
+
expect(webstore["products"]["product"].size).to eq(2)
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should return Enumerator when calling #parse without a block" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michał Szajbe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|