xmlhasher 0.0.2 → 0.0.3
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/lib/xmlhasher/handler.rb +7 -2
- data/lib/xmlhasher/node.rb +5 -6
- data/lib/xmlhasher/version.rb +1 -1
- data/test/xmlhasher/parser_test.rb +167 -70
- data/xmlhasher.gemspec +1 -0
- metadata +17 -1
data/lib/xmlhasher/handler.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'ox'
|
2
|
+
require 'escape_utils'
|
2
3
|
|
3
4
|
module XmlHasher
|
4
5
|
class Handler < ::Ox::Sax
|
@@ -17,12 +18,12 @@ module XmlHasher
|
|
17
18
|
|
18
19
|
def attr(name, value)
|
19
20
|
unless ignore_attribute?(name)
|
20
|
-
@stack.last.attributes[transform(name)] = value unless @stack.empty?
|
21
|
+
@stack.last.attributes[transform(name)] = escape(value) unless @stack.empty?
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
25
|
def text(value)
|
25
|
-
@stack.last.text = value
|
26
|
+
@stack.last.text = escape(value)
|
26
27
|
end
|
27
28
|
|
28
29
|
def end_element(name)
|
@@ -42,6 +43,10 @@ module XmlHasher
|
|
42
43
|
name
|
43
44
|
end
|
44
45
|
|
46
|
+
def escape(value)
|
47
|
+
EscapeUtils.unescape_html(value)
|
48
|
+
end
|
49
|
+
|
45
50
|
def ignore_attribute?(name)
|
46
51
|
@options[:ignore_namespaces] ? !name.to_s[/^(xmlns|xsi)/].nil? : false
|
47
52
|
end
|
data/lib/xmlhasher/node.rb
CHANGED
@@ -13,20 +13,19 @@ module XmlHasher
|
|
13
13
|
if text
|
14
14
|
h[name] = text
|
15
15
|
else
|
16
|
+
h[name] = attributes.inject({}) { |r, (key, value)| r[key] = value if !value.nil? && !value.to_s.empty?; r }
|
16
17
|
if children.size == 1
|
17
18
|
child = children.first
|
18
|
-
h[name]
|
19
|
+
h[name].merge!(child.to_hash)
|
19
20
|
else
|
20
21
|
if children.map(&:name).uniq.size == 1
|
21
|
-
h[name]
|
22
|
+
h[name].merge!(children.inject({}) { |r, child| (r[child.name] ||= []) << child.to_hash[child.name]; r })
|
22
23
|
else
|
23
|
-
h[name]
|
24
|
+
h[name].merge!(children.inject({}) { |r, child| r.merge!(child.to_hash); r })
|
24
25
|
end
|
25
26
|
end
|
26
27
|
end
|
27
|
-
|
28
|
-
h[key] = value if value
|
29
|
-
end
|
28
|
+
h[name] = nil if h[name].empty?
|
30
29
|
h
|
31
30
|
end
|
32
31
|
end
|
data/lib/xmlhasher/version.rb
CHANGED
@@ -3,42 +3,57 @@ require 'test_helper'
|
|
3
3
|
class XmlhasherTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_string_parsing_no_tranformation
|
6
|
-
hash = {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
6
|
+
hash = {
|
7
|
+
:InstitutionDetail =>
|
8
|
+
{
|
9
|
+
:address =>
|
10
|
+
{
|
11
|
+
:"ns2:address1" => "100 Main Street",
|
12
|
+
:"ns2:city" => "Anytown",
|
13
|
+
:"ns2:country" => "USA",
|
14
|
+
:"ns2:postalCode" => "94043",
|
15
|
+
:"ns2:state" => "CA"
|
16
|
+
},
|
17
|
+
:currencyCode => "ANG",
|
18
|
+
:emailAddress => "CustomerCentralBank@intuit.com",
|
19
|
+
:homeUrl => "http://www.example.com",
|
20
|
+
:institutionId => "100000",
|
21
|
+
:institutionName => "CCBank",
|
22
|
+
:keys =>
|
23
|
+
{
|
24
|
+
:key =>
|
25
|
+
[
|
26
|
+
{
|
27
|
+
:description => "2",
|
28
|
+
:displayFlag => "true",
|
29
|
+
:displayOrder => "1",
|
30
|
+
:instructions => "2",
|
31
|
+
:mask => "false",
|
32
|
+
:name => "Banking Userid",
|
33
|
+
:status => "Active",
|
34
|
+
:valueLengthMax => "100",
|
35
|
+
:valueLengthMin => "1"},
|
36
|
+
{
|
37
|
+
:description => "2",
|
38
|
+
:displayFlag => "false",
|
39
|
+
:displayOrder => "2",
|
40
|
+
:instructions => "2",
|
41
|
+
:mask => "true",
|
42
|
+
:name => "Banking Password",
|
43
|
+
:status => "Active",
|
44
|
+
:valueLengthMax => "100",
|
45
|
+
:valueLengthMin => "1"
|
46
|
+
}
|
47
|
+
]
|
48
|
+
},
|
49
|
+
:locale => {
|
50
|
+
:language => "en"
|
51
|
+
},
|
52
|
+
:phoneNumber => "123-456-7890",
|
53
|
+
:xmlns => "http://schema.intuit.com/platform/fdatafeed/account/v1",
|
54
|
+
:"xmlns:ns2" => "http://schema.intuit.com/platform/fdatafeed/common/v1"
|
55
|
+
},
|
56
|
+
}
|
42
57
|
result = XmlHasher::Parser.new.parse(fixture('institution.xml').read)
|
43
58
|
assert_equal hash, result
|
44
59
|
end
|
@@ -48,40 +63,56 @@ class XmlhasherTest < Test::Unit::TestCase
|
|
48
63
|
:snakecase => true,
|
49
64
|
:ignore_namespaces => true
|
50
65
|
}
|
51
|
-
hash = {
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
66
|
+
hash = {
|
67
|
+
:institution_detail =>
|
68
|
+
{
|
69
|
+
:address =>
|
70
|
+
{
|
71
|
+
:address1 => "100 Main Street",
|
72
|
+
:city => "Anytown",
|
73
|
+
:country => "USA",
|
74
|
+
:postal_code => "94043",
|
75
|
+
:state => "CA"
|
76
|
+
},
|
77
|
+
:currency_code => "ANG",
|
78
|
+
:email_address => "CustomerCentralBank@intuit.com",
|
79
|
+
:home_url => "http://www.example.com",
|
80
|
+
:institution_id => "100000",
|
81
|
+
:institution_name => "CCBank",
|
82
|
+
:keys =>
|
83
|
+
{
|
84
|
+
:key =>
|
85
|
+
[
|
86
|
+
{
|
87
|
+
:description => "2",
|
88
|
+
:display_flag => "true",
|
89
|
+
:display_order => "1",
|
90
|
+
:instructions => "2",
|
91
|
+
:mask => "false",
|
92
|
+
:name => "Banking Userid",
|
93
|
+
:status => "Active",
|
94
|
+
:value_length_max => "100",
|
95
|
+
:value_length_min => "1"},
|
96
|
+
{
|
97
|
+
:description => "2",
|
98
|
+
:display_flag => "false",
|
99
|
+
:display_order => "2",
|
100
|
+
:instructions => "2",
|
101
|
+
:mask => "true",
|
102
|
+
:name => "Banking Password",
|
103
|
+
:status => "Active",
|
104
|
+
:value_length_max => "100",
|
105
|
+
:value_length_min => "1"
|
106
|
+
}
|
107
|
+
]
|
108
|
+
},
|
109
|
+
:locale =>
|
110
|
+
{
|
111
|
+
:language => "en"
|
112
|
+
},
|
113
|
+
:phone_number => "123-456-7890"
|
114
|
+
}
|
115
|
+
}
|
85
116
|
result = XmlHasher::Parser.new(options).parse(fixture('institution.xml').read)
|
86
117
|
assert_equal hash, result
|
87
118
|
end
|
@@ -128,4 +159,70 @@ class XmlhasherTest < Test::Unit::TestCase
|
|
128
159
|
result = XmlHasher::Parser.new(options).parse(fixture('institution.xml'))
|
129
160
|
assert_equal hash, result
|
130
161
|
end
|
162
|
+
|
163
|
+
def test_escaped_characters
|
164
|
+
xml = %[<tag>&</tag>]
|
165
|
+
expected = {:tag => '&'}
|
166
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_simple_tag
|
170
|
+
xml = %[<tag>content</tag>]
|
171
|
+
expected = {:tag => 'content'}
|
172
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_ignore_attributes_if_has_content
|
176
|
+
xml = %[<tag a1='1' a2='2'>content</tag>]
|
177
|
+
expected = {:tag => 'content'}
|
178
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_attributes
|
182
|
+
xml = %[<tag a1='1' a2='2'></tag>]
|
183
|
+
expected = {:tag => {:a1 => '1', :a2 => '2'}}
|
184
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_attributes_and_children
|
188
|
+
xml = %[<tag a1='1' a2='2'><tag2>content</tag2></tag>]
|
189
|
+
expected = {:tag => {:a1 => '1', :a2 => '2', :tag2 => 'content'}}
|
190
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_array_detection
|
194
|
+
xml = %[<tag><tag2 attr='1' /><tag2 attr='2' /></tag>]
|
195
|
+
expected = {:tag => {:tag2 => [{:attr => '1'}, {:attr => '2'}]}}
|
196
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_single_child
|
200
|
+
xml = %[<tag><tag2 attr='1' /></tag>]
|
201
|
+
expected = {:tag => {:tag2 => {:attr => '1'}}}
|
202
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_snakecasing_attributes
|
206
|
+
options = {
|
207
|
+
:snakecase => true,
|
208
|
+
}
|
209
|
+
xml = %[<tag attr-1='1'></tag>]
|
210
|
+
expected = {:tag => {:attr_1 => '1'}}
|
211
|
+
assert_equal expected, XmlHasher::Parser.new(options).parse(xml)
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_snakecasing_elements
|
215
|
+
options = {
|
216
|
+
:snakecase => true,
|
217
|
+
}
|
218
|
+
xml = %[<my-tag><MyTag2>content</MyTag2></my-tag>]
|
219
|
+
expected = {:my_tag => {:my_tag2 => 'content'}}
|
220
|
+
assert_equal expected, XmlHasher::Parser.new(options).parse(xml)
|
221
|
+
end
|
222
|
+
|
223
|
+
def test_empty_tag
|
224
|
+
xml = %[<tag></tag>]
|
225
|
+
expected = {:tag => nil}
|
226
|
+
assert_equal expected, XmlHasher::Parser.new.parse(xml)
|
227
|
+
end
|
131
228
|
end
|
data/xmlhasher.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xmlhasher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '2.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: escape_utils
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.3.2
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.3.2
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rake
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|