smacks-apricoteatsgorilla 0.4.7 → 0.5.0

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.
@@ -10,7 +10,8 @@ for working with SOAP services.
10
10
 
11
11
  == Dependencies
12
12
 
13
- hpricot 0.6.164 (also available for JRuby)
13
+ hpricot 0.8.241 (the latest JRuby-compatible version)
14
+ Also available from the Github download page of Apricot eats Gorilla.
14
15
 
15
16
  == Translate an XML String into a Ruby Hash
16
17
 
@@ -49,4 +50,4 @@ for working with SOAP services.
49
50
  == Read more
50
51
 
51
52
  For more detailed information, please take a look at the
52
- {GitHub Wiki}[http://wiki.github.com/smacks/apricoteatsgorilla].
53
+ {GitHub Wiki}[http://wiki.github.com/smacks/apricoteatsgorilla].
@@ -0,0 +1,19 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "spec/rake/spectask"
4
+ require "rake/rdoctask"
5
+
6
+ task :default => :spec
7
+
8
+ Spec::Rake::SpecTask.new do |spec|
9
+ spec.spec_files = FileList["spec/**/*_spec.rb"]
10
+ spec.spec_opts << "--color"
11
+ end
12
+
13
+ Rake::RDocTask.new do |rdoc|
14
+ rdoc.title = "Apricot eats Gorilla"
15
+ rdoc.rdoc_dir = "rdoc"
16
+ rdoc.main = "README.rdoc"
17
+ rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb")
18
+ rdoc.options = ["--line-numbers", "--inline-source"]
19
+ end
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.7
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington
@@ -20,10 +20,20 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.6.164
23
+ version: 0.8.241
24
24
  version:
25
- description: Apricot eats Gorilla is a SOAP communication helper.
26
- email:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.8
34
+ version:
35
+ description: SOAP communication helper.
36
+ email: me@d-harrington.com
27
37
  executables: []
28
38
 
29
39
  extensions: []
@@ -32,15 +42,17 @@ extra_rdoc_files:
32
42
  - README.rdoc
33
43
  files:
34
44
  - README.rdoc
35
- - lib/apricoteatsgorilla.rb
36
- - lib/apricoteatsgorilla/apricoteatsgorilla.rb
37
- - lib/apricoteatsgorilla/xml_node.rb
45
+ - Rakefile
38
46
  has_rdoc: true
39
47
  homepage: http://github.com/smacks/apricoteatsgorilla
40
48
  post_install_message:
41
49
  rdoc_options:
50
+ - --title
51
+ - Apricot eats Gorilla
52
+ - --main
53
+ - README.rdoc
54
+ - --line-numbers
42
55
  - --inline-source
43
- - --charset=UTF-8
44
56
  require_paths:
45
57
  - lib
46
58
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -55,18 +67,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
67
  - !ruby/object:Gem::Version
56
68
  version: "0"
57
69
  version:
58
- requirements:
59
- - shoulda for testing
70
+ requirements: []
71
+
60
72
  rubyforge_project:
61
73
  rubygems_version: 1.2.0
62
74
  signing_key:
63
75
  specification_version: 2
64
- summary: Apricot eats Gorilla is a SOAP communication helper.
65
- test_files:
66
- - test/test_apricoteatsgorilla.rb
67
- - test/helper.rb
68
- - test/apricoteatsgorilla/test_hash_to_xml.rb
69
- - test/apricoteatsgorilla/test_xml_to_hash.rb
70
- - test/apricoteatsgorilla/test_shortcut.rb
71
- - test/apricoteatsgorilla/test_soap_envelope.rb
72
- - test/apricoteatsgorilla/test_xml_node.rb
76
+ summary: Translates between SOAP messages (XML) and Ruby Hashes.
77
+ test_files: []
78
+
@@ -1,3 +0,0 @@
1
- %w(apricoteatsgorilla xml_node).each do |file|
2
- require File.join(File.dirname(__FILE__), "apricoteatsgorilla", file)
3
- end
@@ -1,222 +0,0 @@
1
- %w(rubygems hpricot iconv).each do |gem|
2
- require gem
3
- end
4
-
5
- # == ApricotEatsGorilla
6
- #
7
- # Apricot eats Gorilla is a SOAP communication helper. It translates between
8
- # SOAP messages (XML) and Ruby Hashes and comes with some additional helpers
9
- # for working with SOAP services.
10
- class ApricotEatsGorilla
11
- class << self
12
-
13
- # Flag to enable sorting of Hash keys.
14
- attr_accessor :sort_keys
15
-
16
- # Flag to disable conversion of XML tags names to lowerCamelCase.
17
- attr_accessor :disable_tag_names_to_lower_camel_case
18
-
19
- # Flag to disable conversion of Hash keys to snake_case.
20
- attr_accessor :disable_hash_keys_to_snake_case
21
-
22
- # Flag to disable conversion of Hash keys to Symbols.
23
- attr_accessor :disable_hash_keys_to_symbols
24
-
25
- # Hash of namespaces and XML nodes to apply these namespaces to.
26
- attr_accessor :nodes_to_namespace
27
-
28
- # Shortcut method for translating between XML Strings and Ruby Hashes.
29
- # Delegates to +xml_to_hash+ in case +source+ is a String or delegates
30
- # to +hash_to_xml+ in case +source+ is a Hash. Returns nil otherwise.
31
- def [](source, root_node = nil)
32
- case source
33
- when String
34
- xml_to_hash(source, root_node)
35
- when Hash
36
- hash_to_xml(source)
37
- else
38
- nil
39
- end
40
- end
41
-
42
- # Yields this class in case a +block+ was given.
43
- def setup
44
- yield self if block_given?
45
- end
46
-
47
- # Translates a given +xml+ String into a Ruby Hash.
48
- #
49
- # Starts parsing at the XML root node by default. Accepts an optional
50
- # +root_node+ parameter for defining a custom root node to start parsing
51
- # at using an XPath (Hpricot search).
52
- #
53
- # The root node itself won't be included in the Hash.
54
- #
55
- # ==== Examples
56
- #
57
- # xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
58
- # <soap:Body>
59
- # <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
60
- # <return>
61
- # <apricot>
62
- # <eats>Gorilla</eats>
63
- # </apricot>
64
- # </return>
65
- # </ns2:authenticateResponse>
66
- # </soap:Body>
67
- # </soap:Envelope>'
68
- #
69
- # ApricotEatsGorilla.xml_to_hash(xml, "//return")
70
- # # => { :apricot => { :eats => "Gorilla" } }
71
- def xml_to_hash(xml, root_node = nil)
72
- doc = Hpricot.XML remove_whitespace(xml)
73
- root = root_node ? doc.search(root_node) : doc.root
74
-
75
- return nil if root.nil?
76
- return xml_node_to_hash(root) unless root.respond_to? :each
77
-
78
- if root.size == 1
79
- if root.first.children.first.kind_of?(Hpricot::Text)
80
- map_xml_value(root.first.children.to_s)
81
- else
82
- xml_node_to_hash(root.first)
83
- end
84
- else
85
- root.map do |node|
86
- if node.children.first.kind_of?(Hpricot::Text)
87
- map_xml_value(node.children.to_s)
88
- else
89
- xml_node_to_hash(node)
90
- end
91
- end
92
- end
93
- end
94
-
95
- # Translates a given Ruby +hash+ into an XML String.
96
- #
97
- # ==== Examples
98
- #
99
- # hash = { :apricot => { :eats => "Gorilla" } }
100
- #
101
- # ApricotEatsGorilla.hash_to_xml(hash)
102
- # # => "<apricot><eats>Gorilla</eats></apricot>"
103
- def hash_to_xml(hash)
104
- nested_data_to_xml(hash.keys.first, hash.values.first)
105
- end
106
-
107
- # Builds a SOAP request envelope and includes the content from a given
108
- # +block+ into the envelope body. Accepts a Hash of additional +namespaces+
109
- # to set.
110
- #
111
- # ==== Examples
112
- #
113
- # ApricotEatsGorilla.soap_envelope do
114
- # "<apricot><eats>Gorilla</eats></apricot>"
115
- # end
116
- #
117
- # # => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
118
- # # => <env:Body>
119
- # # => <apricot><eats>Gorilla</eats></apricot>
120
- # # => </env:Body>
121
- # # => </env:Envelope>'
122
- def soap_envelope(namespaces = {})
123
- namespaces[:env] = "http://schemas.xmlsoap.org/soap/envelope/"
124
-
125
- xml_node("env:Envelope", namespaces) do
126
- xml_node("env:Body") { yield if block_given? }
127
- end
128
- end
129
-
130
- private
131
-
132
- # Iterates through an expected Hpricot +element+ and returns a Ruby Hash
133
- # equal to the XML content of the given element.
134
- def xml_node_to_hash(element)
135
- hash = {}
136
- element.each_child do |child|
137
- key = XMLNode.new(child.name)
138
- key.strip_namespace!
139
- key.to_snake_case! unless disable_hash_keys_to_snake_case
140
- key = disable_hash_keys_to_symbols ? key.to_s : key.to_sym
141
-
142
- # hpricot 0.6.1 returns an empty array, while 0.8 returns nil
143
- if child.children.nil? || child.children.empty?
144
- value = nil
145
- elsif child.children.size == 1 && child.children.first.text?
146
- value = map_xml_value(child.children.first.to_html)
147
- else
148
- value = xml_node_to_hash(child)
149
- end
150
-
151
- case hash[key]
152
- when Array
153
- hash[key] << value
154
- when nil
155
- hash[key] = value
156
- else
157
- hash[key] = [hash[key].dup, value]
158
- end
159
- end
160
- hash
161
- end
162
-
163
- # Expects a Hash +key+ and a Hash +value+. Iterates through the given Hash
164
- # +value+ and returns an XML String of the given Hash structure.
165
- def nested_data_to_xml(key, value)
166
- case value
167
- when Array
168
- value.map { |subitem| nested_data_to_xml(key, subitem) }.join
169
- when Hash
170
- xml_node(key) do
171
- sort_hash_keys(value).map do |subkey, subvalue|
172
- case subvalue
173
- when Array
174
- subvalue.map { |subitem| nested_data_to_xml(subkey, subitem) }.join
175
- when Hash
176
- nested_data_to_xml(subkey, subvalue)
177
- else
178
- xml_node(subkey) { subvalue.to_s } if subvalue.respond_to?(:to_s)
179
- end
180
- end.join
181
- end
182
- else
183
- xml_node(key) { value.to_s } if value.respond_to?(:to_s)
184
- end
185
- end
186
-
187
- # Returns an XML tag with a given +name+. Accepts a +block+ for tag content.
188
- # Defaults to returning an empty element tag in case no block was given.
189
- # Also accepts a Hash of +attributes+ to be added to the XML tag.
190
- def xml_node(name, attributes = {})
191
- node = XMLNode.new(name.to_s)
192
-
193
- node.to_lower_camel_case! unless disable_tag_names_to_lower_camel_case
194
- node.namespace_from_hash!(nodes_to_namespace)
195
- node.attributes = sort_hash_keys(attributes)
196
- node.body = yield if block_given?
197
-
198
- node.to_tag
199
- end
200
-
201
- # Removes whitespace between tags from a given +xml+ String.
202
- def remove_whitespace(xml)
203
- xml.gsub(/(>)\s*(<)/, '\1\2')
204
- end
205
-
206
- # Maps an XML value to a more natural Ruby object. Converts String values
207
- # of "true" and "false" to TrueClass and FalseClass. Converts other String
208
- # values from "iso-8859-1" to "utf-8".
209
- def map_xml_value(value)
210
- return true if value == "true"
211
- return false if value == "false"
212
- Iconv.new("iso-8859-1", "utf-8").iconv(value)
213
- end
214
-
215
- # Returns a sorted version of a given +hash+ if +sort_keys+ is enabled.
216
- def sort_hash_keys(hash)
217
- return hash unless sort_keys
218
- hash.keys.sort_by { |key| key.to_s }.map { |key| [ key, hash[key] ] }
219
- end
220
-
221
- end
222
- end
@@ -1,75 +0,0 @@
1
- # == XMLNode
2
- #
3
- # Representation of an XML node.
4
- class XMLNode < String
5
-
6
- # Hash of attributes.
7
- attr_writer :attributes
8
-
9
- # Body content.
10
- attr_writer :body
11
-
12
- # Strips the namespace from this node.
13
- def strip_namespace!
14
- sub!(/.+:(.+)/, '\1')
15
- end
16
-
17
- # Converts the name of this node to snake_case.
18
- def to_snake_case!
19
- self.gsub!(/[A-Z]/, '_\0')
20
- self.gsub!(/^_/, '')
21
- self.downcase!
22
- end
23
-
24
- # Converts the name of this node to lowerCamelCase.
25
- def to_lower_camel_case!
26
- self.gsub!(/_(.)/) { $1.upcase }
27
- end
28
-
29
- # Checks if the name of this node is included in a given Hash of +namespaces+
30
- # and sets the namespace for this node in case it was found in the Hash.
31
- def namespace_from_hash!(namespaces)
32
- return if namespaces.nil? || namespaces.empty?
33
-
34
- namespaces.each do |namespace, nodes|
35
- @namespace = namespace if self_included?(nodes)
36
- end
37
- end
38
-
39
- # Returns this node as a complete XML tag including a +namespace+, +attributes+
40
- # and a +body+ in case these values were supplied.
41
- def to_tag
42
- return "<#{namespace}#{self}#{attributes} />" unless @body
43
- "<#{namespace}#{self}#{attributes}>#{body}</#{namespace}#{self}>"
44
- end
45
-
46
- private
47
-
48
- # Returns +true+ if self as a String or a Symbol is included in a given
49
- # +array+. Returns +false+ otherwise.
50
- def self_included?(array)
51
- array.include?(self.to_s) || array.include?(self.to_sym)
52
- end
53
-
54
- # Returns the namespace of this node. Defaults to an empty String in case
55
- # no namespace was defined.
56
- def namespace
57
- return "" if @namespace.nil?
58
- "#{@namespace}:"
59
- end
60
-
61
- # Returns the attributes of this node. Defaults to an empty String in case
62
- # no attributes were defined.
63
- def attributes
64
- return "" if @attributes.nil?
65
- @attributes.map { |key, value| %Q( xmlns:#{key}="#{value}") }
66
- end
67
-
68
- # Returns the body of this node. Defaults to an empty String in case no body
69
- # was defined.
70
- def body
71
- return "" if @body.nil?
72
- @body
73
- end
74
-
75
- end
@@ -1,121 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "helper")
2
-
3
- class TestHashToXml < 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_symbols = false
12
- s.nodes_to_namespace = nil
13
- end
14
- end
15
-
16
- context "with a Hash containing only a single key-value-pair" do
17
- should "return an XML String containing one node and a value" do
18
- hash = { "apricot" => "eats Gorilla" }
19
- expected = "<apricot>eats Gorilla</apricot>"
20
-
21
- result = ApricotEatsGorilla.hash_to_xml(hash)
22
- assert_equal expected, result
23
- end
24
- end
25
-
26
- context "with a Hash containing another Hash" do
27
- should "return an XML String representing the given structure" do
28
- hash = { "apricot" => { "eats" => "gorilla", "drinks" => "beer" } }
29
- expected = "<apricot><drinks>beer</drinks><eats>gorilla</eats></apricot>"
30
-
31
- result = ApricotEatsGorilla.hash_to_xml(hash)
32
- assert_equal expected, result
33
- end
34
- end
35
-
36
- context "with a Hash containing a Hash containing an Array" do
37
- should "return an XML String representing the given structure" do
38
- hash = { "apricot" => { "eats" => [ "gorilla", "snake" ] } }
39
- expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
40
-
41
- result = ApricotEatsGorilla.hash_to_xml(hash)
42
- assert_equal expected, result
43
- end
44
- end
45
-
46
- context "with a Hash containing a Hash containing an Array containing a Hash" do
47
- should "return an XML String representing the given structure" do
48
- hash = { "apricot" =>
49
- { "eats" => [ { "lotsOf" => "gorillas" }, { "justSome" => "snakes" } ]
50
- } }
51
- expected = "<apricot><eats><lotsOf>gorillas</lotsOf></eats>" <<
52
- "<eats><justSome>snakes</justSome></eats></apricot>"
53
-
54
- result = ApricotEatsGorilla.hash_to_xml(hash)
55
- assert_equal expected, result
56
- end
57
- end
58
-
59
- context "with a Hash containing Symbols" do
60
- should "returns an XML String with Symbols converted into Strings" do
61
- hash = { :apricot => { :eats => [ :gorilla, "snake" ] } }
62
- expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
63
-
64
- result = ApricotEatsGorilla.hash_to_xml(hash)
65
- assert_equal expected, result
66
- end
67
- end
68
-
69
- context "with a Hash containing snake_case keys" do
70
- should "convert snake_case Hash keys to lowerCamelCase" do
71
- hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
72
- expected = "<apricot><eats><lotsOf>gorillas</lotsOf></eats></apricot>"
73
-
74
- result = ApricotEatsGorilla.hash_to_xml(hash)
75
- assert_equal expected, result
76
- end
77
-
78
- context "and converting snake_case tag names to lowerCamelCase turned off" do
79
- setup { ApricotEatsGorilla.disable_tag_names_to_lower_camel_case = true }
80
-
81
- should "not convert snake_case tag names to lowerCamelCase" do
82
- hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
83
- expected = "<apricot><eats><lots_of>gorillas</lots_of></eats></apricot>"
84
-
85
- result = ApricotEatsGorilla.hash_to_xml(hash)
86
- assert_equal expected, result
87
- end
88
- end
89
- end
90
-
91
- context "with a Hash different types of values" do
92
- should "convert values responding to 'to_s' to Strings" do
93
- date_time = DateTime.now
94
- hash = { :apricot => {
95
- :at => date_time,
96
- :with => 100.01,
97
- :when => nil,
98
- :what => :gorillas
99
- } }
100
- expected = "<apricot><at>#{date_time}</at><what>gorillas</what>" <<
101
- "<when></when><with>100.01</with></apricot>"
102
-
103
- result = ApricotEatsGorilla.hash_to_xml(hash)
104
- assert_equal expected, result
105
- end
106
- end
107
-
108
- context "with some Hash and nodes to namespace" do
109
- setup { ApricotEatsGorilla.nodes_to_namespace = { :wsdl => [ :apricot ] } }
110
-
111
- should "apply the defined namespaces" do
112
- hash = { :apricot => { :eats => "Gorilla" } }
113
- expected = "<wsdl:apricot><eats>Gorilla</eats></wsdl:apricot>"
114
-
115
- result = ApricotEatsGorilla.hash_to_xml(hash)
116
- assert_equal expected, result
117
- end
118
- end
119
- end
120
-
121
- end
@@ -1,47 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "helper")
2
-
3
- class TestShortcutMethod < 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_symbols = false
12
- s.nodes_to_namespace = nil
13
- end
14
- end
15
-
16
- context "with an XML String" do
17
- should "return a Hash containing the XML content" do
18
- xml = "<root><name>Jungle Julia</name></root>"
19
- expected = { :name => "Jungle Julia" }
20
-
21
- result = ApricotEatsGorilla[xml]
22
- assert_equal expected, result
23
- end
24
- end
25
-
26
- context "with an XML String and a custom root node" do
27
- should "return a Hash containing the XML content starting at custom root" do
28
- xml = "<root><something><name>Jungle Julia</name></something></root>"
29
- expected = { :name => "Jungle Julia" }
30
-
31
- result = ApricotEatsGorilla[xml, "//something"]
32
- assert_equal expected, result
33
- end
34
- end
35
-
36
- context "with a Hash" do
37
- should "return an XML String containing the XML content" do
38
- hash = { "apricot" => "eats gorilla" }
39
- expected = "<apricot>eats gorilla</apricot>"
40
-
41
- result = ApricotEatsGorilla[hash]
42
- assert_equal expected, result
43
- end
44
- end
45
- end
46
-
47
- end
@@ -1,33 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "helper")
2
-
3
- class TestSoapEnvelope < 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 with an empty element body tag" do
10
- expected = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">' <<
11
- '<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 a 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,87 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "helper")
2
-
3
- class TestXMLNode < Test::Unit::TestCase
4
-
5
- context "strip_namespace!" do
6
- should "strip the namespace from the XMLNode" do
7
- node = XMLNode.new("wsdl:apricot")
8
- node.strip_namespace!
9
-
10
- assert_equal "apricot", node.to_s
11
- end
12
- end
13
-
14
- context "to_snake_case!" do
15
- should "convert the XMLNode from CamelCase to snake_case" do
16
- node = XMLNode.new("ApricotEatsGorilla")
17
- node.to_snake_case!
18
-
19
- assert_equal "apricot_eats_gorilla", node.to_s
20
- end
21
-
22
- should "convert the XMLNode from lowerCamelCase to snake_case" do
23
- node = XMLNode.new("apricotEatsGorilla")
24
- node.to_snake_case!
25
-
26
- assert_equal "apricot_eats_gorilla", node.to_s
27
- end
28
- end
29
-
30
- context "namespace_from_hash!" do
31
- context "with a Hash containing the name of the XMLNode" do
32
- should "set the namespace of the XMLNode" do
33
- node = XMLNode.new("apricot")
34
- node.namespace_from_hash!(:wsdl => [ :apricot ])
35
-
36
- assert_equal "apricot", node.to_s
37
- assert_equal "<wsdl:apricot />", node.to_tag
38
- end
39
- end
40
-
41
- context "with a Hash that does not contain the name of the XMLNode" do
42
- should "not set the namespace of the XMLNode" do
43
- node = XMLNode.new("apricot")
44
- node.namespace_from_hash!(:wsdl => [ :some_key ])
45
-
46
- assert_equal "<apricot />", node.to_tag
47
- end
48
- end
49
- end
50
-
51
- context "to_tag" do
52
- context "with a simple XMLNode" do
53
- should "return an empty element tag" do
54
- node = XMLNode.new("apricot")
55
- assert_equal "<apricot />", node.to_tag
56
- end
57
- end
58
-
59
- context "with an XMLNode containing a namespace" do
60
- should "return a namespaced empty element tag" do
61
- node = XMLNode.new("apricot")
62
- node.namespace_from_hash!(:wsdl => [ :apricot ])
63
-
64
- assert_equal "<wsdl:apricot />", node.to_tag
65
- end
66
- end
67
-
68
- context "with an XMLNode containing an attribute" do
69
- should "return an empty element tag with an xmlns attribute" do
70
- node = XMLNode.new("apricot")
71
- node.attributes = { :wsdl => "http://example.com" }
72
-
73
- assert_equal '<apricot xmlns:wsdl="http://example.com" />', node.to_tag
74
- end
75
- end
76
-
77
- context "with an XMLNode containing a body" do
78
- should "return an element with a body" do
79
- node = XMLNode.new("apricot")
80
- node.body = "eats Gorilla"
81
-
82
- assert_equal '<apricot>eats Gorilla</apricot>', node.to_tag
83
- end
84
- end
85
- end
86
-
87
- end
@@ -1,162 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "..", "helper")
2
-
3
- class TestXmlToHash < 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_symbols = false
12
- s.nodes_to_namespace = nil
13
- end
14
- end
15
-
16
- context "with a SOAP response example and a custom root node" do
17
- should "return a Hash containing the XML content" do
18
- xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
19
- <soap:Body>
20
- <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
21
- <return>
22
- <authValue>
23
- <token>secret</token>
24
- <client>example</client>
25
- </authValue>
26
- </return>
27
- </ns2:authenticateResponse>
28
- </soap:Body>
29
- </soap:Envelope>'
30
- expected = { :auth_value => { :token => "secret", :client => "example" } }
31
-
32
- result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
33
- assert_equal expected, result
34
- end
35
- end
36
-
37
- context "with XML containing namespaced nodes" do
38
- should "remove namespaces from nodes" do
39
- xml = "<return><wsdl:apricot><eats>Gorilla</eats></wsdl:apricot></return>"
40
- expected = { :apricot => { :eats => "Gorilla" } }
41
-
42
- result = ApricotEatsGorilla.xml_to_hash(xml)
43
- assert_equal expected, result
44
- end
45
- end
46
-
47
- context "with XML containing 'true' and 'false' Strings" do
48
- should "convert these Strings into actual Boolean objects" do
49
- xml = "<root><yes>true</yes><no>false</no><text>something</text></root>"
50
- expected = { :yes => true, :no => false, :text => "something" }
51
-
52
- result = ApricotEatsGorilla.xml_to_hash(xml)
53
- assert_equal expected, result
54
- end
55
- end
56
-
57
- context "with XML containing empty element tags" do
58
- should "convert empty element tags to nil" do
59
- xml = "<contact><name>Jungle Julia</name><email /><phone/></contact>"
60
- expected = { :name => "Jungle Julia", :email => nil, :phone => nil }
61
-
62
- result = ApricotEatsGorilla.xml_to_hash(xml)
63
- assert_equal expected, result
64
- end
65
- end
66
-
67
- context "with XML containing nodes with attributes" do
68
- should "return a Hash without tag attributes" do
69
- xml = '<root><user id="123">example</user></root>'
70
- expected = { :user => "example" }
71
-
72
- result = ApricotEatsGorilla.xml_to_hash(xml)
73
- assert_equal expected, result
74
- end
75
- end
76
-
77
- context "with XML containing multiple custom root nodes" do
78
- should "return an Array containing the values of each root node" do
79
- xml = '<root><return><id>123</id></return><return><id>456</id></return></root>'
80
- expected = [{ :id => "123" }, { :id => "456" }]
81
-
82
- result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
83
- assert_equal expected, result
84
- end
85
- end
86
-
87
- context "with XML containing no subnodes at custom root node" do
88
- should "return the plain content of the custom root node" do
89
- xml = '<root><return>123</return></root>'
90
- expected = "123"
91
-
92
- result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
93
- assert_equal expected, result
94
- end
95
- end
96
-
97
- context "with XML containing no subnodes in multiple custom root nodes" do
98
- should "return an Array containing the values of each root node" do
99
- xml = '<root><return>123</return><return>456</return></root>'
100
- expected = ["123", "456"]
101
-
102
- result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
103
- assert_equal expected, result
104
- end
105
- end
106
-
107
- context "with XML containing several subnodes with the same name and text content" do
108
- should "return a Hash containing one key and an array of Strings" do
109
- xml = '<root><return><items>123</items><items>456</items></return></root>'
110
- expected = { :items => ["123", "456"] }
111
-
112
- result = ApricotEatsGorilla[xml, "//return"]
113
- assert_equal expected, result
114
- end
115
- end
116
-
117
- context "with XML containing lowerCamelCase nodes" do
118
- should "convert lowerCamelCase nodes to snake_case" do
119
- xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
120
- expected = { :first_name => "Jungle", :last_name => "Julia" }
121
-
122
- result = ApricotEatsGorilla.xml_to_hash(xml)
123
- assert_equal expected, result
124
- end
125
-
126
- context "and converting lowerCamelCase Hash keys to snake_case turned off" do
127
- setup { ApricotEatsGorilla.disable_hash_keys_to_snake_case = true }
128
-
129
- should "not convert lowerCamelCase Hash keys to snake_case" do
130
- xml = "<contact><firstName>Jungle</firstName><lastName>Julia</lastName></contact>"
131
- expected = { :firstName => "Jungle", :lastName => "Julia" }
132
-
133
- result = ApricotEatsGorilla.xml_to_hash(xml)
134
- assert_equal expected, result
135
- end
136
- end
137
- end
138
-
139
- context "with some XML" do
140
- should "convert Hash keys to Symbols" do
141
- xml = "<contact><name>Jungle Julia</name><address/></contact>"
142
- expected = { :name => "Jungle Julia", :address => nil }
143
-
144
- result = ApricotEatsGorilla.xml_to_hash(xml)
145
- assert_equal expected, result
146
- end
147
-
148
- context "and converting Hash keys to Symbols turned off" do
149
- setup { ApricotEatsGorilla.disable_hash_keys_to_symbols = true }
150
-
151
- should "not convert Hash keys to Symbols" do
152
- xml = "<contact><name>Jungle Julia</name><address/></contact>"
153
- expected = { "name" => "Jungle Julia", "address" => nil }
154
-
155
- result = ApricotEatsGorilla.xml_to_hash(xml)
156
- assert_equal expected, result
157
- end
158
- end
159
- end
160
- end
161
-
162
- end
@@ -1,5 +0,0 @@
1
- %w(rubygems test/unit shoulda).each do |gem|
2
- require gem
3
- end
4
-
5
- require File.join(File.dirname(__FILE__), "..", "lib", "apricoteatsgorilla")
@@ -1,3 +0,0 @@
1
- %w(shortcut xml_to_hash hash_to_xml soap_envelope xml_node).each do |file|
2
- require File.join(File.dirname(__FILE__), "apricoteatsgorilla", "test_#{file}")
3
- end