smacks-apricoteatsgorilla 0.3.3 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,7 +12,7 @@ uses Hpricot instead of REXML and doesn't follow the BadgerFish convention.
12
12
 
13
13
  == Dependencies
14
14
 
15
- Hpricot 0.6.164 (also available for JRuby)
15
+ hpricot 0.6.164 (also available for JRuby)
16
16
 
17
17
  == How to use
18
18
 
@@ -7,20 +7,33 @@ require "hpricot"
7
7
  # be used to build a SOAP request envelope. It is based on CobraVsMongoose but
8
8
  # uses Hpricot instead of REXML and doesn't follow the BadgerFish convention.
9
9
  #
10
- # === Translating an XML String into a Ruby Hash
10
+ # === xml_to_hash(xml, root_node = nil)
11
11
  #
12
- # xml = "<apricot><eats>Gorilla</eats></apricot>"
13
- # ApricotEatsGorilla[xml]
14
- # # => { :eats => "Gorilla" }
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>'
15
24
  #
16
- # === Translating a Ruby Hash into an XML String
25
+ # ApricotEatsGorilla[xml, "//return"]
26
+ # # => { :auth_value => { :token => "secret", :client => "example" } }
27
+ #
28
+ # === hash_to_xml(hash)
17
29
  #
18
30
  # hash = { :apricot => { :eats => "Gorilla" } }
31
+ #
19
32
  # ApricotEatsGorilla[hash]
20
33
  # # => "<apricot><eats>Gorilla</eats></apricot>"
21
34
  #
22
- # === Creating a SOAP request envelope
23
- #
35
+ # === soap_envelope(namespaces = {})
36
+ #
24
37
  # ApricotEatsGorilla.soap_envelope { "<authenticate>me</authenticate>" }
25
38
  #
26
39
  # # => '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
@@ -76,6 +89,8 @@ class ApricotEatsGorilla
76
89
  xml = clean_xml(xml)
77
90
  doc = Hpricot.XML(xml)
78
91
  root = root_node ? doc.at(root_node) : doc.root
92
+
93
+ return nil if root.nil?
79
94
  xml_node_to_hash(root)
80
95
  end
81
96
 
@@ -164,7 +179,7 @@ class ApricotEatsGorilla
164
179
  when nil
165
180
  this_node[key] = value
166
181
  else
167
- this_node[key] = [current, value]
182
+ this_node[key] = [current.dup, value]
168
183
  end
169
184
  end
170
185
  this_node
