smacks-apricoteatsgorilla 0.3.9 → 0.3.31
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/apricoteatsgorilla.rb +15 -49
- data/{test → tests}/apricoteatsgorilla_test.rb +0 -0
- metadata +2 -7
- data/test/apricoteatsgorilla/hash_to_xml_test.rb +0 -91
- data/test/apricoteatsgorilla/shortcut_method_test.rb +0 -46
- data/test/apricoteatsgorilla/soap_envelope_test.rb +0 -33
- data/test/apricoteatsgorilla/xml_to_hash_test.rb +0 -111
- data/test/helper.rb +0 -5
data/README.rdoc
CHANGED
data/lib/apricoteatsgorilla.rb
CHANGED
@@ -47,21 +47,6 @@ class ApricotEatsGorilla
|
|
47
47
|
# Flag to enable optional sorting of Hash keys.
|
48
48
|
attr_accessor :sort_keys
|
49
49
|
|
50
|
-
# Flag to disable conversion of tag names to lowerCamelCase.
|
51
|
-
attr_accessor :disable_tag_names_to_lower_camel_case
|
52
|
-
|
53
|
-
# Flag to disable conversion of Hash keys to snake_case.
|
54
|
-
attr_accessor :disable_hash_keys_to_snake_case
|
55
|
-
|
56
|
-
# Flag to disable conversion of Hash keys to Symbols.
|
57
|
-
attr_accessor :disable_hash_keys_to_symbol
|
58
|
-
|
59
|
-
# Array of XML nodes to add a namespace to.
|
60
|
-
attr_accessor :nodes_to_namespace
|
61
|
-
|
62
|
-
# The namespace for nodes in :nodes_to_namespace.
|
63
|
-
attr_accessor :node_namespace
|
64
|
-
|
65
50
|
# Shortcut method for translating between XML Strings and Ruby Hashes.
|
66
51
|
# Delegates to xml_to_hash in case +source+ is of type String or delegates
|
67
52
|
# to hash_to_xml in case +source+ is of type Hash. Returns nil otherwise.
|
@@ -81,12 +66,6 @@ class ApricotEatsGorilla
|
|
81
66
|
end
|
82
67
|
end
|
83
68
|
|
84
|
-
# Yields this class object in case a +block+ was given. Nice way for setting
|
85
|
-
# multiple options at once.
|
86
|
-
def setup
|
87
|
-
yield self if block_given?
|
88
|
-
end
|
89
|
-
|
90
69
|
# Converts a given +xml+ String into a Ruby Hash. Starts parsing at root
|
91
70
|
# node by default. The optional +root_node+ parameter can be used to specify
|
92
71
|
# a custom root node to start parsing at via XPath (Hpricot search).
|
@@ -172,18 +151,6 @@ class ApricotEatsGorilla
|
|
172
151
|
end
|
173
152
|
end
|
174
153
|
|
175
|
-
# Converts a given +string+ from CamelCase/lowerCamelCase to snake_case.
|
176
|
-
def to_snake_case(string)
|
177
|
-
string = string.gsub(/[A-Z]+/, '\1_\0').downcase
|
178
|
-
string = string[1, string.length-1] if string[0, 1] == "_"
|
179
|
-
string
|
180
|
-
end
|
181
|
-
|
182
|
-
# Converts a given +string+ from snake_case to lowerCamelCase.
|
183
|
-
def to_lower_camel_case(string)
|
184
|
-
string.to_s.gsub(/_(.)/) { $1.upcase }
|
185
|
-
end
|
186
|
-
|
187
154
|
private
|
188
155
|
|
189
156
|
# Actual implementation for xml_to_hash. Takes and iterates through a given
|
@@ -204,9 +171,7 @@ class ApricotEatsGorilla
|
|
204
171
|
key, value = child.name, xml_node_to_hash(child)
|
205
172
|
end
|
206
173
|
|
207
|
-
key = remove_namespace(key)
|
208
|
-
key = to_snake_case(key) unless disable_hash_keys_to_snake_case
|
209
|
-
key = key.intern unless disable_hash_keys_to_symbol
|
174
|
+
key = to_snake_case(remove_namespace(key)).intern
|
210
175
|
current = this_node[key]
|
211
176
|
case current
|
212
177
|
when Array
|
@@ -229,16 +194,16 @@ class ApricotEatsGorilla
|
|
229
194
|
# * +item+ - A Hash value to translate into an XML String.
|
230
195
|
def nested_data_to_xml(name, item)
|
231
196
|
case item
|
232
|
-
when String
|
233
|
-
tag(name) { item
|
197
|
+
when String
|
198
|
+
tag(name) { item }
|
234
199
|
when Array
|
235
200
|
item.map { |subitem| nested_data_to_xml(name, subitem) }.join
|
236
201
|
when Hash
|
237
202
|
tag(name) do
|
238
203
|
opt_order(item).map { |tag, value|
|
239
204
|
case value
|
240
|
-
when String
|
241
|
-
tag(tag) { value
|
205
|
+
when String
|
206
|
+
tag(tag) { value }
|
242
207
|
when Array
|
243
208
|
value.map { |subitem| nested_data_to_xml(tag, subitem) }.join
|
244
209
|
when Hash
|
@@ -257,10 +222,6 @@ class ApricotEatsGorilla
|
|
257
222
|
# * +name+ - The name of the XML tag.
|
258
223
|
# * +attributes+ - Optional. Hash of attributes for the XML tag.
|
259
224
|
def tag(name, attributes = {})
|
260
|
-
name = to_lower_camel_case(name) unless disable_tag_names_to_lower_camel_case
|
261
|
-
if nodes_to_namespace.kind_of? Array
|
262
|
-
name = "#{node_namespace}:#{name}" if node_namespace && nodes_to_namespace.include?(name)
|
263
|
-
end
|
264
225
|
return "<#{name} />" unless block_given?
|
265
226
|
|
266
227
|
attr = opt_order(attributes).map { |k, v| %Q( xmlns:#{k}="#{v}") }.to_s
|
@@ -270,15 +231,20 @@ class ApricotEatsGorilla
|
|
270
231
|
|
271
232
|
# Removes line breaks and whitespace between tags from a given +xml+ String.
|
272
233
|
def clean_xml(xml)
|
273
|
-
xml.gsub
|
274
|
-
xml.gsub
|
275
|
-
xml
|
234
|
+
xml = xml.gsub(/\n+/, "")
|
235
|
+
xml.gsub(/(>)\s*(<)/, '\1\2')
|
276
236
|
end
|
277
237
|
|
278
238
|
# Removes the namespace from a given XML +tag+.
|
279
239
|
def remove_namespace(tag)
|
280
|
-
tag.sub
|
281
|
-
|
240
|
+
tag.sub(/.+:(.+)/, '\1')
|
241
|
+
end
|
242
|
+
|
243
|
+
# Converts a given +string+ from CamelCase/lowerCamelCase to snake_case.
|
244
|
+
def to_snake_case(string)
|
245
|
+
string = string.gsub(/[A-Z]+/, '\1_\0').downcase
|
246
|
+
string = string[1, string.length-1] if string[0, 1] == "_"
|
247
|
+
string
|
282
248
|
end
|
283
249
|
|
284
250
|
# Checks to see if a given +string+ matches "true" or "false" and converts
|
File without changes
|
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.3.
|
4
|
+
version: 0.3.31
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Harrington
|
@@ -61,9 +61,4 @@ signing_key:
|
|
61
61
|
specification_version: 2
|
62
62
|
summary: Apricot eats Gorilla is a SOAP communication helper.
|
63
63
|
test_files:
|
64
|
-
-
|
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
|
64
|
+
- tests/apricoteatsgorilla_test.rb
|
@@ -1,91 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "helper")
|
2
|
-
|
3
|
-
class HashToXmlTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "Calling hash_to_xml" do
|
6
|
-
setup do
|
7
|
-
ApricotEatsGorilla.setup do |s|
|
8
|
-
s.sort_keys = true
|
9
|
-
s.disable_tag_names_to_lower_camel_case = false
|
10
|
-
s.disable_hash_keys_to_snake_case = false
|
11
|
-
s.disable_hash_keys_to_symbol = false
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context "with a Hash consisting of a single key-value-pair" do
|
16
|
-
should "return an XML String containing one node and a value" do
|
17
|
-
hash = { "apricot" => "eats gorilla" }
|
18
|
-
expected = "<apricot>eats gorilla</apricot>"
|
19
|
-
|
20
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
21
|
-
assert_equal expected, result
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "with a Hash containing another Hash" do
|
26
|
-
should "return an XML String representing the given structure" do
|
27
|
-
hash = { "apricot" => { "eats" => "gorilla", "drinks" => "beer" } }
|
28
|
-
expected = "<apricot><drinks>beer</drinks><eats>gorilla</eats></apricot>"
|
29
|
-
|
30
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
31
|
-
assert_equal expected, result
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "with a Hash containing a Hash containing an Array" do
|
36
|
-
should "return an XML String representing the given structure" do
|
37
|
-
hash = { "apricot" => { "eats" => [ "gorilla", "snake" ] } }
|
38
|
-
expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
|
39
|
-
|
40
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
41
|
-
assert_equal expected, result
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
context "with a Hash containing a Hash containing an Array containing a Hash" do
|
46
|
-
should "return an XML String representing the given structure" do
|
47
|
-
hash = { "apricot" =>
|
48
|
-
{ "eats" => [ { "lotsOf" => "gorillas" }, { "justSome" => "snakes" } ]
|
49
|
-
} }
|
50
|
-
expected = "<apricot><eats><lotsOf>gorillas</lotsOf></eats>" <<
|
51
|
-
"<eats><justSome>snakes</justSome></eats></apricot>"
|
52
|
-
|
53
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
54
|
-
assert_equal expected, result
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context "with a Hash containing Symbols" do
|
59
|
-
should "returns an XML String with Symbols converted into Strings" do
|
60
|
-
hash = { :apricot => { :eats => [ :gorilla, "snake" ] } }
|
61
|
-
expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
|
62
|
-
|
63
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
64
|
-
assert_equal expected, result
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context "with a Hash containing snake_case keys" do
|
69
|
-
should "convert snake_case Hash keys to lowerCamelCase" do
|
70
|
-
hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
|
71
|
-
expected = "<apricot><eats><lotsOf>gorillas</lotsOf></eats></apricot>"
|
72
|
-
|
73
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
74
|
-
assert_equal expected, result
|
75
|
-
end
|
76
|
-
|
77
|
-
context "and converting snake_case tag names to lowerCamelCase turned off" do
|
78
|
-
setup { ApricotEatsGorilla.disable_tag_names_to_lower_camel_case = true }
|
79
|
-
|
80
|
-
should "not convert snake_case tag names to lowerCamelCase" do
|
81
|
-
hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
|
82
|
-
expected = "<apricot><eats><lots_of>gorillas</lots_of></eats></apricot>"
|
83
|
-
|
84
|
-
result = ApricotEatsGorilla.hash_to_xml(hash)
|
85
|
-
assert_equal expected, result
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "helper")
|
2
|
-
|
3
|
-
class ShortcutMethodTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "Calling []" do
|
6
|
-
setup do
|
7
|
-
ApricotEatsGorilla.setup do |s|
|
8
|
-
s.sort_keys = true
|
9
|
-
s.disable_tag_names_to_lower_camel_case = false
|
10
|
-
s.disable_hash_keys_to_snake_case = false
|
11
|
-
s.disable_hash_keys_to_symbol = false
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context "with an XML String" do
|
16
|
-
should "return a Hash containing the XML content" do
|
17
|
-
xml = "<root><name>Jungle Julia</name></root>"
|
18
|
-
expected = { :name => "Jungle Julia" }
|
19
|
-
|
20
|
-
result = ApricotEatsGorilla[xml]
|
21
|
-
assert_equal expected, result
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "with an XML String and a custom root node" do
|
26
|
-
should "return a Hash containing the XML content starting at custom root" do
|
27
|
-
xml = "<root><something><name>Jungle Julia</name></something></root>"
|
28
|
-
expected = { :name => "Jungle Julia" }
|
29
|
-
|
30
|
-
result = ApricotEatsGorilla[xml, "//something"]
|
31
|
-
assert_equal expected, result
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "with a Hash" do
|
36
|
-
should "return an XML String containing the XML content" do
|
37
|
-
hash = { "apricot" => "eats gorilla" }
|
38
|
-
expected = "<apricot>eats gorilla</apricot>"
|
39
|
-
|
40
|
-
result = ApricotEatsGorilla[hash]
|
41
|
-
assert_equal expected, result
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "helper")
|
2
|
-
|
3
|
-
class SoapEnvelopeTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "Calling soap_envelope" do
|
6
|
-
setup { ApricotEatsGorilla.sort_keys = true }
|
7
|
-
|
8
|
-
context "without parameter and block" do
|
9
|
-
should "returns a SOAP envelope without body content" do
|
10
|
-
expected = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">' <<
|
11
|
-
'<env:Body></env:Body></env:Envelope>'
|
12
|
-
|
13
|
-
result = ApricotEatsGorilla.soap_envelope
|
14
|
-
assert_equal expected, result
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "with a Hash containing a custom namespace and a block" do
|
19
|
-
should "returns a SOAP envelope with custom namespace and body content" do
|
20
|
-
expected = '<env:Envelope ' <<
|
21
|
-
'xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' <<
|
22
|
-
'xmlns:wsdl="http://example.com">' <<
|
23
|
-
'<env:Body><id>123</id></env:Body></env:Envelope>'
|
24
|
-
|
25
|
-
result = ApricotEatsGorilla.soap_envelope "wsdl" => "http://example.com" do
|
26
|
-
"<id>123</id>"
|
27
|
-
end
|
28
|
-
assert_equal expected, result
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
@@ -1,111 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "helper")
|
2
|
-
|
3
|
-
class XmlToHashTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "Calling xml_to_hash" do
|
6
|
-
setup do
|
7
|
-
ApricotEatsGorilla.setup do |s|
|
8
|
-
s.sort_keys = true
|
9
|
-
s.disable_tag_names_to_lower_camel_case = false
|
10
|
-
s.disable_hash_keys_to_snake_case = false
|
11
|
-
s.disable_hash_keys_to_symbol = false
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context "with a SOAP response example and a custom root node" do
|
16
|
-
should "return a Hash containing the XML content" do
|
17
|
-
xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
|
18
|
-
<soap:Body>
|
19
|
-
<ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
|
20
|
-
<return>
|
21
|
-
<authValue>
|
22
|
-
<token>secret</token>
|
23
|
-
<client>example</client>
|
24
|
-
</authValue>
|
25
|
-
</return>
|
26
|
-
</ns2:authenticateResponse>
|
27
|
-
</soap:Body>
|
28
|
-
</soap:Envelope>'
|
29
|
-
expected = { :auth_value => { :token => "secret", :client => "example" } }
|
30
|
-
|
31
|
-
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
|
32
|
-
assert_equal expected, result
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context "with XML containing 'true' and 'false' Strings" do
|
37
|
-
should "convert these Strings into actual Boolean objects" do
|
38
|
-
xml = "<root><yes>true</yes><no>false</no><text>something</text></root>"
|
39
|
-
expected = { :yes => true, :no => false, :text => "something" }
|
40
|
-
|
41
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
42
|
-
assert_equal expected, result
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "with XML containing empty element tags" do
|
47
|
-
should "convert empty element tags to nil" do
|
48
|
-
xml = "<contact><name>Jungle Julia</name><email /><phone/></contact>"
|
49
|
-
expected = { :name => "Jungle Julia", :email => nil, :phone => nil }
|
50
|
-
|
51
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
52
|
-
assert_equal expected, result
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
context "with XML containing nodes with attributes" do
|
57
|
-
should "return a Hash without tag attributes" do
|
58
|
-
xml = '<todo><paint subject="chair" color="#000000">black</paint></todo>'
|
59
|
-
expected = { :paint => "black" }
|
60
|
-
|
61
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
62
|
-
assert_equal expected, result
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "with XML containing lowerCamelCase nodes" do
|
67
|
-
should "convert lowerCamelCase nodes to snake_case" do
|
68
|
-
xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
|
69
|
-
expected = { :first_name => "Jungle", :last_name => "Julia" }
|
70
|
-
|
71
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
72
|
-
assert_equal expected, result
|
73
|
-
end
|
74
|
-
|
75
|
-
context "and converting lowerCamelCase Hash keys to snake_case turned off" do
|
76
|
-
setup { ApricotEatsGorilla.disable_hash_keys_to_snake_case = true }
|
77
|
-
|
78
|
-
should "not convert lowerCamelCase Hash keys to snake_case" do
|
79
|
-
xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
|
80
|
-
expected = { :firstName => "Jungle", :lastName => "Julia" }
|
81
|
-
|
82
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
83
|
-
assert_equal expected, result
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context "with some XML" do
|
89
|
-
should "convert Hash keys to Symbols" do
|
90
|
-
xml = "<contact><name>Jungle Julia</name><address/></contact>"
|
91
|
-
expected = { :name => "Jungle Julia", :address => nil }
|
92
|
-
|
93
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
94
|
-
assert_equal expected, result
|
95
|
-
end
|
96
|
-
|
97
|
-
context "and converting Hash keys to Symbols turned off" do
|
98
|
-
setup { ApricotEatsGorilla.disable_hash_keys_to_symbol = true }
|
99
|
-
|
100
|
-
should "not convert Hash keys to Symbols" do
|
101
|
-
xml = "<contact><name>Jungle Julia</name><address/></contact>"
|
102
|
-
expected = { "name" => "Jungle Julia", "address" => nil }
|
103
|
-
|
104
|
-
result = ApricotEatsGorilla.xml_to_hash(xml)
|
105
|
-
assert_equal expected, result
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|