saxy 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +10 -6
- data/lib/saxy.rb +4 -0
- data/lib/saxy/parser.rb +6 -2
- data/lib/saxy/version.rb +1 -1
- data/spec/fixtures/webstore.xml +3 -3
- data/spec/saxy/parser_spec.rb +6 -1
- metadata +79 -62
data/README.md
CHANGED
@@ -31,34 +31,38 @@ Assume the XML file:
|
|
31
31
|
<product>
|
32
32
|
<name>Kindle - The world's best-selling e-reader.</name>
|
33
33
|
<images>
|
34
|
-
<
|
34
|
+
<thumbSize width="80" height="60">http://amazon.com/kindle_thumb.jpg</thumbSize>
|
35
35
|
</images>
|
36
36
|
</product>
|
37
37
|
<product>
|
38
38
|
<name>Kindle Touch - Simple-to-use touchscreen with built-in WIFI.</name>
|
39
39
|
<images>
|
40
|
-
<
|
40
|
+
<thumbSize width="120" height="90">http://amazon.com/kindle_touch_thumb.jpg</thumbSize>
|
41
41
|
</images>
|
42
42
|
</product>
|
43
43
|
</products>
|
44
44
|
</webstore>
|
45
45
|
|
46
|
-
You instantiate the parser by passing path to XML file and object-identyfing tag name as
|
46
|
+
You instantiate the parser by passing path to XML file and object-identyfing tag name as its arguments.
|
47
47
|
|
48
|
-
The following will parse the XML, find product definitions (inside `<product>` and `</product>` tags), build `OpenStruct`s and yield them inside the block
|
48
|
+
The following will parse the XML, find product definitions (inside `<product>` and `</product>` tags), build `OpenStruct`s and yield them inside the block.
|
49
|
+
Tag attributes become object attributes and attributes' name are underscored.
|
49
50
|
|
50
51
|
Saxy.parse("filename.xml", "product").each do |product|
|
51
52
|
puts product.name
|
52
|
-
puts product.images.
|
53
|
+
puts product.images.thumb_size
|
54
|
+
puts "#{product.images.thumb_size.width}x#{product.images.thumb_size.height}"
|
53
55
|
end
|
54
56
|
|
55
57
|
# =>
|
56
58
|
Kindle - The world's best-selling e-reader.
|
57
59
|
http://amazon.com/kindle_thumb.jpg
|
60
|
+
80x60
|
58
61
|
Kindle Touch - Simple-to-use touchscreen with built-in WIFI.
|
59
62
|
http://amazon.com/kindle_touch_thumb.jpg
|
63
|
+
120x90
|
60
64
|
|
61
|
-
Saxy supports Enumerable, so you can use
|
65
|
+
Saxy supports Enumerable, so you can use its goodies to your comfort without building intermediate arrays:
|
62
66
|
|
63
67
|
Saxy.parse("filename.xml", "product").map do |object|
|
64
68
|
# map OpenStructs to ActiveRecord instances, etc.
|
data/lib/saxy.rb
CHANGED
data/lib/saxy/parser.rb
CHANGED
@@ -26,6 +26,10 @@ module Saxy
|
|
26
26
|
|
27
27
|
if tag == @object_tag || elements.any?
|
28
28
|
elements << Element.new
|
29
|
+
|
30
|
+
attributes.each do |(attr, value)|
|
31
|
+
current_element.set_attribute(attr, value)
|
32
|
+
end
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
@@ -65,8 +69,8 @@ module Saxy
|
|
65
69
|
parser = Nokogiri::XML::SAX::Parser.new(self)
|
66
70
|
parser.parse_file(@xml_file)
|
67
71
|
else
|
68
|
-
(
|
72
|
+
(Saxy.ruby_18? ? Enumerable::Enumerator : Enumerator).new(self, :each)
|
69
73
|
end
|
70
74
|
end
|
71
75
|
end
|
72
|
-
end
|
76
|
+
end
|
data/lib/saxy/version.rb
CHANGED
data/spec/fixtures/webstore.xml
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
<name>Kindle</name>
|
8
8
|
<description>The world's best-selling e-reader.</description>
|
9
9
|
<price>$109</price>
|
10
|
-
<images>
|
10
|
+
<images count="2">
|
11
11
|
<thumb>http://amazon.com/kindle_thumb.jpg</thumb>
|
12
12
|
<large>http://amazon.com/kindle.jpg</large>
|
13
13
|
</images>
|
@@ -17,10 +17,10 @@
|
|
17
17
|
<name>Kindle Touch</name>
|
18
18
|
<description>Simple-to-use touchscreen with built-in WIFI.</description>
|
19
19
|
<price>$79</price>
|
20
|
-
<images>
|
20
|
+
<images count="2">
|
21
21
|
<thumb>http://amazon.com/kindle_touch_thumb.jpg</thumb>
|
22
22
|
<large>http://amazon.com/kindle_touch.jpg</large>
|
23
23
|
</images>
|
24
24
|
</product>
|
25
25
|
</products>
|
26
|
-
</webstore>
|
26
|
+
</webstore>
|
data/spec/saxy/parser_spec.rb
CHANGED
@@ -126,6 +126,11 @@ describe Saxy::Parser do
|
|
126
126
|
parser.characters("bar")
|
127
127
|
parser.end_element("foo")
|
128
128
|
end
|
129
|
+
|
130
|
+
it "should set element's attributes when opening tag with attributes" do
|
131
|
+
parser.start_element("foo", [["bar", "baz"]])
|
132
|
+
parser.current_element.as_object.bar.should == "baz"
|
133
|
+
end
|
129
134
|
end
|
130
135
|
|
131
136
|
it "should raise Saxy::ParsingError on error" do
|
@@ -139,4 +144,4 @@ describe Saxy::Parser do
|
|
139
144
|
it "should return Enumerator when calling #each without a block", :if => RUBY_1_8 do
|
140
145
|
parser.each.should be_instance_of Enumerable::Enumerator
|
141
146
|
end
|
142
|
-
end
|
147
|
+
end
|
metadata
CHANGED
@@ -1,71 +1,90 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: saxy
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.1.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
-
|
7
|
+
authors:
|
8
|
+
- Michał Szajbe
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: activesupport
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
24
22
|
type: :runtime
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: nokogiri
|
28
23
|
prerelease: false
|
29
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: nokogiri
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
30
33
|
none: false
|
31
|
-
requirements:
|
32
|
-
- -
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
35
38
|
type: :runtime
|
36
|
-
version_requirements: *id002
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: rspec
|
39
39
|
prerelease: false
|
40
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
41
49
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
46
54
|
type: :development
|
47
|
-
version_requirements: *id003
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: ZenTest
|
50
55
|
prerelease: false
|
51
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: ZenTest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
52
65
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version:
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
57
70
|
type: :development
|
58
|
-
|
59
|
-
|
60
|
-
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Saxy finds object definitions in XML files and translates them into Ruby
|
79
|
+
objects. It uses SAX parser under the hood, which means that it doesn't load the
|
80
|
+
whole XML file into memory. It goes once though it and yields objects along the
|
81
|
+
way.
|
82
|
+
email:
|
61
83
|
- michal.szajbe@gmail.com
|
62
84
|
executables: []
|
63
|
-
|
64
85
|
extensions: []
|
65
|
-
|
66
86
|
extra_rdoc_files: []
|
67
|
-
|
68
|
-
files:
|
87
|
+
files:
|
69
88
|
- .gitignore
|
70
89
|
- .rspec
|
71
90
|
- .travis.yml
|
@@ -87,32 +106,30 @@ files:
|
|
87
106
|
- spec/spec_helper.rb
|
88
107
|
homepage: http://github.com/monterail/saxy
|
89
108
|
licenses: []
|
90
|
-
|
91
109
|
post_install_message:
|
92
110
|
rdoc_options: []
|
93
|
-
|
94
|
-
require_paths:
|
111
|
+
require_paths:
|
95
112
|
- lib
|
96
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
114
|
none: false
|
98
|
-
requirements:
|
99
|
-
- -
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
version:
|
102
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
120
|
none: false
|
104
|
-
requirements:
|
105
|
-
- -
|
106
|
-
- !ruby/object:Gem::Version
|
107
|
-
version:
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
108
125
|
requirements: []
|
109
|
-
|
110
126
|
rubyforge_project:
|
111
127
|
rubygems_version: 1.8.24
|
112
128
|
signing_key:
|
113
129
|
specification_version: 3
|
114
|
-
summary: Memory-efficient XML parser. Finds object definitions and translates them
|
115
|
-
|
130
|
+
summary: Memory-efficient XML parser. Finds object definitions and translates them
|
131
|
+
into Ruby objects.
|
132
|
+
test_files:
|
116
133
|
- spec/fixtures/webstore.xml
|
117
134
|
- spec/fixtures_helper.rb
|
118
135
|
- spec/saxy/element_spec.rb
|