smacks-apricoteatsgorilla 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require "rubygems"
2
+ require "iconv"
2
3
  require "hpricot"
3
4
 
4
5
  # Apricot eats Gorilla is a SOAP communication helper.
@@ -30,11 +31,6 @@ class ApricotEatsGorilla
30
31
  # Shortcut method for translating between XML Strings and Ruby Hashes.
31
32
  # Delegates to xml_to_hash in case +source+ is of type String or delegates
32
33
  # to hash_to_xml in case +source+ is of type Hash. Returns nil otherwise.
33
- #
34
- # ==== Parameters
35
- #
36
- # * +source+ - The XML String or Ruby Hash to translate.
37
- # * +root_node+ - Optional. Custom root node to start parsing the given XML.
38
34
  def [](source, root_node = nil)
39
35
  case source
40
36
  when String
@@ -58,11 +54,6 @@ class ApricotEatsGorilla
58
54
  # The root node itself won't be included in the Hash. Converts tag names
59
55
  # from CamelCase/lowerCamelCase to snake_case and into Symbols by default.
60
56
  #
61
- # ==== Parameters
62
- #
63
- # * +xml+ - The XML String to translate.
64
- # * +root_node+ - Optional. Custom root node to start parsing the given XML.
65
- #
66
57
  # ==== Examples
67
58
  #
68
59
  # xml = "<apricot><eats>Gorilla</eats></apricot>"
@@ -73,23 +64,29 @@ class ApricotEatsGorilla
73
64
  # ApricotEatsGorilla[xml, "//eats"]
74
65
  # # => { :lots_of => "Gorillas" }
75
66
  def xml_to_hash(xml, root_node = nil)
76
- doc = Hpricot.XML clean_xml(xml)
77
- root = root_node ? doc.at(root_node) : doc.root
67
+ doc = Hpricot.XML remove_whitespace(xml)
68
+ root = root_node ? doc.search(root_node) : doc.root
69
+
70
+ return nil if root.nil?
71
+ return xml_node_to_hash(root) unless root.respond_to? :each
78
72
 
79
- return nil if root.nil? # root_node not found
80
- if root.children.size == 1 && root.children.first.kind_of?(Hpricot::Text)
81
- return root.children.first.to_s # text-only value
73
+ if root.size == 1
74
+ return root.first.children.to_s if root.first.children.first.kind_of?(Hpricot::Text)
75
+ xml_node_to_hash(root.first)
76
+ else
77
+ root.map do |node|
78
+ if node.children.first.kind_of?(Hpricot::Text)
79
+ node.children.to_s
80
+ else
81
+ xml_node_to_hash(node)
82
+ end
83
+ end
82
84
  end
83
- xml_node_to_hash(root)
84
85
  end
85
86
 
86
87
  # Converts a given Ruby Hash into an XML String. Converts Hash keys from
87
88
  # snake_case to lowerCamelCase by default.
88
89
  #
89
- # ==== Parameters
90
- #
91
- # * +hash+ - The Ruby Hash to translate.
92
- #
93
90
  # ==== Examples
94
91
  #
95
92
  # hash = { :apricot => { :eats => "Gorilla" } }
@@ -107,10 +104,6 @@ class ApricotEatsGorilla
107
104
  # into the envelope body. Accepts a Hash (namespace => namespace_uri) of
108
105
  # additional +namespaces+ to set.
109
106
  #
110
- # ==== Parameters
111
- #
112
- # * +namespaces+ - A Hash of namespaces and their URI.
113
- #
114
107
  # ==== Examples
115
108
  #
116
109
  # ApricotEatsGorilla.soap_envelope { "<authenticate>me</authenticate>" }
@@ -157,10 +150,6 @@ class ApricotEatsGorilla
157
150
 
158
151
  # Actual implementation for xml_to_hash. Takes and iterates through a given
159
152
  # Hpricot +element+ and returns a Ruby Hash equal to the given content.
160
- #
161
- # ==== Parameters
162
- #
163
- # * +element+ - The Hpricot element to translate.
164
153
  def xml_node_to_hash(element)
