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.
@@ -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
@@ -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] = child.to_hash
19
+ h[name].merge!(child.to_hash)
19
20
  else
20
21
  if children.map(&:name).uniq.size == 1
21
- h[name] = children.inject({}) { |r, child| (r[child.name] ||= []) << child.to_hash[child.name]; r }
22
+ h[name].merge!(children.inject({}) { |r, child| (r[child.name] ||= []) << child.to_hash[child.name]; r })
22
23
  else
23
- h[name] = children.inject({}) { |r, child| r.merge!(child.to_hash); r }
24
+ h[name].merge!(children.inject({}) { |r, child| r.merge!(child.to_hash); r })
24
25
  end
25
26
  end
26
27
  end
27
- attributes.each do |key, value|
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
@@ -1,3 +1,3 @@
1
1
  module XmlHasher
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -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 = {:InstitutionDetail =>
7
- {:address =>
8
- {:"ns2:address1" => "100 Main Street",
9
- :"ns2:city" => "Anytown",
10
- :"ns2:country" => "USA",
11
- :"ns2:postalCode" => "94043",
12
- :"ns2:state" => "CA"},
13
- :currencyCode => "ANG",
14
- :emailAddress => "CustomerCentralBank@intuit.com",
15
- :homeUrl => "http://www.example.com",
16
- :institutionId => "100000",
17
- :institutionName => "CCBank",
18
- :keys =>
19
- {:key =>
20
- [{:description => "2",
21
- :displayFlag => "true",
22
- :displayOrder => "1",
23
- :instructions => "2",
24
- :mask => "false",
25
- :name => "Banking Userid",
26
- :status => "Active",
27
- :valueLengthMax => "100",
28
- :valueLengthMin => "1"},
29
- {:description => "2",
30
- :displayFlag => "false",
31
- :displayOrder => "2",
32
- :instructions => "2",
33
- :mask => "true",
34
- :name => "Banking Password",
35
- :status => "Active",
36
- :valueLengthMax => "100",
37
- :valueLengthMin => "1"}]},
38
- :locale => {:language => "en"},
39
- :phoneNumber => "123-456-7890"},
40
- :xmlns => "http://schema.intuit.com/platform/fdatafeed/account/v1",
41
- :"xmlns:ns2" => "http://schema.intuit.com/platform/fdatafeed/common/v1"}
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 = {:institution_detail =>
52
- {:address =>
53
- {:address1 => "100 Main Street",
54
- :city => "Anytown",
55
- :country => "USA",
56
- :postal_code => "94043",
57
- :state => "CA"},
58
- :currency_code => "ANG",
59
- :email_address => "CustomerCentralBank@intuit.com",
60
- :home_url => "http://www.example.com",
61
- :institution_id => "100000",
62
- :institution_name => "CCBank",
63
- :keys =>
64
- {:key =>
65
- [{:description => "2",
66
- :display_flag => "true",
67
- :display_order => "1",
68
- :instructions => "2",
69
- :mask => "false",
70
- :name => "Banking Userid",
71
- :status => "Active",
72
- :value_length_max => "100",
73
- :value_length_min => "1"},
74
- {:description => "2",
75
- :display_flag => "false",
76
- :display_order => "2",
77
- :instructions => "2",
78
- :mask => "true",
79
- :name => "Banking Password",
80
- :status => "Active",
81
- :value_length_max => "100",
82
- :value_length_min => "1"}]},
83
- :locale => {:language => "en"},
84
- :phone_number => "123-456-7890"}}
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>&amp;</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
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.required_rubygems_version = '>= 1.3.6'
23
23
 
24
24
  spec.add_dependency 'ox', '>= 2.0'
25
+ spec.add_dependency 'escape_utils', '>= 0.3.2'
25
26
 
26
27
  spec.add_development_dependency 'rake'
27
28
  spec.add_development_dependency 'bundler'
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.2
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