saxy 0.4.0 → 0.5.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.
- checksums.yaml +4 -4
- data/.travis.yml +4 -1
- data/README.md +23 -10
- data/lib/saxy.rb +3 -2
- data/lib/saxy/element.rb +8 -13
- data/lib/saxy/parser.rb +1 -1
- data/lib/saxy/version.rb +1 -1
- data/saxy.gemspec +2 -3
- data/spec/saxy/element_spec.rb +9 -9
- data/spec/saxy/parser_spec.rb +3 -2
- data/spec/saxy_spec.rb +14 -14
- metadata +5 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e902b0cd867994419869833857f4c96bb6427f17
|
4
|
+
data.tar.gz: f4e5babf7a86675953869fbec446404fceb9ea0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aebfd829d32a73141a4c289cc4f557ed6c2c647248b659bbc037d9bca1e58196394dd42c9439841262062c29dfb49af953ce7e9cde99179d9ce5e1be2c87962d
|
7
|
+
data.tar.gz: c3d2f658bae270cc76da97881037fb598eb2b254760f349c304fc6943409c9fa1913d2cb65d3bada50c223354e49eed2a4183b5869f7d8b0c7a1debadfc08954
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -9,11 +9,9 @@ It uses SAX parser under the hood, which means that it doesn't load the whole XM
|
|
9
9
|
|
10
10
|
## Installation
|
11
11
|
|
12
|
-
This version supports only ruby 1.9.2, it is not maintained anymore. See master branch if you're looking for support of different ruby versions.
|
13
|
-
|
14
12
|
Add this line to your application's Gemfile:
|
15
13
|
|
16
|
-
gem 'saxy'
|
14
|
+
gem 'saxy'
|
17
15
|
|
18
16
|
And then execute:
|
19
17
|
|
@@ -21,7 +19,22 @@ And then execute:
|
|
21
19
|
|
22
20
|
Or install it yourself as:
|
23
21
|
|
24
|
-
$ gem install saxy
|
22
|
+
$ gem install saxy
|
23
|
+
|
24
|
+
As of `0.5.0` version `saxy` requires ruby 1.9.3 or higher. Previous versions of the gem work with ruby 1.8 and 1.9.2 (see below), but they are not maintained anymore.
|
25
|
+
|
26
|
+
## Ruby 1.8 support
|
27
|
+
|
28
|
+
See `ruby-1.8` branch. Install with:
|
29
|
+
|
30
|
+
gem 'saxy', '~> 0.3.0'
|
31
|
+
|
32
|
+
## Ruby 1.9.2 support
|
33
|
+
|
34
|
+
See `ruby-1.9.2` branch. Install with:
|
35
|
+
|
36
|
+
gem 'saxy', '~> 0.4.0'
|
37
|
+
|
25
38
|
|
26
39
|
## Usage
|
27
40
|
|
@@ -48,15 +61,15 @@ Assume the XML file:
|
|
48
61
|
|
49
62
|
You instantiate the parser by passing path to XML file or an IO-like object and object-identyfing tag name as its arguments.
|
50
63
|
|
51
|
-
The following will parse the XML, find product definitions (inside `<product>` and `</product>` tags), build `
|
64
|
+
The following will parse the XML, find product definitions (inside `<product>` and `</product>` tags), build `Hash`s and yield them inside the block.
|
52
65
|
Tag attributes become object attributes and attributes' name are underscored.
|
53
66
|
|
54
67
|
Usage with a file path:
|
55
68
|
|
56
69
|
Saxy.parse("filename.xml", "product").each do |product|
|
57
|
-
puts product
|
58
|
-
puts product
|
59
|
-
puts "#{product
|
70
|
+
puts product[:name]
|
71
|
+
puts product[:images][:thumb_size][:contents]
|
72
|
+
puts "#{product[:images][:thumb_size][:width]}x#{product[:images][:thumb_size][:height]}"
|
60
73
|
end
|
61
74
|
|
62
75
|
# =>
|
@@ -80,7 +93,7 @@ Usage with an IO-like object `ARGF`:
|
|
80
93
|
Saxy supports Enumerable, so you can use its goodies to your comfort without building intermediate arrays:
|
81
94
|
|
82
95
|
Saxy.parse("filename.xml", "product").map do |object|
|
83
|
-
# map
|
96
|
+
# map yielded Hash to ActiveRecord instances, etc.
|
84
97
|
end
|
85
98
|
|
86
99
|
You can also grab an Enumerator for external use (e.g. lazy evaluation, etc.):
|
@@ -91,7 +104,7 @@ You can also grab an Enumerator for external use (e.g. lazy evaluation, etc.):
|
|
91
104
|
Multiple definitions of child objects are grouped in arrays:
|
92
105
|
|
93
106
|
webstore = Saxy.parse("filename.xml", "webstore").first
|
94
|
-
webstore
|
107
|
+
webstore[:products][:product].size # => 2
|
95
108
|
|
96
109
|
## Contributing
|
97
110
|
|
data/lib/saxy.rb
CHANGED
@@ -2,8 +2,8 @@ require 'saxy/version'
|
|
2
2
|
|
3
3
|
module Saxy
|
4
4
|
class << self
|
5
|
-
def parse(
|
6
|
-
parser = Parser.new(
|
5
|
+
def parse(object, object_tag, &blk)
|
6
|
+
parser = Parser.new(object, object_tag)
|
7
7
|
|
8
8
|
if blk
|
9
9
|
parser.each(blk)
|
@@ -11,6 +11,7 @@ module Saxy
|
|
11
11
|
parser.each
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
data/lib/saxy/element.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'active_support/core_ext/string/inflections'
|
2
|
-
require 'ostruct'
|
3
2
|
|
4
3
|
module Saxy
|
5
4
|
class Element
|
@@ -23,22 +22,18 @@ module Saxy
|
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
object.send("#{name}=", value)
|
32
|
-
end
|
33
|
-
object.contents = value
|
34
|
-
object
|
35
|
-
else
|
36
|
-
value
|
25
|
+
def to_h
|
26
|
+
return value unless attributes.any?
|
27
|
+
data = attributes.reduce({}) do |memo, (name, value)|
|
28
|
+
memo[name.to_sym] = value.size == 1 ? value.first : value
|
29
|
+
memo
|
37
30
|
end
|
31
|
+
data[:contents] = value
|
32
|
+
data
|
38
33
|
end
|
39
34
|
|
40
35
|
def attribute_name(name)
|
41
|
-
name.underscore
|
36
|
+
name.underscore.to_sym
|
42
37
|
end
|
43
38
|
end
|
44
39
|
end
|
data/lib/saxy/parser.rb
CHANGED
data/lib/saxy/version.rb
CHANGED
data/saxy.gemspec
CHANGED
@@ -15,10 +15,9 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Saxy::VERSION
|
17
17
|
|
18
|
-
gem.required_ruby_version =
|
18
|
+
gem.required_ruby_version = ">= 1.9.3"
|
19
19
|
|
20
|
-
gem.add_dependency "activesupport"
|
21
|
-
gem.add_dependency "i18n", "< 0.7.0"
|
20
|
+
gem.add_dependency "activesupport"
|
22
21
|
gem.add_dependency "nokogiri"
|
23
22
|
gem.add_development_dependency "rspec"
|
24
23
|
end
|
data/spec/saxy/element_spec.rb
CHANGED
@@ -16,34 +16,34 @@ describe Saxy::Element do
|
|
16
16
|
|
17
17
|
it "should dump as string when no attributes are set" do
|
18
18
|
element.stub(:value).and_return("foo")
|
19
|
-
element.
|
19
|
+
element.to_h.should == "foo"
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should dump as object when attributes are set" do
|
23
23
|
element.stub(:attributes).and_return("foo" => 1, "bar" => 2)
|
24
|
-
object = element.
|
24
|
+
object = element.to_h
|
25
25
|
|
26
|
-
object
|
27
|
-
object
|
26
|
+
object[:foo].should == 1
|
27
|
+
object[:bar].should == 2
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should dump as object with value when attributes and contents are set" do
|
31
31
|
element.set_attribute("foo", "bar")
|
32
32
|
element.append_value("value")
|
33
|
-
object = element.
|
33
|
+
object = element.to_h
|
34
34
|
|
35
|
-
object
|
36
|
-
object
|
35
|
+
object[:foo].should == "bar"
|
36
|
+
object[:contents].should == "value"
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should add attributes under underscored names" do
|
40
40
|
element.set_attribute("FooBar", "baz")
|
41
|
-
element.
|
41
|
+
element.to_h[:foo_bar].should == "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
|
-
element.
|
47
|
+
element.to_h[:foo].should == ["bar", "baz"]
|
48
48
|
end
|
49
49
|
end
|
data/spec/saxy/parser_spec.rb
CHANGED
@@ -112,7 +112,7 @@ describe Saxy::Parser do
|
|
112
112
|
end
|
113
113
|
|
114
114
|
it "should yield the object inside the callback after detecting object tag closing" do
|
115
|
-
@callback.should_receive(:call).with(parser.current_element.
|
115
|
+
@callback.should_receive(:call).with(parser.current_element.to_h)
|
116
116
|
parser.end_element("product")
|
117
117
|
end
|
118
118
|
|
@@ -147,7 +147,7 @@ describe Saxy::Parser do
|
|
147
147
|
|
148
148
|
it "should set element's attributes when opening tag with attributes" do
|
149
149
|
parser.start_element("foo", [["bar", "baz"]])
|
150
|
-
parser.current_element.
|
150
|
+
parser.current_element.to_h[:bar].should == "baz"
|
151
151
|
end
|
152
152
|
end
|
153
153
|
|
@@ -158,4 +158,5 @@ describe Saxy::Parser do
|
|
158
158
|
it "should return Enumerator when calling #each without a block" do
|
159
159
|
parser.each.should be_instance_of Enumerator
|
160
160
|
end
|
161
|
+
|
161
162
|
end
|
data/spec/saxy_spec.rb
CHANGED
@@ -9,26 +9,26 @@ describe Saxy do
|
|
9
9
|
arr
|
10
10
|
end
|
11
11
|
|
12
|
-
products[0]
|
13
|
-
products[0]
|
14
|
-
products[0]
|
15
|
-
products[0]
|
16
|
-
products[0]
|
17
|
-
products[0]
|
12
|
+
products[0][:uid].should == "FFCF177"
|
13
|
+
products[0][:name].should == "Kindle"
|
14
|
+
products[0][:description].should == "The world's best-selling e-reader."
|
15
|
+
products[0][:price].should == "$109"
|
16
|
+
products[0][:images][:thumb].should == "http://amazon.com/kindle_thumb.jpg"
|
17
|
+
products[0][:images][:large].should == "http://amazon.com/kindle.jpg"
|
18
18
|
|
19
|
-
products[1]
|
20
|
-
products[1]
|
21
|
-
products[1]
|
22
|
-
products[1]
|
23
|
-
products[1]
|
24
|
-
products[1]
|
19
|
+
products[1][:uid].should == "YD26NT"
|
20
|
+
products[1][:name].should == "Kindle Touch"
|
21
|
+
products[1][:description].should == "Simple-to-use touchscreen with built-in WIFI."
|
22
|
+
products[1][:price].should == "$79"
|
23
|
+
products[1][:images][:thumb].should == "http://amazon.com/kindle_touch_thumb.jpg"
|
24
|
+
products[1][:images][:large].should == "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
|
-
webstore
|
31
|
-
webstore
|
30
|
+
webstore[:products][:product].should be_instance_of Array
|
31
|
+
webstore[:products][:product].size.should == 2
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should return Enumerator when calling #parse without a block" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michał Szajbe
|
@@ -14,30 +14,16 @@ dependencies:
|
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 4.0.0
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "<"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 4.0.0
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: i18n
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "<"
|
17
|
+
- - ">="
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0
|
19
|
+
version: '0'
|
34
20
|
type: :runtime
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
|
-
- - "
|
24
|
+
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0
|
26
|
+
version: '0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: nokogiri
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,9 +92,6 @@ require_paths:
|
|
106
92
|
required_ruby_version: !ruby/object:Gem::Requirement
|
107
93
|
requirements:
|
108
94
|
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: 1.9.0
|
111
|
-
- - "<"
|
112
95
|
- !ruby/object:Gem::Version
|
113
96
|
version: 1.9.3
|
114
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|