165
154
  this_node = {}
166
155
  element.each_child do |child|
@@ -168,7 +157,7 @@ class ApricotEatsGorilla
168
157
  if child.children.nil? || child.children.empty?
169
158
  key, value = child.name, nil
170
159
  elsif child.children.size == 1 && child.children.first.text?
171
- key, value = child.name, to_boolean(child.children.first.to_html)
160
+ key, value = child.name, map_to_boolean(child.children.first.to_html)
172
161
  else
173
162
  key, value = child.name, xml_node_to_hash(child)
174
163
  end
@@ -191,11 +180,6 @@ class ApricotEatsGorilla
191
180
 
192
181
  # Actual implementation for hash_to_xml. Takes a Hash key +name+ and a
193
182
  # value +item+ and returns an XML String equal to the given content.
194
- #
195
- # ==== Parameters
196
- #
197
- # * +name+ - Root key from Hash to translate.
198
- # * +item+ - Root value from Hash to translate.
199
183
  def nested_data_to_xml(name, item)
200
184
  case item
201
185
  when Array
@@ -220,11 +204,6 @@ class ApricotEatsGorilla
220
204
 
221
205
  # Creates an XML tag. Expects a block for tag content. Defaults to an empty
222
206
  # element tag in case no block was supplied.
223
- #
224
- # ==== Parameters
225
- #
226
- # * +name+ - The name of the XML tag.
227
- # * +attributes+ - Optional. Hash of attributes for the XML tag.
228
207
  def tag(name, attributes = {})
229
208
  name = to_lower_camel_case(name) unless disable_tag_names_to_lower_camel_case
230
209
  if nodes_to_namespace.kind_of? Array
@@ -233,30 +212,27 @@ class ApricotEatsGorilla
233
212
  return "<#{name} />" unless block_given?
234
213
 
