smacks-apricoteatsgorilla 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +48 -7
- data/lib/apricoteatsgorilla.rb +36 -60
- data/test/apricoteatsgorilla/xml_to_hash_test.rb +21 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
Apricot eats Gorilla is a SOAP communication helper.
|
4
4
|
|
5
|
-
It translates between SOAP
|
6
|
-
|
7
|
-
uses Hpricot instead of REXML
|
5
|
+
It translates between SOAP messages (XML) and Ruby Hashes while offering some
|
6
|
+
helpful methods for working with SOAP webservices. Apricot eats Gorilla was
|
7
|
+
initially based on CobraVsMongoose but uses Hpricot instead of REXML.
|
8
8
|
|
9
9
|
== Install
|
10
10
|
|
@@ -18,6 +18,12 @@ uses Hpricot instead of REXML and doesn't follow the BadgerFish convention.
|
|
18
18
|
|
19
19
|
=== xml_to_hash(xml, root_node = nil)
|
20
20
|
|
21
|
+
Converts a given XML String into a Ruby Hash. Starts parsing at root node by
|
22
|
+
default. The optional root_node parameter can be used to specify a custom root
|
23
|
+
node to start parsing at using an XPath expression (Hpricot search). The root
|
24
|
+
node itself won't be included in the Hash. Converts tag names from CamelCase/
|
25
|
+
lowerCamelCase to snake_case and into Symbols by default.
|
26
|
+
|
21
27
|
xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
22
28
|
<soap:Body>
|
23
29
|
<ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
|
@@ -36,13 +42,20 @@ uses Hpricot instead of REXML and doesn't follow the BadgerFish convention.
|
|
36
42
|
|
37
43
|
=== hash_to_xml(hash)
|
38
44
|
|
39
|
-
|
45
|
+
Converts a given Ruby Hash into an XML String. Converts Hash keys from snake_case
|
46
|
+
to lowerCamelCase by default.
|
47
|
+
|
48
|
+
hash = { :apricot => { :eats => { :lots_of => "Gorillas" } } }
|
40
49
|
|
41
50
|
ApricotEatsGorilla[hash]
|
42
|
-
# => "<apricot><eats>
|
51
|
+
# => "<apricot><eats><lotsOf>Gorillas</lotsOf></eats></apricot>"
|
43
52
|
|
44
53
|
=== soap_envelope(namespaces = {})
|
45
54
|
|
55
|
+
Builds a SOAP request envelope and includes the content from a given block
|
56
|
+
into the envelope body. Accepts a Hash (namespace => namespace_uri) of additional
|
57
|
+
namespaces to set.
|
58
|
+
|
46
59
|
ApricotEatsGorilla.soap_envelope { "<authenticate>me</authenticate>" }
|
47
60
|
|
48
61
|
# => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
|
@@ -51,6 +64,34 @@ uses Hpricot instead of REXML and doesn't follow the BadgerFish convention.
|
|
51
64
|
# => </env:Body>
|
52
65
|
# => </env:Envelope>'
|
53
66
|
|
54
|
-
==
|
67
|
+
== Changing defaults
|
68
|
+
|
69
|
+
There are several options for changing the default behavior. Options can be
|
70
|
+
set using a setup block or by setting one option at a time.
|
71
|
+
|
72
|
+
ApricotEatsGorilla.setup do |s|
|
73
|
+
s.disable_tag_names_to_lower_camel_case = true
|
74
|
+
s.disable_hash_keys_to_snake_case = true
|
75
|
+
s.disable_hash_keys_to_symbol = true
|
76
|
+
end
|
77
|
+
|
78
|
+
=== Disable conversion of tag names to lowerCamelCase
|
79
|
+
|
80
|
+
By default XML tags created by hash_to_xml are converted from snake_case to
|
81
|
+
lowerCamelCase. Disable by setting disable_tag_names_to_lower_camel_case to true.
|
82
|
+
|
83
|
+
ApricotEatsGorilla.disable_tag_names_to_lower_camel_case = true
|
84
|
+
|
85
|
+
=== Disable conversion of Hash keys to snake_case
|
86
|
+
|
87
|
+
By default Hash keys created by xml_to_hash are converted from lowerCamelCase/
|
88
|
+
CamelCase to snake_case. Disable by setting disable_hash_keys_to_snake_case to true.
|
89
|
+
|
90
|
+
ApricotEatsGorilla.disable_hash_keys_to_snake_case = true
|
91
|
+
|
92
|
+
=== Disable conversion of Hash keys to Symbols
|
93
|
+
|
94
|
+
By default Hash keys created by xml_to_hash are converted to Symbols.
|
95
|
+
Disable by setting disable_hash_keys_to_symbol to true.
|
55
96
|
|
56
|
-
|
97
|
+
ApricotEatsGorilla.disable_hash_keys_to_symbol = true
|
data/lib/apricoteatsgorilla.rb
CHANGED
@@ -3,48 +3,13 @@ require "hpricot"
|
|
3
3
|
|
4
4
|
# Apricot eats Gorilla is a SOAP communication helper.
|
5
5
|
#
|
6
|
-
# It translates between SOAP
|
7
|
-
#
|
8
|
-
# uses Hpricot instead of REXML
|
9
|
-
#
|
10
|
-
# === xml_to_hash(xml, root_node = nil)
|
11
|
-
#
|
12
|
-
# xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
13
|
-
# <soap:Body>
|
14
|
-
# <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
|
15
|
-
# <return>
|
16
|
-
# <authValue>
|
17
|
-
# <token>secret</token>
|
18
|
-
# <client>example</client>
|
19
|
-
# </authValue>
|
20
|
-
# </return>
|
21
|
-
# </ns2:authenticateResponse>
|
22
|
-
# </soap:Body>
|
23
|
-
# </soap:Envelope>'
|
24
|
-
#
|
25
|
-
# ApricotEatsGorilla[xml, "//return"]
|
26
|
-
# # => { :auth_value => { :token => "secret", :client => "example" } }
|
27
|
-
#
|
28
|
-
# === hash_to_xml(hash)
|
29
|
-
#
|
30
|
-
# hash = { :apricot => { :eats => "Gorilla" } }
|
31
|
-
#
|
32
|
-
# ApricotEatsGorilla[hash]
|
33
|
-
# # => "<apricot><eats>Gorilla</eats></apricot>"
|
34
|
-
#
|
35
|
-
# === soap_envelope(namespaces = {})
|
36
|
-
#
|
37
|
-
# ApricotEatsGorilla.soap_envelope { "<authenticate>me</authenticate>" }
|
38
|
-
#
|
39
|
-
# # => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
|
40
|
-
# # => <env:Body>
|
41
|
-
# # => <authenticate>me</authenticate>
|
42
|
-
# # => </env:Body>
|
43
|
-
# # => </env:Envelope>'
|
6
|
+
# It translates between SOAP messages (XML) and Ruby Hashes while offering some
|
7
|
+
# helpful methods for working with SOAP webservices. Apricot eats Gorilla was
|
8
|
+
# initially based on CobraVsMongoose but uses Hpricot instead of REXML.
|
44
9
|
class ApricotEatsGorilla
|
45
10
|
class << self # Class methods
|
46
11
|
|
47
|
-
# Flag to enable
|
12
|
+
# Flag to enable sorting of Hash keys.
|
48
13
|
attr_accessor :sort_keys
|
49
14
|
|
50
15
|
# Flag to disable conversion of tag names to lowerCamelCase.
|
@@ -59,7 +24,7 @@ class ApricotEatsGorilla
|
|
59
24
|
# Array of XML nodes to add a namespace to.
|
60
25
|
attr_accessor :nodes_to_namespace
|
61
26
|
|
62
|
-
# The namespace for nodes
|
27
|
+
# The namespace for nodes set by nodes_to_namespace.
|
63
28
|
attr_accessor :node_namespace
|
64
29
|
|
65
30
|
# Shortcut method for translating between XML Strings and Ruby Hashes.
|
@@ -86,15 +51,16 @@ class ApricotEatsGorilla
|
|
86
51
|
def setup
|
87
52
|
yield self if block_given?
|
88
53
|
end
|
89
|
-
|
54
|
+
|
90
55
|
# Converts a given +xml+ String into a Ruby Hash. Starts parsing at root
|
91
56
|
# node by default. The optional +root_node+ parameter can be used to specify
|
92
|
-
# a custom root node to start parsing at
|
93
|
-
# The root node itself won't be included in the Hash.
|
57
|
+
# a custom root node to start parsing at using an XPath (Hpricot search).
|
58
|
+
# The root node itself won't be included in the Hash. Converts tag names
|
59
|
+
# from CamelCase/lowerCamelCase to snake_case and into Symbols by default.
|
94
60
|
#
|
95
61
|
# ==== Parameters
|
96
62
|
#
|
97
|
-
# * +xml+ - The XML String to translate
|
63
|
+
# * +xml+ - The XML String to translate.
|
98
64
|
# * +root_node+ - Optional. Custom root node to start parsing the given XML.
|
99
65
|
#
|
100
66
|
# ==== Examples
|
@@ -107,19 +73,29 @@ class ApricotEatsGorilla
|
|
107
73
|
# ApricotEatsGorilla[xml, "//eats"]
|
108
74
|
# # => { :lots_of => "Gorillas" }
|
109
75
|
def xml_to_hash(xml, root_node = nil)
|
110
|
-
|
111
|
-
|
112
|
-
root = root_node ? doc.at(root_node) : doc.root
|
76
|
+
doc = Hpricot.XML clean_xml(xml)
|
77
|
+
root = root_node ? doc.search(root_node) : doc.root
|
113
78
|
|
114
79
|
return nil if root.nil?
|
80
|
+
# root is an Hpricot::Elements object
|
81
|
+
if root.respond_to?(:count)
|
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
|
89
|
+
end
|
115
90
|
xml_node_to_hash(root)
|
116
91
|
end
|
117
92
|
|
118
|
-
# Converts a given
|
93
|
+
# Converts a given Ruby Hash into an XML String. Converts Hash keys from
|
94
|
+
# snake_case to lowerCamelCase by default.
|
119
95
|
#
|
120
96
|
# ==== Parameters
|
121
97
|
#
|
122
|
-
# * +hash+ - The Ruby Hash to translate
|
98
|
+
# * +hash+ - The Ruby Hash to translate.
|
123
99
|
#
|
124
100
|
# ==== Examples
|
125
101
|
#
|
@@ -134,13 +110,13 @@ class ApricotEatsGorilla
|
|
134
110
|
nested_data_to_xml(hash.keys.first, hash.values.first)
|
135
111
|
end
|
136
112
|
|
137
|
-
# Builds a SOAP request envelope and includes the content from a given
|
138
|
-
#
|
139
|
-
#
|
113
|
+
# Builds a SOAP request envelope and includes the content from a given +block+
|
114
|
+
# into the envelope body. Accepts a Hash (namespace => namespace_uri) of
|
115
|
+
# additional +namespaces+ to set.
|
140
116
|
#
|
141
117
|
# ==== Parameters
|
142
118
|
#
|
143
|
-
# * +namespaces+ - A Hash of namespaces and their
|
119
|
+
# * +namespaces+ - A Hash of namespaces and their URI.
|
144
120
|
#
|
145
121
|
# ==== Examples
|
146
122
|
#
|
@@ -191,7 +167,7 @@ class ApricotEatsGorilla
|
|
191
167
|
#
|
192
168
|
# ==== Parameters
|
193
169
|
#
|
194
|
-
# * +element+ - The Hpricot element to translate
|
170
|
+
# * +element+ - The Hpricot element to translate.
|
195
171
|
def xml_node_to_hash(element)
|
196
172
|
this_node = {}
|
197
173
|
element.each_child do |child|
|
@@ -199,7 +175,7 @@ class ApricotEatsGorilla
|
|
199
175
|
if child.children.nil? || child.children.empty?
|
200
176
|
key, value = child.name, nil
|
201
177
|
elsif child.children.size == 1 && child.children.first.text?
|
202
|
-
key, value = child.name,
|
178
|
+
key, value = child.name, to_boolean(child.children.first.to_html)
|
203
179
|
else
|
204
180
|
key, value = child.name, xml_node_to_hash(child)
|
205
181
|
end
|
@@ -225,8 +201,8 @@ class ApricotEatsGorilla
|
|
225
201
|
#
|
226
202
|
# ==== Parameters
|
227
203
|
#
|
228
|
-
# * +name+ -
|
229
|
-
# * +item+ -
|
204
|
+
# * +name+ - Root key from Hash to translate.
|
205
|
+
# * +item+ - Root value from Hash to translate.
|
230
206
|
def nested_data_to_xml(name, item)
|
231
207
|
case item
|
232
208
|
when Array
|
@@ -282,15 +258,15 @@ class ApricotEatsGorilla
|
|
282
258
|
end
|
283
259
|
|
284
260
|
# Checks to see if a given +string+ matches "true" or "false" and converts
|
285
|
-
# these values to actual Boolean objects. Returns the original
|
261
|
+
# these values to actual Boolean objects. Returns the original String in
|
286
262
|
# case it does not match "true" or "false".
|
287
|
-
def
|
263
|
+
def to_boolean(string)
|
288
264
|
return true if string == "true"
|
289
265
|
return false if string == "false"
|
290
266
|
string
|
291
267
|
end
|
292
268
|
|
293
|
-
# Returns a sorted version of a given +hash+ if
|
269
|
+
# Returns a sorted version of a given +hash+ if sort_keys is enabled.
|
294
270
|
def opt_order(hash)
|
295
271
|
return hash unless sort_keys
|
296
272
|
hash.keys.sort_by { |s| s.to_s }.map { |key| [key, hash[key]] }
|
@@ -63,6 +63,27 @@ class XmlToHashTest < Test::Unit::TestCase
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
context "with XML containing no subnodes at custom root node" do
|
67
|
+
should "return the plain content of the custom root node" do
|
68
|
+
xml = '<root><return>123</return></root>'
|
69
|
+
expected = "123"
|
70
|
+
|
71
|
+
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
|
72
|
+
assert_equal expected, result
|
73
|
+
end
|
74
|
+
end
|
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
|
+
|
66
87
|
context "with XML containing lowerCamelCase nodes" do
|
67
88
|
should "convert lowerCamelCase nodes to snake_case" do
|
68
89
|
xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
|