nori 2.7.0 → 2.8.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/CHANGELOG.md +51 -12
- data/README.md +77 -1
- data/lib/nori/core_ext/hash.rb +1 -1
- data/lib/nori/core_ext.rb +1 -1
- data/lib/nori/parser/nokogiri.rb +53 -6
- data/lib/nori/parser/rexml.rb +31 -2
- data/lib/nori/string_io_file.rb +2 -0
- data/lib/nori/string_utils.rb +18 -0
- data/lib/nori/version.rb +1 -1
- data/lib/nori/xml_utility_node.rb +89 -51
- data/lib/nori.rb +21 -1
- metadata +20 -24
- data/.devcontainer/devcontainer.json +0 -19
- data/.github/dependabot.yml +0 -12
- data/.github/workflows/test.yml +0 -26
- data/.gitignore +0 -8
- data/.rspec +0 -1
- data/Gemfile +0 -6
- data/Rakefile +0 -12
- data/benchmark/benchmark.rb +0 -19
- data/benchmark/soap_response.xml +0 -266
- data/lib/nori/core_ext/string.rb +0 -21
- data/nori.gemspec +0 -29
- data/spec/nori/api_spec.rb +0 -177
- data/spec/nori/core_ext/hash_spec.rb +0 -30
- data/spec/nori/core_ext/string_spec.rb +0 -33
- data/spec/nori/nori_spec.rb +0 -650
- data/spec/spec_helper.rb +0 -2
data/spec/nori/api_spec.rb
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe Nori do
|
|
4
|
-
|
|
5
|
-
describe "PARSERS" do
|
|
6
|
-
it "should return a Hash of parser details" do
|
|
7
|
-
expect(Nori::PARSERS).to eq({ :rexml => "REXML", :nokogiri => "Nokogiri" })
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
context ".new" do
|
|
12
|
-
it "defaults to not strip any namespace identifiers" do
|
|
13
|
-
xml = <<-XML
|
|
14
|
-
<history xmlns:ns10="http://ns10.example.com">
|
|
15
|
-
<ns10:case>a_case</ns10:case>
|
|
16
|
-
</history>
|
|
17
|
-
XML
|
|
18
|
-
|
|
19
|
-
expect(nori.parse(xml)["history"]["ns10:case"]).to eq("a_case")
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it "defaults to not change XML tags" do
|
|
23
|
-
xml = '<userResponse id="1"><accountStatus>active</accountStatus></userResponse>'
|
|
24
|
-
expect(nori.parse(xml)).to eq({ "userResponse" => { "@id" => "1", "accountStatus" => "active" } })
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it "raises when passed unknown global options" do
|
|
28
|
-
expect { Nori.new(:invalid => true) }.
|
|
29
|
-
to raise_error(ArgumentError, /Spurious options: \[:invalid\]/)
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
context ".new with :strip_namespaces" do
|
|
34
|
-
it "strips the namespace identifiers when set to true" do
|
|
35
|
-
xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></soap:Envelope>'
|
|
36
|
-
expect(nori(:strip_namespaces => true).parse(xml)).to have_key("Envelope")
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
it "still converts namespaced entries to array elements" do
|
|
40
|
-
xml = <<-XML
|
|
41
|
-
<history
|
|
42
|
-
xmlns:ns10="http://ns10.example.com"
|
|
43
|
-
xmlns:ns11="http://ns10.example.com">
|
|
44
|
-
<ns10:case><ns10:name>a_name</ns10:name></ns10:case>
|
|
45
|
-
<ns11:case><ns11:name>another_name</ns11:name></ns11:case>
|
|
46
|
-
</history>
|
|
47
|
-
XML
|
|
48
|
-
|
|
49
|
-
expected = [{ "name" => "a_name" }, { "name" => "another_name" }]
|
|
50
|
-
expect(nori(:strip_namespaces => true).parse(xml)["history"]["case"]).to eq(expected)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
context ".new with :convert_tags_to" do
|
|
55
|
-
it "converts all tags by a given formula" do
|
|
56
|
-
xml = '<userResponse id="1"><accountStatus>active</accountStatus></userResponse>'
|
|
57
|
-
|
|
58
|
-
snakecase_symbols = lambda { |tag| tag.snakecase.to_sym }
|
|
59
|
-
nori = nori(:convert_tags_to => snakecase_symbols)
|
|
60
|
-
|
|
61
|
-
expect(nori.parse(xml)).to eq({ :user_response => { :@id => "1", :account_status => "active" } })
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
context '#find' do
|
|
66
|
-
before do
|
|
67
|
-
upcase = lambda { |tag| tag.upcase }
|
|
68
|
-
@nori = nori(:convert_tags_to => upcase)
|
|
69
|
-
|
|
70
|
-
xml = '<userResponse id="1"><accountStatus>active</accountStatus></userResponse>'
|
|
71
|
-
@hash = @nori.parse(xml)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
it 'returns the Hash when the path is empty' do
|
|
75
|
-
result = @nori.find(@hash)
|
|
76
|
-
expect(result).to eq("USERRESPONSE" => { "ACCOUNTSTATUS" => "active", "@ID" => "1" })
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
it 'returns the result for a single key' do
|
|
80
|
-
result = @nori.find(@hash, 'userResponse')
|
|
81
|
-
expect(result).to eq("ACCOUNTSTATUS" => "active", "@ID" => "1")
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
it 'returns the result for nested keys' do
|
|
85
|
-
result = @nori.find(@hash, 'userResponse', 'accountStatus')
|
|
86
|
-
expect(result).to eq("active")
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
it 'strips the namespaces from Hash keys' do
|
|
90
|
-
xml = '<v1:userResponse xmlns:v1="http://example.com"><v1:accountStatus>active</v1:accountStatus></v1:userResponse>'
|
|
91
|
-
hash = @nori.parse(xml)
|
|
92
|
-
|
|
93
|
-
result = @nori.find(hash, 'userResponse', 'accountStatus')
|
|
94
|
-
expect(result).to eq("active")
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
context "#parse" do
|
|
99
|
-
it "defaults to use advanced typecasting" do
|
|
100
|
-
hash = nori.parse("<value>true</value>")
|
|
101
|
-
expect(hash["value"]).to eq(true)
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
it "defaults to use the Nokogiri parser" do
|
|
105
|
-
# parsers are loaded lazily by default
|
|
106
|
-
require "nori/parser/nokogiri"
|
|
107
|
-
|
|
108
|
-
expect(Nori::Parser::Nokogiri).to receive(:parse).and_return({})
|
|
109
|
-
nori.parse("<any>thing</any>")
|
|
110
|
-
end
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
context "#parse without :advanced_typecasting" do
|
|
114
|
-
it "can be changed to not typecast too much" do
|
|
115
|
-
hash = nori(:advanced_typecasting => false).parse("<value>true</value>")
|
|
116
|
-
expect(hash["value"]).to eq("true")
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
context "#parse with :parser" do
|
|
121
|
-
it "can be configured to use the REXML parser" do
|
|
122
|
-
# parsers are loaded lazily by default
|
|
123
|
-
require "nori/parser/rexml"
|
|
124
|
-
|
|
125
|
-
expect(Nori::Parser::REXML).to receive(:parse).and_return({})
|
|
126
|
-
nori(:parser => :rexml).parse("<any>thing</any>")
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
context "#parse without :delete_namespace_attributes" do
|
|
131
|
-
it "can be changed to not delete xmlns attributes" do
|
|
132
|
-
xml = '<userResponse xmlns="http://schema.company.com/some/path/to/namespace/v1"><accountStatus>active</accountStatus></userResponse>'
|
|
133
|
-
hash = nori(:delete_namespace_attributes => false).parse(xml)
|
|
134
|
-
expect(hash).to eq({"userResponse" => {"@xmlns" => "http://schema.company.com/some/path/to/namespace/v1", "accountStatus" => "active"}})
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
it "can be changed to not delete xsi attributes" do
|
|
138
|
-
xml = '<userResponse xsi="abc:myType"><accountStatus>active</accountStatus></userResponse>'
|
|
139
|
-
hash = nori(:delete_namespace_attributes => false).parse(xml)
|
|
140
|
-
expect(hash).to eq({"userResponse" => {"@xsi" => "abc:myType", "accountStatus" => "active"}})
|
|
141
|
-
end
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
context "#parse with :delete_namespace_attributes" do
|
|
145
|
-
it "can be changed to delete xmlns attributes" do
|
|
146
|
-
xml = '<userResponse xmlns="http://schema.company.com/some/path/to/namespace/v1"><accountStatus>active</accountStatus></userResponse>'
|
|
147
|
-
hash = nori(:delete_namespace_attributes => true).parse(xml)
|
|
148
|
-
expect(hash).to eq({"userResponse" => {"accountStatus" => "active"}})
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
it "can be changed to delete xsi attributes" do
|
|
152
|
-
xml = '<userResponse xsi="abc:myType"><accountStatus>active</accountStatus></userResponse>'
|
|
153
|
-
hash = nori(:delete_namespace_attributes => true).parse(xml)
|
|
154
|
-
expect(hash).to eq({"userResponse" => {"accountStatus" => "active"}})
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
context "#parse with :convert_dashes_to_underscores" do
|
|
159
|
-
it "can be configured to skip dash to underscore conversion" do
|
|
160
|
-
xml = '<any-tag>foo bar</any-tag>'
|
|
161
|
-
hash = nori(:convert_dashes_to_underscores => false).parse(xml)
|
|
162
|
-
expect(hash).to eq({'any-tag' => 'foo bar'})
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
context "#parse with :empty_tag_value set to empty string" do
|
|
167
|
-
it "can be configured to convert empty tags to given value" do
|
|
168
|
-
xml = "<parentTag><tag/></parentTag>"
|
|
169
|
-
hash = nori(:empty_tag_value => "").parse(xml)
|
|
170
|
-
expect(hash).to eq("parentTag" => { "tag" => "" })
|
|
171
|
-
end
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
def nori(options = {})
|
|
175
|
-
Nori.new(options)
|
|
176
|
-
end
|
|
177
|
-
end
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe Hash do
|
|
4
|
-
|
|
5
|
-
describe "#normalize_param" do
|
|
6
|
-
it "should have specs"
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
describe "#to_xml_attributes" do
|
|
10
|
-
|
|
11
|
-
it "should turn the hash into xml attributes" do
|
|
12
|
-
attrs = { :one => "ONE", "two" => "TWO" }.to_xml_attributes
|
|
13
|
-
expect(attrs).to match(/one="ONE"/m)
|
|
14
|
-
expect(attrs).to match(/two="TWO"/m)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it "should preserve _ in hash keys" do
|
|
18
|
-
attrs = {
|
|
19
|
-
:some_long_attribute => "with short value",
|
|
20
|
-
:crash => :burn,
|
|
21
|
-
:merb => "uses extlib"
|
|
22
|
-
}.to_xml_attributes
|
|
23
|
-
|
|
24
|
-
expect(attrs).to match(/some_long_attribute="with short value"/)
|
|
25
|
-
expect(attrs).to match(/merb="uses extlib"/)
|
|
26
|
-
expect(attrs).to match(/crash="burn"/)
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
end
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
describe String do
|
|
4
|
-
|
|
5
|
-
describe "#snakecase" do
|
|
6
|
-
it "lowercases one word CamelCase" do
|
|
7
|
-
expect("Merb".snakecase).to eq("merb")
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "makes one underscore snakecase two word CamelCase" do
|
|
11
|
-
expect("MerbCore".snakecase).to eq("merb_core")
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "handles CamelCase with more than 2 words" do
|
|
15
|
-
expect("SoYouWantContributeToMerbCore".snakecase).to eq("so_you_want_contribute_to_merb_core")
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it "handles CamelCase with more than 2 capital letter in a row" do
|
|
19
|
-
expect("CNN".snakecase).to eq("cnn")
|
|
20
|
-
expect("CNNNews".snakecase).to eq("cnn_news")
|
|
21
|
-
expect("HeadlineCNNNews".snakecase).to eq("headline_cnn_news")
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
it "does NOT change one word lowercase" do
|
|
25
|
-
expect("merb".snakecase).to eq("merb")
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it "leaves snake_case as is" do
|
|
29
|
-
expect("merb_core".snakecase).to eq("merb_core")
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
end
|