smacks-apricoteatsgorilla 0.2.2 → 0.2.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/README.rdoc +8 -4
- data/lib/apricoteatsgorilla.rb +21 -7
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -19,7 +19,7 @@ but it quickly evolved into a more general translation tool.
|
|
19
19
|
|
20
20
|
Let's assume you receive the following SOAP response:
|
21
21
|
|
22
|
-
|
22
|
+
xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
23
23
|
<soap:Body>
|
24
24
|
<ns2:findCustomerByIdResponse xmlns:ns2="http://v1_0.ws.example.com/">
|
25
25
|
<return>
|
@@ -34,7 +34,7 @@ Let's assume you receive the following SOAP response:
|
|
34
34
|
</return>
|
35
35
|
</ns2:findCustomerByIdResponse>
|
36
36
|
</soap:Body>
|
37
|
-
</soap:Envelope>
|
37
|
+
</soap:Envelope>'
|
38
38
|
|
39
39
|
Just pass in the raw XML string:
|
40
40
|
|
@@ -42,6 +42,10 @@ Just pass in the raw XML string:
|
|
42
42
|
require "apricoteatsgorilla
|
43
43
|
hash = ApricotEatsGorilla.xml_to_hash(xml, "//return")
|
44
44
|
|
45
|
+
Or use the shortcut:
|
46
|
+
|
47
|
+
hash = ApricotEatsGorilla(xml, "//return")
|
48
|
+
|
45
49
|
And it gets converted into a nice little Hash:
|
46
50
|
|
47
51
|
"empty" => false,
|
@@ -56,8 +60,8 @@ And it gets converted into a nice little Hash:
|
|
56
60
|
== The conclusion
|
57
61
|
|
58
62
|
* The xml_to_hash method starts parsing the XML at the root node by default.
|
59
|
-
By calling the method with an XPath expression as second parameter, we
|
60
|
-
a custom root node.
|
63
|
+
By calling the method with an XPath expression as second parameter, we're
|
64
|
+
able to define a custom root node.
|
61
65
|
* Node attributes are ignored and won't be included in the hash.
|
62
66
|
* The value of empty element nodes will be nil.
|
63
67
|
* Node values of "true" or "false" will be converted to boolean objects.
|
data/lib/apricoteatsgorilla.rb
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
require 'rubygems'
|
3
3
|
require 'hpricot'
|
4
4
|
|
5
|
-
# Apricot eats Gorilla translates between XML documents and
|
5
|
+
# Apricot eats Gorilla translates between XML documents and Hashes.
|
6
6
|
# It's based on CobraVsMongoose but uses Hpricot instead of REXML to parse
|
7
7
|
# XML and it also doesn't follow the BadgerFish convention.
|
8
8
|
#
|
9
|
-
# Its initial purpose was to convert SOAP response messages to
|
9
|
+
# Its initial purpose was to convert SOAP response messages to Hashes,
|
10
10
|
# but it quickly evolved into a more general translation tool.
|
11
11
|
class ApricotEatsGorilla
|
12
12
|
|
13
|
-
# Converts XML into a
|
13
|
+
# Converts XML into a Hash. Starts parsing at root node by default.
|
14
14
|
# Call with XPath expession (Hpricot search) as second parameter to define
|
15
15
|
# a custom root node. The root node itself will not be included in the Hash.
|
16
16
|
#
|
@@ -25,7 +25,7 @@ class ApricotEatsGorilla
|
|
25
25
|
xml_node_to_hash(root)
|
26
26
|
end
|
27
27
|
|
28
|
-
# Converts a
|
28
|
+
# Converts a Hash to XML.
|
29
29
|
#
|
30
30
|
# E.g.
|
31
31
|
# hash = { "dude" => { "likes" => "beer", "hates" => "appletini" } }
|
@@ -37,7 +37,7 @@ class ApricotEatsGorilla
|
|
37
37
|
|
38
38
|
private
|
39
39
|
|
40
|
-
# Converts XML
|
40
|
+
# Converts XML into a Hash.
|
41
41
|
def self.xml_node_to_hash(node)
|
42
42
|
this_node = {}
|
43
43
|
|
@@ -64,9 +64,8 @@ private
|
|
64
64
|
this_node
|
65
65
|
end
|
66
66
|
|
67
|
-
# Converts a
|
67
|
+
# Converts a Hash to XML.
|
68
68
|
def self.nested_data_to_xml(name, item)
|
69
|
-
children = {}
|
70
69
|
case item
|
71
70
|
when String
|
72
71
|
make_tag(name) { item }
|
@@ -114,3 +113,18 @@ private
|
|
114
113
|
end
|
115
114
|
|
116
115
|
end
|
116
|
+
|
117
|
+
# Shortcut method for translating between XML documents and Hashes.
|
118
|
+
# Calls xml_to_hash in case the first parameter is of type String.
|
119
|
+
# Calls hash_to_xml in case the first parameter is of type Hash.
|
120
|
+
# Returns nil otherwise.
|
121
|
+
def ApricotEatsGorilla(source, root_node = nil)
|
122
|
+
case source
|
123
|
+
when String
|
124
|
+
ApricotEatsGorilla.xml_to_hash(source, root_node)
|
125
|
+
when Hash
|
126
|
+
ApricotEatsGorilla.hash_to_xml(source)
|
127
|
+
else
|
128
|
+
nil
|
129
|
+
end
|
130
|
+
end
|