@@ -0,0 +1,52 @@
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 { ApricotEatsGorilla.sort_keys = true }
7
+
8
+ context "with a Hash consisting of a single key-value-pair" do
9
+ should "returns an XML String containing one node and a value" do
10
+ hash = { "apricot" => "eats gorilla" }
11
+ expected = "<apricot>eats gorilla</apricot>"
12
+
13
+ result = ApricotEatsGorilla.hash_to_xml(hash)
14
+ assert_equal expected, result
15
+ end
16
+ end
17
+
18
+ context "with a Hash containing another Hash" do
19
+ should "returns an XML String representing the given structure" do
20
+ hash = { "apricot" => { "eats" => "gorilla", "drinks" => "beer" } }
21
+ expected = "<apricot><drinks>beer</drinks><eats>gorilla</eats></apricot>"
22
+
23
+ result = ApricotEatsGorilla.hash_to_xml(hash)
24
+ assert_equal expected, result
25
+ end
26
+ end
27
+
28
+ context "with a Hash containing a Hash containing an Array" do
29
+ should "returns an XML String representing the given structure" do
30
+ hash = { "apricot" => { "eats" => [ "gorilla", "snake" ] } }
31
+ expected = "<apricot><eats>gorilla</eats><eats>snake</eats></apricot>"
32
+
33
+ result = ApricotEatsGorilla.hash_to_xml(hash)
34
+ assert_equal expected, result
35
+ end
36
+ end
37
+
38
+ context "with a Hash containing a Hash containing an Array containing a Hash" do
39
+ should "returns an XML String representing the given structure" do
40
+ hash = { "apricot" =>
41
+ { "eats" => [ { "lotsOf" => "gorillas" }, { "justSome" => "snakes" } ]
42
+ } }
43
+ expected = "<apricot><eats><lotsOf>gorillas</lotsOf></eats>" <<
44
+ "<eats><justSome>snakes</justSome></eats></apricot>"
45
+
46
+ result = ApricotEatsGorilla.hash_to_xml(hash)
47
+ assert_equal expected, result
48
+ end
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,39 @@
1
+ require File.join(File.dirname(__FILE__), "..", "helper")
2
+
3
+ class ShortcutMethodTest < Test::Unit::TestCase
4
+
5
+ context "Calling []" do
6
+ setup { ApricotEatsGorilla.sort_keys = true }
7
+
8
+ context "with an XML String" do
9
+ should "return a Hash containing the XML content" do
10
+ xml = "<root><name>Jungle Julia</name></root>"
11
+ expected = { :name => "Jungle Julia" }
12
+
13
+ result = ApricotEatsGorilla[xml]
14
+ assert_equal expected, result
15
+ end
16
+ end
17
+
18
+ context "with an XML String and a custom root node" do
19
+ should "return a Hash containing the XML content starting at custom root" do
20
+ xml = "<root><something><name>Jungle Julia</name></something></root>"
21
+ expected = { :name => "Jungle Julia" }
22
+
23
+ result = ApricotEatsGorilla[xml, "//something"]
24
+ assert_equal expected, result
25
+ end
26
+ end
27
+
28
+ context "with a Hash" do
29
+ should "return an XML String containing the XML content" do
30
+ hash = { "apricot" => "eats gorilla" }
31
+ expected = "<apricot>eats gorilla</apricot>"
32
+
33
+ result = ApricotEatsGorilla[hash]
34
+ assert_equal expected, result
35
+ end
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,33 @@
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
@@ -0,0 +1,70 @@
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 { ApricotEatsGorilla.sort_keys = true }
7
+
8
+ context "with a SOAP response example and a custom root node" do
9
+ should "return a Hash containing the XML content" do
10
+ xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
11
+ <soap:Body>
12
+ <ns2:authenticateResponse xmlns:ns2="http://v1_0.ws.example.com/">
13
+ <return>
14
+ <authValue>
15
+ <token>secret</token>
16
+ <client>example</client>
17
+ </authValue>
18
+ </return>
19
+ </ns2:authenticateResponse>
20
+ </soap:Body>
21
+ </soap:Envelope>'
22
+ expected = { :auth_value => { :token => "secret", :client => "example" } }
23
+
24
+ result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
25
+ assert_equal expected, result
26
+ end
27
+ end
28
+
29
+ context "with XML containing 'true' and 'false' Strings" do
30
+ should "convert these Strings into actual Boolean objects" do
31
+ xml = "<root><yes>true</yes><no>false</no><text>something</text></root>"
32
+ expected = { :yes => true, :no => false, :text => "something" }
33
+
34
+ result = ApricotEatsGorilla.xml_to_hash(xml)
35
+ assert_equal expected, result
36
+ end
37
+ end
38
+
39
+ context "with XML containing empty element tags" do
40
+ should "convert empty element tags to nil" do
41
+ xml = "<contact><name>Jungle Julia</name><email /><phone/></contact>"
42
+ expected = { :name => "Jungle Julia", :email => nil, :phone => nil }
43
+
44
+ result = ApricotEatsGorilla.xml_to_hash(xml)
45
+ assert_equal expected, result
46
+ end
47
+ end
48
+
49
+ context "with XML containing nodes with attributes" do
50
+ should "return a Hash without tag attributes" do
51
+ xml = '<todo><paint subject="chair" color="#000000">black</paint></todo>'
52
+ expected = { :paint => "black" }
53
+
54
+ result = ApricotEatsGorilla.xml_to_hash(xml)
55
+ assert_equal expected, result
56
+ end
57
+ end
58
+
59
+ context "with XML containing nodes with the same name" do
60
+ should "group these nodes into an Array" do
61
+ xml = "<root><items>first</items><items>second</items></root>"
62
+ expected = { :items => [ "first", "second" ] }
63
+
64
+ result = ApricotEatsGorilla.xml_to_hash(xml)
65
+ assert_equal expected, result
66
+ end
67
+ end
68
+ end
69
+
70
+ end
File without changes
data/test/helper.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "shoulda"
4
+
5
+ require File.join(File.dirname(__FILE__), "..", "lib", "apricoteatsgorilla")
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.3
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington
@@ -61,4 +61,9 @@ signing_key:
61
61
  specification_version: 2
62
62
  summary: Apricot eats Gorilla is a SOAP communication helper.
63
63
  test_files:
64
- - tests/apricoteatsgorilla_test.rb
64
+ - test/apricoteatsgorilla_test.rb
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