saxerator 0.9.5 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +9 -12
- data/Gemfile +4 -2
- data/README.md +23 -3
- data/Rakefile +12 -1
- data/benchmark/benchmark.rb +54 -12
- data/benchmark/generate_sample_file.rb +1 -1
- data/lib/saxerator.rb +2 -3
- data/lib/saxerator/adapters/nokogiri.rb +39 -0
- data/lib/saxerator/adapters/ox.rb +69 -0
- data/lib/saxerator/adapters/rexml.rb +42 -0
- data/lib/saxerator/builder.rb +2 -2
- data/lib/saxerator/builder/array_element.rb +8 -2
- data/lib/saxerator/builder/empty_element.rb +2 -3
- data/lib/saxerator/builder/hash_builder.rb +8 -7
- data/lib/saxerator/builder/hash_element.rb +7 -8
- data/lib/saxerator/builder/string_element.rb +5 -7
- data/lib/saxerator/builder/xml_builder.rb +11 -7
- data/lib/saxerator/configuration.rb +27 -7
- data/lib/saxerator/document_fragment.rb +3 -5
- data/lib/saxerator/dsl.rb +6 -5
- data/lib/saxerator/full_document.rb +1 -1
- data/lib/saxerator/latches/abstract_latch.rb +1 -1
- data/lib/saxerator/latches/at_depth.rb +2 -2
- data/lib/saxerator/latches/child_of.rb +9 -14
- data/lib/saxerator/latches/for_tags.rb +2 -2
- data/lib/saxerator/latches/with_attributes.rb +2 -2
- data/lib/saxerator/latches/within.rb +6 -10
- data/lib/saxerator/parser/accumulator.rb +6 -9
- data/lib/saxerator/parser/latched_accumulator.rb +4 -49
- data/lib/saxerator/sax_handler.rb +9 -0
- data/lib/saxerator/version.rb +1 -1
- data/saxerator.gemspec +5 -2
- data/spec/lib/builder/hash_builder_spec.rb +25 -19
- data/spec/lib/builder/xml_builder_spec.rb +7 -26
- data/spec/lib/dsl/all_spec.rb +11 -6
- data/spec/lib/dsl/at_depth_spec.rb +10 -8
- data/spec/lib/dsl/child_of_spec.rb +6 -6
- data/spec/lib/dsl/for_tag_spec.rb +3 -3
- data/spec/lib/dsl/for_tags_spec.rb +5 -5
- data/spec/lib/dsl/with_attribute_spec.rb +4 -4
- data/spec/lib/dsl/with_attributes_spec.rb +8 -7
- data/spec/lib/dsl/within_spec.rb +6 -5
- data/spec/lib/saxerator_spec.rb +70 -58
- data/spec/spec_helper.rb +24 -3
- data/spec/support/fixture_file.rb +1 -1
- metadata +39 -5
data/lib/saxerator/version.rb
CHANGED
data/saxerator.gemspec
CHANGED
@@ -17,6 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
|
18
18
|
s.rubyforge_project = 'saxerator'
|
19
19
|
|
20
|
+
s.required_ruby_version = '>= 2.1.0'
|
21
|
+
|
20
22
|
s.files = [
|
21
23
|
'LICENSE',
|
22
24
|
'README.md',
|
@@ -33,7 +35,8 @@ Gem::Specification.new do |s|
|
|
33
35
|
s.executables = []
|
34
36
|
s.require_paths = ['lib']
|
35
37
|
|
36
|
-
s.
|
37
|
-
|
38
|
+
s.add_development_dependency 'nokogiri', '>= 1.4.0'
|
39
|
+
s.add_development_dependency 'ox'
|
40
|
+
s.add_development_dependency 'rake'
|
38
41
|
s.add_development_dependency 'rspec', '~> 3.1'
|
39
42
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe 'Saxerator (default) hash format' do
|
5
5
|
let(:xml) { fixture_file('nested_elements.xml') }
|
6
6
|
subject(:entry) { Saxerator.parser(xml).for_tag(:entry).first }
|
7
7
|
|
@@ -9,10 +9,13 @@ describe "Saxerator (default) hash format" do
|
|
9
9
|
specify { expect(entry['title']).to eq('How to eat an airplane') }
|
10
10
|
|
11
11
|
# hash and cdata inside name
|
12
|
-
specify { expect(entry['author']).to eq(
|
12
|
+
specify { expect(entry['author']).to eq('name' => 'Soul<utter') }
|
13
13
|
|
14
14
|
# array of hashes
|
15
|
-
specify
|
15
|
+
specify do
|
16
|
+
expect(entry['contributor'])
|
17
|
+
.to eq([{ 'name' => 'Jane Doe' }, { 'name' => 'Leviticus Alabaster' }])
|
18
|
+
end
|
16
19
|
|
17
20
|
# attributes on a hash
|
18
21
|
specify { expect(entry['contributor'][0].attributes['type']).to eq('primary') }
|
@@ -26,32 +29,32 @@ describe "Saxerator (default) hash format" do
|
|
26
29
|
# name on a string
|
27
30
|
specify { expect(entry['title'].name).to eq('title') }
|
28
31
|
|
29
|
-
describe
|
30
|
-
it
|
32
|
+
describe '#to_s' do
|
33
|
+
it 'preserves the element name' do
|
31
34
|
expect(entry['title'].to_a.name).to eq('title')
|
32
35
|
end
|
33
36
|
end
|
34
37
|
|
35
|
-
describe
|
36
|
-
it
|
38
|
+
describe '#to_h' do
|
39
|
+
it 'preserves the element name' do
|
37
40
|
expect(entry.to_h.name).to eq('entry')
|
38
41
|
end
|
39
42
|
end
|
40
43
|
|
41
|
-
describe
|
42
|
-
it
|
44
|
+
describe '#to_a' do
|
45
|
+
it 'preserves the element name on a parsed hash' do
|
43
46
|
expect(entry.to_a.name).to eq('entry')
|
44
47
|
end
|
45
48
|
|
46
|
-
it
|
49
|
+
it 'converts parsed hashes to nested key/value pairs (just like regular hashes)' do
|
47
50
|
expect(entry.to_a.first).to eq(['id', '1'])
|
48
51
|
end
|
49
52
|
|
50
|
-
it
|
53
|
+
it 'preserves the element name on a parsed string' do
|
51
54
|
expect(entry['title'].to_a.name).to eq('title')
|
52
55
|
end
|
53
56
|
|
54
|
-
it
|
57
|
+
it 'preserves the element name on an array' do
|
55
58
|
expect(entry['contributor'].to_a.name).to eq 'contributor'
|
56
59
|
end
|
57
60
|
end
|
@@ -60,21 +63,24 @@ describe "Saxerator (default) hash format" do
|
|
60
63
|
specify { expect(entry['contributor'].name).to eq('contributor') }
|
61
64
|
|
62
65
|
# character entity decoding
|
63
|
-
specify
|
66
|
+
specify do
|
67
|
+
expect(entry['content'])
|
68
|
+
.to eq('<p>Airplanes are very large — this can present difficulty in digestion.</p>')
|
69
|
+
end
|
64
70
|
|
65
|
-
context
|
71
|
+
context 'parsing an empty element' do
|
66
72
|
subject(:element) { entry['media:thumbnail'] }
|
67
73
|
|
68
|
-
it
|
74
|
+
it 'behaves somewhat like nil' do
|
69
75
|
expect(element).to be_nil
|
70
|
-
expect(!element).to
|
71
|
-
expect(element.to_s).to eq
|
72
|
-
expect(element.to_h).to eq
|
76
|
+
expect(!element).to be true
|
77
|
+
expect(element.to_s).to eq('')
|
78
|
+
expect(element.to_h).to eq({})
|
73
79
|
end
|
74
80
|
|
75
81
|
it { is_expected.to be_empty }
|
76
82
|
|
77
|
-
it
|
83
|
+
it 'has attributes' do
|
78
84
|
expect(element.attributes.keys).to eq ['url']
|
79
85
|
end
|
80
86
|
|
@@ -1,37 +1,18 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
describe
|
4
|
+
describe 'Saxerator xml format' do
|
5
5
|
let(:xml) { fixture_file('nested_elements.xml') }
|
6
|
+
|
6
7
|
subject(:entry) do
|
7
8
|
Saxerator.parser(xml) do |config|
|
8
9
|
config.output_type = :xml
|
9
10
|
end.for_tag(:entry).first
|
10
11
|
end
|
11
12
|
|
12
|
-
it { is_expected.to be_a(
|
13
|
-
it
|
14
|
-
expected_xml =
|
15
|
-
|
16
|
-
<entry>
|
17
|
-
<id>1</id>
|
18
|
-
<published>2012-01-01T16:17:00-06:00</published>
|
19
|
-
<updated>2012-01-01T16:17:00-06:00</updated>
|
20
|
-
<link href="https://example.com/blog/how-to-eat-an-airplane"/>
|
21
|
-
<title>How to eat an airplane</title>
|
22
|
-
<content type="html"><p>Airplanes are very large — this can present difficulty in digestion.</p></content>
|
23
|
-
<media:thumbnail url="http://www.gravatar.com/avatar/a9eb6ba22e482b71b266daadf9c9a080?s=80"/>
|
24
|
-
<author>
|
25
|
-
<name>Soul<utter</name>
|
26
|
-
</author>
|
27
|
-
<contributor type="primary">
|
28
|
-
<name>Jane Doe</name>
|
29
|
-
</contributor>
|
30
|
-
<contributor>
|
31
|
-
<name>Leviticus Alabaster</name>
|
32
|
-
</contributor>
|
33
|
-
</entry>
|
34
|
-
eos
|
35
|
-
expect(entry.to_xml).to eq(expected_xml)
|
13
|
+
it { is_expected.to be_a(REXML::Document) }
|
14
|
+
it 'looks like the original document' do
|
15
|
+
expected_xml = '<?xml version=\'1.0\' encoding=\'UTF-8\'?><entry><id>1</id><published>2012-01-01T16:17:00-06:00</published><updated>2012-01-01T16:17:00-06:00</updated><link href="https://example.com/blog/how-to-eat-an-airplane"/><title>How to eat an airplane</title><content type="html"><p>Airplanes are very large — this can present difficulty in digestion.</p></content><media:thumbnail url="http://www.gravatar.com/avatar/a9eb6ba22e482b71b266daadf9c9a080?s=80"/><author><name>Soul<utter</name></author><contributor type="primary"><name>Jane Doe</name></contributor><contributor><name>Leviticus Alabaster</name></contributor></entry>'
|
16
|
+
expect(entry.to_s).to eq(expected_xml)
|
36
17
|
end
|
37
|
-
end
|
18
|
+
end
|
data/spec/lib/dsl/all_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::FullDocument#all' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -15,17 +15,22 @@ describe "Saxerator::FullDocument#all" do
|
|
15
15
|
eos
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
19
|
-
expect(parser.all)
|
18
|
+
it 'allows you to parse an entire document' do
|
19
|
+
expect(parser.all)
|
20
|
+
.to eq 'blurb' => ['one', 'two', 'three'], 'notablurb' => 'four', 'empty' => {}
|
20
21
|
end
|
21
22
|
|
22
|
-
context
|
23
|
+
context 'with_put_attributes_in_hash' do
|
23
24
|
subject(:parser) do
|
24
25
|
Saxerator.parser(xml) { |config| config.put_attributes_in_hash! }
|
25
26
|
end
|
26
27
|
|
27
|
-
it
|
28
|
-
expect(parser.all).to eq
|
28
|
+
it 'allows you to parse an entire document' do
|
29
|
+
expect(parser.all).to eq(
|
30
|
+
'blurb' => ['one', 'two', 'three'],
|
31
|
+
'notablurb' => 'four',
|
32
|
+
'empty' => { 'with' => 'attribute' }
|
33
|
+
)
|
29
34
|
end
|
30
35
|
end
|
31
36
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#at_depth' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -18,17 +18,19 @@ describe "Saxerator::DSL#at_depth" do
|
|
18
18
|
eos
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'parses elements at the requested tag depth' do
|
22
22
|
expect(parser.at_depth(2).inject([], :<<)).to eq([
|
23
|
-
'How to eat an airplane',
|
24
|
-
'
|
23
|
+
'How to eat an airplane',
|
24
|
+
'Leviticus Alabaster',
|
25
|
+
'To wallop a horse in the face',
|
26
|
+
'Jeanne Clarewood'
|
25
27
|
])
|
26
28
|
end
|
27
29
|
|
28
|
-
it
|
30
|
+
it 'works in combination with #for_tag' do
|
29
31
|
expect(parser.at_depth(2).for_tag(:name).inject([], :<<)).to eq([
|
30
|
-
|
31
|
-
|
32
|
+
'How to eat an airplane',
|
33
|
+
'To wallop a horse in the face'
|
32
34
|
])
|
33
35
|
end
|
34
|
-
end
|
36
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#child_of' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
7
|
-
|
8
|
-
|
7
|
+
<<-eos
|
8
|
+
<root>
|
9
9
|
<children>
|
10
10
|
<name>Rudy McMannis</name>
|
11
11
|
<children>
|
@@ -20,17 +20,17 @@ describe "Saxerator::DSL#child_of" do
|
|
20
20
|
eos
|
21
21
|
end
|
22
22
|
|
23
|
-
it
|
23
|
+
it 'only parses children of the specified tag' do
|
24
24
|
expect(parser.child_of(:grandchildren).inject([], :<<)).to eq([
|
25
25
|
'Mildred Marston'
|
26
26
|
])
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it 'works in combination with #for_tag' do
|
30
30
|
expect(parser.for_tag(:name).child_of(:children).inject([], :<<)).to eq([
|
31
31
|
'Rudy McMannis',
|
32
32
|
'Tom McMannis',
|
33
33
|
'Anne Welsh'
|
34
34
|
])
|
35
35
|
end
|
36
|
-
end
|
36
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#for_tag' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -14,7 +14,7 @@ describe "Saxerator::DSL#for_tag" do
|
|
14
14
|
eos
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'only selects the specified tag' do
|
18
18
|
expect(parser.for_tag(:blurb).inject([], :<<)).to eq(['one', 'two', 'three'])
|
19
19
|
end
|
20
|
-
end
|
20
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#for_tags' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -13,11 +13,11 @@ describe "Saxerator::DSL#for_tags" do
|
|
13
13
|
eos
|
14
14
|
end
|
15
15
|
|
16
|
-
it
|
16
|
+
it 'only selects the specified tags' do
|
17
17
|
expect(parser.for_tags(%w(blurb1 blurb3)).inject([], :<<)).to eq(['one', 'three'])
|
18
18
|
end
|
19
19
|
|
20
|
-
it
|
21
|
-
expect { parser.for_tags(
|
20
|
+
it 'raises an ArgumentError for a non-Array argument' do
|
21
|
+
expect { parser.for_tags('asdf') }.to raise_error ArgumentError
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#with_attribute' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -15,14 +15,14 @@ describe "Saxerator::DSL#with_attribute" do
|
|
15
15
|
eos
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it 'matches tags with the specified attributes' do
|
19
19
|
expect(subject.with_attribute(:type).inject([], :<<)).to eq([
|
20
20
|
'Leviticus Alabaster',
|
21
21
|
'Eunice Diesel'
|
22
22
|
])
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
25
|
+
it 'matches tags with the specified attributes' do
|
26
26
|
expect(subject.with_attribute(:type, :primary).inject([], :<<)).to eq(['Leviticus Alabaster'])
|
27
27
|
end
|
28
|
-
end
|
28
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#with_attributes' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -16,17 +16,18 @@ describe "Saxerator::DSL#with_attributes" do
|
|
16
16
|
eos
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
20
|
-
expect(parser.with_attributes(:
|
19
|
+
it 'matches tags with the exact specified attributes' do
|
20
|
+
expect(parser.with_attributes(type: :primary, ridiculous: 'true').inject([], :<<)).to eq([
|
21
21
|
'Leviticus Alabaster'
|
22
22
|
])
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
26
|
-
expect(parser.with_attributes(%w(type ridiculous)).inject([], :<<))
|
25
|
+
it 'matches tags which have the specified attributes' do
|
26
|
+
expect(parser.with_attributes(%w(type ridiculous)).inject([], :<<))
|
27
|
+
.to eq(['Leviticus Alabaster', 'Eunice Diesel'])
|
27
28
|
end
|
28
29
|
|
29
|
-
it
|
30
|
+
it 'raises ArgumentError if you pass something other than a Hash or Array' do
|
30
31
|
expect { parser.with_attributes('asdf') }.to raise_error ArgumentError
|
31
32
|
end
|
32
|
-
end
|
33
|
+
end
|
data/spec/lib/dsl/within_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Saxerator::DSL#within' do
|
4
4
|
subject(:parser) { Saxerator.parser(xml) }
|
5
5
|
|
6
6
|
let(:xml) do
|
@@ -15,14 +15,15 @@ describe "Saxerator::DSL#within" do
|
|
15
15
|
eos
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it 'only parses elements nested within the specified tag' do
|
19
19
|
expect(parser.within(:article).inject([], :<<)).to eq([
|
20
20
|
'Is our children learning?',
|
21
21
|
'Hazel Nutt'
|
22
22
|
])
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
26
|
-
expect(parser.for_tag(:name).within(:article).inject([], :<<))
|
25
|
+
it 'works in combination with #for_tag' do
|
26
|
+
expect(parser.for_tag(:name).within(:article).inject([], :<<))
|
27
|
+
.to eq(['Is our children learning?'])
|
27
28
|
end
|
28
|
-
end
|
29
|
+
end
|
data/spec/lib/saxerator_spec.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'spec_helper'
|
4
2
|
|
5
|
-
describe Saxerator do
|
6
|
-
context
|
7
|
-
subject(:parser)
|
3
|
+
RSpec.describe Saxerator do
|
4
|
+
context '#parser' do
|
5
|
+
subject(:parser) do
|
6
|
+
Saxerator.parser(xml)
|
7
|
+
end
|
8
8
|
|
9
|
-
context
|
9
|
+
context 'with a File argument' do
|
10
10
|
let(:xml) { fixture_file('flat_blurbs.xml') }
|
11
11
|
|
12
|
-
it
|
13
|
-
expect(parser.all).to eq(
|
12
|
+
it 'can parse it' do
|
13
|
+
expect(parser.all).to eq('blurb' => %w(one two three))
|
14
14
|
end
|
15
15
|
|
16
|
-
it
|
16
|
+
it 'allows multiple operations on the same parser' do
|
17
17
|
# This exposes a bug where if a File is not reset only the first
|
18
18
|
# Enumerable method works as expected
|
19
19
|
expect(parser.for_tag(:blurb).first).to eq('one')
|
@@ -21,48 +21,50 @@ describe Saxerator do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
context
|
24
|
+
context 'with a String argument' do
|
25
25
|
let(:xml) do
|
26
26
|
<<-eos
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
<book>
|
28
|
+
<name>Illiterates that can read</name>
|
29
|
+
<author>Eunice Diesel</author>
|
30
|
+
</book>
|
31
31
|
eos
|
32
32
|
end
|
33
33
|
|
34
|
-
it
|
35
|
-
expect(parser.all).to eq(
|
34
|
+
it 'can parse it' do
|
35
|
+
expect(parser.all).to eq('name' => 'Illiterates that can read', 'author' => 'Eunice Diesel')
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
context
|
40
|
+
context 'configuration' do
|
41
41
|
let(:xml) { '<foo><bar foo="bar">baz</bar></foo>' }
|
42
42
|
|
43
|
-
context
|
43
|
+
context 'output type' do
|
44
44
|
subject(:parser) do
|
45
|
-
Saxerator.parser(xml)
|
45
|
+
Saxerator.parser(xml) do |config|
|
46
|
+
config.output_type = output_type
|
47
|
+
end
|
46
48
|
end
|
47
49
|
|
48
|
-
context
|
50
|
+
context 'with config.output_type = :hash' do
|
49
51
|
let(:output_type) { :hash }
|
50
52
|
specify { expect(parser.all).to eq('bar' => 'baz') }
|
51
53
|
end
|
52
54
|
|
53
|
-
context
|
55
|
+
context 'with config.output_type = :xml' do
|
54
56
|
let(:output_type) { :xml }
|
55
|
-
specify { expect(parser.all).to be_a
|
57
|
+
specify { expect(parser.all).to be_a REXML::Document }
|
56
58
|
specify { expect(parser.all.to_s).to include '<bar foo="bar">' }
|
57
59
|
end
|
58
60
|
|
59
|
-
context
|
61
|
+
context 'with an invalid config.output_type' do
|
60
62
|
let(:output_type) { 'lmao' }
|
61
63
|
specify { expect { parser }.to raise_error(ArgumentError) }
|
62
64
|
end
|
63
65
|
end
|
64
66
|
|
65
|
-
context
|
67
|
+
context 'symbolize keys' do
|
66
68
|
subject(:parser) do
|
67
69
|
Saxerator.parser(xml) do |config|
|
68
70
|
config.symbolize_keys!
|
@@ -70,50 +72,59 @@ describe Saxerator do
|
|
70
72
|
end
|
71
73
|
end
|
72
74
|
|
73
|
-
specify { expect(parser.all).to eq(:
|
75
|
+
specify { expect(parser.all).to eq(bar: 'baz') }
|
74
76
|
specify { expect(parser.all.name).to eq(:foo) }
|
75
77
|
|
76
78
|
it 'will symbolize attributes' do
|
77
79
|
parser.for_tag('bar').each do |tag|
|
78
|
-
expect(tag.attributes).to include(:
|
80
|
+
expect(tag.attributes).to include(foo: 'bar')
|
79
81
|
end
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
83
|
-
context
|
84
|
-
let(:xml)
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
85
|
+
context 'with ignore namespaces' do
|
86
|
+
let(:xml) do
|
87
|
+
<<-eos
|
88
|
+
<ns1:foo xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ns1="http://foo.com" xmlns:ns3="http://bar.com">
|
89
|
+
<ns3:bar>baz</ns3:bar>
|
90
|
+
<ns3:bar bar="bar" ns1:foo="foo" class="class">bax</ns3:bar>
|
91
|
+
</ns1:foo>
|
92
|
+
eos
|
93
|
+
end
|
91
94
|
|
92
95
|
subject(:parser) do
|
93
|
-
Saxerator.parser(xml)
|
96
|
+
Saxerator.parser(xml) do |config|
|
94
97
|
config.ignore_namespaces!
|
95
|
-
|
98
|
+
end
|
96
99
|
end
|
97
100
|
|
98
|
-
specify
|
101
|
+
specify do
|
99
102
|
bar_count = 0
|
100
|
-
parser.for_tag(
|
103
|
+
parser.for_tag('bar').each do |_|
|
101
104
|
bar_count += 1
|
102
105
|
end
|
103
106
|
expect(bar_count).to eq(2)
|
104
|
-
|
107
|
+
end
|
105
108
|
end
|
106
109
|
|
107
|
-
context
|
108
|
-
let(:xml)
|
110
|
+
context 'with strip namespaces' do
|
111
|
+
let(:xml) do
|
112
|
+
<<-XML
|
113
|
+
<ns1:foo xmlns:ns1="http://foo.com" xmlns:ns3="http://baz.com">
|
114
|
+
<ns3:bar>baz</ns3:bar>
|
115
|
+
</ns1:foo>
|
116
|
+
XML
|
117
|
+
end
|
109
118
|
subject(:parser) do
|
110
|
-
Saxerator.parser(xml)
|
119
|
+
Saxerator.parser(xml) do |config|
|
120
|
+
config.strip_namespaces!
|
121
|
+
end
|
111
122
|
end
|
112
123
|
|
113
|
-
specify { expect(parser.all).to eq(
|
124
|
+
specify { expect(parser.all).to eq('bar' => 'baz') }
|
114
125
|
specify { expect(parser.all.name).to eq('foo') }
|
115
126
|
|
116
|
-
context
|
127
|
+
context 'combined with symbolize keys' do
|
117
128
|
subject(:parser) do
|
118
129
|
Saxerator.parser(xml) do |config|
|
119
130
|
config.strip_namespaces!
|
@@ -121,30 +132,31 @@ describe Saxerator do
|
|
121
132
|
end
|
122
133
|
end
|
123
134
|
|
124
|
-
specify { expect(parser.all).to eq(
|
135
|
+
specify { expect(parser.all).to eq(bar: 'baz') }
|
125
136
|
end
|
126
137
|
|
127
|
-
context
|
138
|
+
context 'for specific namespaces' do
|
128
139
|
let(:xml) do
|
129
|
-
<<-XML
|
130
|
-
<ns1:foo>
|
131
|
-
|
132
|
-
|
140
|
+
<<-XML
|
141
|
+
<ns1:foo xmlns:ns1="http://foo.com" xmlns:ns2="http://bar.com" xmlns:ns3="http://baz.com">
|
142
|
+
<ns2:bar>baz</ns2:bar>
|
143
|
+
<ns3:bar>biz</ns3:bar>
|
133
144
|
</ns1:foo>
|
134
145
|
XML
|
135
146
|
end
|
136
147
|
subject(:parser) do
|
137
|
-
Saxerator.parser(xml)
|
148
|
+
Saxerator.parser(xml) do |config|
|
149
|
+
config.strip_namespaces! :ns1, :ns3
|
150
|
+
end
|
138
151
|
end
|
139
152
|
|
140
|
-
specify { expect(parser.all).to eq(
|
153
|
+
specify { expect(parser.all).to eq('ns2:bar' => 'baz', 'bar' => 'biz') }
|
141
154
|
specify { expect(parser.all.name).to eq('foo') }
|
142
155
|
end
|
143
156
|
end
|
144
|
-
|
145
157
|
end
|
146
158
|
|
147
|
-
context
|
159
|
+
context 'configuration with put_attributes_in_hash!' do
|
148
160
|
let(:xml) { '<foo foo="bar"><bar>baz</bar></foo>' }
|
149
161
|
|
150
162
|
subject(:parser) do
|
@@ -153,8 +165,8 @@ describe Saxerator do
|
|
153
165
|
end
|
154
166
|
end
|
155
167
|
|
156
|
-
it
|
157
|
-
expect(parser.all).to eq(
|
168
|
+
it 'can parse it' do
|
169
|
+
expect(parser.all).to eq('bar' => 'baz', 'foo' => 'bar')
|
158
170
|
end
|
159
171
|
|
160
172
|
context 'with configured output_type :xml' do
|
@@ -165,7 +177,7 @@ describe Saxerator do
|
|
165
177
|
end
|
166
178
|
end
|
167
179
|
|
168
|
-
context
|
180
|
+
context 'raises error with' do
|
169
181
|
specify { expect { parser }.to raise_error(ArgumentError) }
|
170
182
|
end
|
171
183
|
end
|
@@ -179,7 +191,7 @@ describe Saxerator do
|
|
179
191
|
end
|
180
192
|
|
181
193
|
it 'will symbolize attribute hash keys' do
|
182
|
-
expect(parser.all).to include(:
|
194
|
+
expect(parser.all.to_hash).to include(bar: 'baz', foo: 'bar')
|
183
195
|
end
|
184
196
|
end
|
185
197
|
end
|