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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 134c308ec032a7fd67bcde3a744f8f75bd656da9
4
- data.tar.gz: 11d8785e94c498dfd91b0ef0d2656ffb97fc314d
3
+ metadata.gz: e902b0cd867994419869833857f4c96bb6427f17
4
+ data.tar.gz: f4e5babf7a86675953869fbec446404fceb9ea0a
5
5
  SHA512:
6
- metadata.gz: c76931d22bee9d2f6723f1d6587a8af51ca510df4d74c15304b06a9b80e7bec63a1330993573af7a2e697f07d174f358a51e18b1a339b1e91953ac027695e17a
7
- data.tar.gz: 38a2eaee5c237f4c46fe463e13e777a9978557e2483150cad4cbb930c505c4577d7604a5dabdb7f226fe24daa9c9743947472b0b88b7dbeb25af60435bed6a01
6
+ metadata.gz: aebfd829d32a73141a4c289cc4f557ed6c2c647248b659bbc037d9bca1e58196394dd42c9439841262062c29dfb49af953ce7e9cde99179d9ce5e1be2c87962d
7
+ data.tar.gz: c3d2f658bae270cc76da97881037fb598eb2b254760f349c304fc6943409c9fa1913d2cb65d3bada50c223354e49eed2a4183b5869f7d8b0c7a1debadfc08954
@@ -2,4 +2,7 @@ script: "bundle exec rspec ./spec/"
2
2
  sudo: false
3
3
  language: ruby
4
4
  rvm:
5
- - 1.9.2
5
+ - 2.2
6
+ - 2.1
7
+ - 2.0.0
8
+ - 1.9.3
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', '~> 0.4.0'
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 --version '~> 0.4.0'
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 `OpenStruct`s and yield them inside the block.
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.name
58
- puts product.images.thumb_size.contents
59
- puts "#{product.images.thumb_size.width}x#{product.images.thumb_size.height}"
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 OpenStructs to ActiveRecord instances, etc.
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.products.product.size # => 2
107
+ webstore[:products][:product].size # => 2
95
108
 
96
109
  ## Contributing
97
110
 
@@ -2,8 +2,8 @@ require 'saxy/version'
2
2
 
3
3
  module Saxy
4
4
  class << self
5
- def parse(xml_file, object_tag, &blk)
6
- parser = Parser.new(xml_file, object_tag)
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
 
@@ -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 as_object
27
- if attributes.any?
28
- object = OpenStruct.new
29
- attributes.each do |name, value|
30
- value = value.first if value.size == 1
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
@@ -36,7 +36,7 @@ module Saxy
36
36
  def end_element(tag)
37
37
  tags.pop
38
38
  if element = elements.pop
39
- object = element.as_object
39
+ object = element.to_h
40
40
 
41
41
  if current_element
42
42
  current_element.set_attribute(tag, object)
@@ -1,3 +1,3 @@
1
1
  module Saxy
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -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 = [">= 1.9.0", "< 1.9.3"]
18
+ gem.required_ruby_version = ">= 1.9.3"
19
19
 
20
- gem.add_dependency "activesupport", "< 4.0.0"
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
@@ -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.as_object.should == "foo"
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.as_object
24
+ object = element.to_h
25
25
 
26
- object.foo.should == 1
27
- object.bar.should == 2
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.as_object
33
+ object = element.to_h
34
34
 
35
- object.foo.should == "bar"
36
- object.contents.should == "value"
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.as_object.foo_bar.should == "baz"
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.as_object.foo.should == ["bar", "baz"]
47
+ element.to_h[:foo].should == ["bar", "baz"]
48
48
  end
49
49
  end
@@ -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.as_object)
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.as_object.bar.should == "baz"
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
@@ -9,26 +9,26 @@ describe Saxy do
9
9
  arr
10
10
  end
11
11
 
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"
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].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"
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.products.product.should be_instance_of Array
31
- webstore.products.product.size.should == 2
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.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.7.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.7.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