235
214
  attr = opt_order(attributes).map { |k, v| %Q( xmlns:#{k}="#{v}") }.to_s
236
- body = (yield && !yield.empty?) ? yield : ""
215
+ body = yield || ""
237
216
  "<#{name}#{attr}>" << body << "</#{name}>"
238
217
  end
239
218
 
240
- # Removes line breaks and whitespace between tags from a given +xml+ String.
241
- def clean_xml(xml)
242
- xml.gsub!(/\n+/, "")
243
- xml.gsub!(/(>)\s*(<)/, '\1\2')
244
- xml
219
+ # Removes whitespace between tags of a given +xml+ String.
220
+ def remove_whitespace(xml)
221
+ xml.gsub(/(>)\s*(<)/, '\1\2')
245
222
  end
246
223
 
247
224
  # Removes the namespace from a given XML +tag+.
248
225
  def remove_namespace(tag)
249
- tag.sub!(/.+:(.+)/, '\1')
250
- tag
226
+ tag.sub(/.+:(.+)/, '\1')
251
227
  end
252
228
 
253
229
  # Checks to see if a given +string+ matches "true" or "false" and converts
254
230
  # these values to actual Boolean objects. Returns the original String in
255
231
  # case it does not match "true" or "false".
256
- def to_boolean(string)
232
+ def map_to_boolean(string)
257
233
  return true if string == "true"
258
234
  return false if string == "false"
259
- string
235
+ Iconv.new("iso-8859-1", "utf-8").iconv(string)
260
236
  end
261
237
 
262
238
  # Returns a sorted version of a given +hash+ if sort_keys is enabled.
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class HashToXmlTest < Test::Unit::TestCase
3
+ class TestHashToXml < Test::Unit::TestCase
4
4
 
5
5
  context "Calling hash_to_xml" do
6
6
  setup do
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class ShortcutMethodTest < Test::Unit::TestCase
3
+ class TestShortcutMethod < Test::Unit::TestCase
4
4
 
5
5
  context "Calling []" do
6
6
  setup do
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class SoapEnvelopeTest < Test::Unit::TestCase
3
+ class TestSoapEnvelope < Test::Unit::TestCase
4
4
 
5
5
  context "Calling soap_envelope" do
6
6
  setup { ApricotEatsGorilla.sort_keys = true }
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class XmlToHashTest < Test::Unit::TestCase
3
+ class TestXmlToHash < Test::Unit::TestCase
4
4
 
5
5
  context "Calling xml_to_hash" do
6
6
  setup do
@@ -55,14 +55,24 @@ class XmlToHashTest < Test::Unit::TestCase
55
55
 
56
56
  context "with XML containing nodes with attributes" do
57
57
  should "return a Hash without tag attributes" do
58
- xml = '<todo><paint subject="chair" color="#000000">black</paint></todo>'
59
- expected = { :paint => "black" }
58
+ xml = '<root><user id="123">example</user></root>'
59
+ expected = { :user => "example" }
60
60
 
61
61
  result = ApricotEatsGorilla.xml_to_hash(xml)
62
62
  assert_equal expected, result
63
63
  end
64
64
  end
65
65
 
66
+ context "with XML containing multiple custom root nodes" do
67
+ should "return an Array containing the values of each root node" do
68
+ xml = '<root><return><id>123</id></return><return><id>456</id></return></root>'
69
+ expected = [{ :id => "123" }, { :id => "456" }]
70
+
71
+ result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
72
+ assert_equal expected, result
73
+ end
74
+ end
75
+
66
76
  context "with XML containing no subnodes at custom root node" do
67
77
  should "return the plain content of the custom root node" do
68
78
  xml = '<root><return>123</return></root>'
@@ -73,6 +83,26 @@ class XmlToHashTest < Test::Unit::TestCase
73
83
  end
74
84
  end
75
85
 
86
+ context "with XML containing no subnodes in multiple custom root nodes" do
87
+ should "return an Array containing the values of each root node" do
88
+ xml = '<root><return>123</return><return>456</return></root>'
89
+ expected = ["123", "456"]
90
+
91
+ result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
92
+ assert_equal expected, result
93
+ end
94
+ end
95
+
96
+ context "with XML containing several subnodes with the same name and text content" do
97
+ should "return a Hash containing one key and an array of Strings" do
98
+ xml = '<root><return><items>123</items><items>456</items></return></root>'
99
+ expected = { :items => ["123", "456"] }
100
+
101
+ result = ApricotEatsGorilla[xml, "//return"]
102
+ assert_equal expected, result
103
+ end
104
+ end
105
+
76
106
  context "with XML containing lowerCamelCase nodes" do
77
107
  should "convert lowerCamelCase nodes to snake_case" do
78
108
  xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "apricoteatsgorilla"))
2
+ require "test_shortcut_method"
3
+ require "test_xml_to_hash"
4
+ require "test_hash_to_xml"
5
+ require "test_soap_envelope"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smacks-apricoteatsgorilla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington
@@ -61,9 +61,9 @@ signing_key:
61
61
  specification_version: 2
62
62
  summary: Apricot eats Gorilla is a SOAP communication helper.
63
63
  test_files:
64
- - test/apricoteatsgorilla_test.rb
64
+ - test/test_apricoteatsgorilla.rb
65
65
  - test/helper.rb
66
- - test/apricoteatsgorilla/hash_to_xml_test.rb
67
- - test/apricoteatsgorilla/xml_to_hash_test.rb
68
- - test/apricoteatsgorilla/shortcut_method_test.rb
69
- - test/apricoteatsgorilla/soap_envelope_test.rb
66
+ - test/apricoteatsgorilla/test_hash_to_xml.rb
67
+ - test/apricoteatsgorilla/test_xml_to_hash.rb
68
+ - test/apricoteatsgorilla/test_shortcut_method.rb
69
+ - test/apricoteatsgorilla/test_soap_envelope.rb
@@ -1,5 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), "apricoteatsgorilla"))
2
- require "shortcut_method_test"
3
- require "xml_to_hash_test"
4
- require "hash_to_xml_test"
5
- require "soap_envelope_test"