smacks-apricoteatsgorilla 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/apricoteatsgorilla.rb +4 -11
- data/test/apricoteatsgorilla/xml_to_hash_test.rb +0 -11
- metadata +1 -1
data/lib/apricoteatsgorilla.rb
CHANGED
@@ -74,18 +74,11 @@ class ApricotEatsGorilla
|
|
74
74
|
# # => { :lots_of => "Gorillas" }
|
75
75
|
def xml_to_hash(xml, root_node = nil)
|
76
76
|
doc = Hpricot.XML clean_xml(xml)
|
77
|
-
root = root_node ? doc.
|
77
|
+
root = root_node ? doc.at(root_node) : doc.root
|
78
78
|
|
79
|
-
return nil if root.nil?
|
80
|
-
|
81
|
-
|
82
|
-
# root is an Hpricot::Elements object with only one element
|
83
|
-
return root.entries.map { |e| xml_node_to_hash(e) } if root.count > 1
|
84
|
-
root = root.entries.first
|
85
|
-
if root.children.size == 1 && root.children.first.text?
|
86
|
-
# root contains text only
|
87
|
-
return root.children.first.to_s
|
88
|
-
end
|
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
|
89
82
|
end
|
90
83
|
xml_node_to_hash(root)
|
91
84
|
end
|
@@ -73,17 +73,6 @@ class XmlToHashTest < Test::Unit::TestCase
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
context "with XML containing multiple custom root nodes" do
|
77
|
-
should "return an Array containing the multiple root nodes" do
|
78
|
-
xml = '<root><return><id>3</id><name>Julia</name></return>' <<
|
79
|
-
'<return><id>5</id><name>Gorilla</name></return></root>'
|
80
|
-
expected = [{ :id => "3", :name => "Julia"}, { :id => "5", :name => "Gorilla"}]
|
81
|
-
|
82
|
-
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
|
83
|
-
assert_equal expected, result
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
76
|
context "with XML containing lowerCamelCase nodes" do
|
88
77
|
should "convert lowerCamelCase nodes to snake_case" do
|
89
78
|
xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
|