badgerhash 0.0.1
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/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +12 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +507 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/badgerhash.gemspec +24 -0
- data/lib/badgerhash.rb +31 -0
- data/lib/badgerhash/handlers/sax_handler.rb +95 -0
- data/lib/badgerhash/parsers/rexml.rb +49 -0
- data/lib/badgerhash/version.rb +5 -0
- data/lib/badgerhash/xml_stream.rb +40 -0
- data/spec/badgerhash/handlers/sax_handler_spec.rb +134 -0
- data/spec/badgerhash/parsers/rexml_spec.rb +60 -0
- data/spec/badgerhash/xml_stream_spec.rb +26 -0
- data/spec/badgerhash_spec.rb +23 -0
- data/spec/integration/stream_parser_spec.rb +100 -0
- data/spec/spec_helper.rb +10 -0
- metadata +107 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require File.expand_path "../../spec_helper.rb", __FILE__
|
|
2
|
+
|
|
3
|
+
module Badgerhash
|
|
4
|
+
describe XmlStream do
|
|
5
|
+
let(:io) { double(:io) }
|
|
6
|
+
|
|
7
|
+
describe ".create" do
|
|
8
|
+
it "creates a new XmlStream" do
|
|
9
|
+
expect(XmlStream).to receive(:new)
|
|
10
|
+
XmlStream.create(io)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "#to_badgerfish" do
|
|
15
|
+
let(:result) { { "foo" => { "$" => "bar"} } }
|
|
16
|
+
let(:handler) { double(:handler, node: result) }
|
|
17
|
+
let(:parser) { double(:parser) }
|
|
18
|
+
subject(:xml_stream) { XmlStream.new(handler, parser, io) }
|
|
19
|
+
|
|
20
|
+
it "parses io using the given parser and handler" do
|
|
21
|
+
expect(parser).to receive(:parse).with(handler, io)
|
|
22
|
+
expect(xml_stream.to_badgerfish).to eq(result)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require File.expand_path "../spec_helper.rb", __FILE__
|
|
2
|
+
|
|
3
|
+
describe Badgerhash do
|
|
4
|
+
describe ".sax_parser=" do
|
|
5
|
+
let(:parser) { double(:parser) }
|
|
6
|
+
|
|
7
|
+
after :each do
|
|
8
|
+
reset_instance_variables Badgerhash
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "sets the parser" do
|
|
12
|
+
Badgerhash.sax_parser = parser
|
|
13
|
+
expect(Badgerhash.sax_parser).to eq(parser)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe ".sax_parser" do
|
|
18
|
+
it "uses the nokogiri parser as default" do
|
|
19
|
+
expect(Badgerhash.sax_parser)
|
|
20
|
+
.to eq(Badgerhash::Parsers::REXML::SaxDocument)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
require File.expand_path "../../spec_helper.rb", __FILE__
|
|
2
|
+
|
|
3
|
+
describe "Parsing XML Stream", integration: true do
|
|
4
|
+
subject(:badgerfish) { Badgerhash::XmlStream.create(xml).to_badgerfish }
|
|
5
|
+
|
|
6
|
+
context "with simple document" do
|
|
7
|
+
let(:xml) { StringIO.new("<alice>bob</alice>") }
|
|
8
|
+
|
|
9
|
+
it "parses document correctly" do
|
|
10
|
+
expect(badgerfish).to eq({"alice" => {"\$" => "bob"}})
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context "with whitespace in nodes" do
|
|
15
|
+
let(:xml_str) {
|
|
16
|
+
<<-XML
|
|
17
|
+
<alice>
|
|
18
|
+
bob
|
|
19
|
+
</alice>
|
|
20
|
+
XML
|
|
21
|
+
}
|
|
22
|
+
let(:xml) { StringIO.new(xml_str) }
|
|
23
|
+
|
|
24
|
+
it "compresses the whitespace" do
|
|
25
|
+
expect(badgerfish).to eq({"alice" => {"\$" => "bob"}})
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "with nested elements" do
|
|
30
|
+
let(:xml_str) { "<alice><bob>charlie</bob><david>edgar</david></alice>" }
|
|
31
|
+
let(:xml) { StringIO.new(xml_str) }
|
|
32
|
+
let(:expected) { {"alice" => {"bob" => {"\$" => "charlie" },
|
|
33
|
+
"david" => { "\$" => "edgar"}}} }
|
|
34
|
+
|
|
35
|
+
it "parses document correctly" do
|
|
36
|
+
expect(badgerfish).to eq(expected)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "with multiple elements at the same level" do
|
|
41
|
+
let(:xml_str) { "<alice><bob>charlie</bob><bob>david</bob></alice>" }
|
|
42
|
+
let(:xml) { StringIO.new(xml_str) }
|
|
43
|
+
let(:expected) { { "alice" => {"bob" => [{"\$" => "charlie"},
|
|
44
|
+
{"\$" => "david" }]}} }
|
|
45
|
+
|
|
46
|
+
it "parses document correctly" do
|
|
47
|
+
expect(badgerfish).to eq(expected)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
context "with attributes on elements" do
|
|
52
|
+
let(:xml) { StringIO.new("<alice charlie=\"david\">bob</alice>") }
|
|
53
|
+
let(:expected) { {"alice" => { "\$" => "bob", "@charlie" => "david" } } }
|
|
54
|
+
|
|
55
|
+
it "add attribute as properties begining with '@'" do
|
|
56
|
+
expect(badgerfish).to eq(expected)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
context "with a default namespace" do
|
|
61
|
+
let(:xml) { StringIO.new("<alice xmlns=\"http://some-namespace\">bob</alice>") }
|
|
62
|
+
let(:expected) { {"alice" => {"\$" => "bob", "@xmlns" => {
|
|
63
|
+
"\$" => "http:\/\/some-namespace"}} } }
|
|
64
|
+
|
|
65
|
+
it "places it in the @xmlns.$ property" do
|
|
66
|
+
expect(badgerfish).to eq(expected)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
context "with other namespaces" do
|
|
71
|
+
let(:xml_str) { "<alice xmlns=\"http:\/\/some-namespace\" xmlns:charlie=\"http:\/\/some-other-namespace\">bob</alice>" }
|
|
72
|
+
let(:xml) { StringIO.new(xml_str) }
|
|
73
|
+
let(:expected) { {"alice" => {"\$" => "bob", "@xmlns" => {
|
|
74
|
+
"\$" => "http:\/\/some-namespace",
|
|
75
|
+
"charlie" => "http:\/\/some-other-namespace" }}} }
|
|
76
|
+
|
|
77
|
+
it "places them in properties of @xmlns" do
|
|
78
|
+
expect(badgerfish).to eq(expected)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
context "with elements containing namespace prefixes" do
|
|
83
|
+
let(:xml_str) {
|
|
84
|
+
"<alice xmlns=\"http://some-namespace\" xmlns:charlie=\"http://some-other-namespace\"><bob>david</bob><charlie:edgar>frank</charlie:edgar></alice>"
|
|
85
|
+
}
|
|
86
|
+
let(:xml) { StringIO.new(xml_str) }
|
|
87
|
+
let(:expected) { {"alice" => {"bob" => {"\$" => "david" ,
|
|
88
|
+
"@xmlns" => {"charlie" => "http:\/\/some-other-namespace",
|
|
89
|
+
"\$" => "http:\/\/some-namespace"}},
|
|
90
|
+
"charlie:edgar" => {"\$" => "frank",
|
|
91
|
+
"@xmlns" => {"charlie" => "http:\/\/some-other-namespace",
|
|
92
|
+
"\$" => "http:\/\/some-namespace"}},
|
|
93
|
+
"@xmlns" => {"charlie" => "http:\/\/some-other-namespace",
|
|
94
|
+
"\$" => "http:\/\/some-namespace"}}} }
|
|
95
|
+
|
|
96
|
+
it "add them as properties" do
|
|
97
|
+
expect(badgerfish).to eq(expected)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: badgerhash
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- George F. Murphy
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2014-03-26 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1.5'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '1.5'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rake
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ~>
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: 10.1.1
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ~>
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 10.1.1
|
|
46
|
+
description:
|
|
47
|
+
email:
|
|
48
|
+
- gmurphy@epublishing.com
|
|
49
|
+
executables: []
|
|
50
|
+
extensions: []
|
|
51
|
+
extra_rdoc_files: []
|
|
52
|
+
files:
|
|
53
|
+
- .gitignore
|
|
54
|
+
- .rspec
|
|
55
|
+
- .travis.yml
|
|
56
|
+
- Gemfile
|
|
57
|
+
- LICENSE.txt
|
|
58
|
+
- README.md
|
|
59
|
+
- Rakefile
|
|
60
|
+
- badgerhash.gemspec
|
|
61
|
+
- lib/badgerhash.rb
|
|
62
|
+
- lib/badgerhash/handlers/sax_handler.rb
|
|
63
|
+
- lib/badgerhash/parsers/rexml.rb
|
|
64
|
+
- lib/badgerhash/version.rb
|
|
65
|
+
- lib/badgerhash/xml_stream.rb
|
|
66
|
+
- spec/badgerhash/handlers/sax_handler_spec.rb
|
|
67
|
+
- spec/badgerhash/parsers/rexml_spec.rb
|
|
68
|
+
- spec/badgerhash/xml_stream_spec.rb
|
|
69
|
+
- spec/badgerhash_spec.rb
|
|
70
|
+
- spec/integration/stream_parser_spec.rb
|
|
71
|
+
- spec/spec_helper.rb
|
|
72
|
+
homepage: http://github.com/gfmurphy/badgerhash
|
|
73
|
+
licenses:
|
|
74
|
+
- GNU LGPL
|
|
75
|
+
post_install_message:
|
|
76
|
+
rdoc_options: []
|
|
77
|
+
require_paths:
|
|
78
|
+
- lib
|
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
|
+
none: false
|
|
81
|
+
requirements:
|
|
82
|
+
- - ! '>='
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: 1.9.3
|
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
|
+
none: false
|
|
87
|
+
requirements:
|
|
88
|
+
- - ! '>='
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
segments:
|
|
92
|
+
- 0
|
|
93
|
+
hash: -4557748935909300667
|
|
94
|
+
requirements: []
|
|
95
|
+
rubyforge_project:
|
|
96
|
+
rubygems_version: 1.8.23
|
|
97
|
+
signing_key:
|
|
98
|
+
specification_version: 3
|
|
99
|
+
summary: Parse XML as IO into hash using badgerfish convention.
|
|
100
|
+
test_files:
|
|
101
|
+
- spec/badgerhash/handlers/sax_handler_spec.rb
|
|
102
|
+
- spec/badgerhash/parsers/rexml_spec.rb
|
|
103
|
+
- spec/badgerhash/xml_stream_spec.rb
|
|
104
|
+
- spec/badgerhash_spec.rb
|
|
105
|
+
- spec/integration/stream_parser_spec.rb
|
|
106
|
+
- spec/spec_helper.rb
|
|
107
|
+
has_rdoc:
|