smacks-savon 0.0.2 → 0.0.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.
@@ -4,8 +4,8 @@ require 'apricoteatsgorilla'
4
4
 
5
5
  module Savon
6
6
 
7
- # Savon::Response represents the SOAP response and offers different methods
8
- # to handle the response.
7
+ # Savon::Response represents the SOAP response and includes methods for
8
+ # working with the raw XML, a Hash or a Savon::Mash object.
9
9
  class Response
10
10
 
11
11
  # Initializer to set the SOAP response.
@@ -14,16 +14,49 @@ module Savon
14
14
  end
15
15
 
16
16
  # Returns the SOAP response message as a Hash. Call with XPath expession
17
- # (Hpricot search) as parameter to define a custom root node. The root node
18
- # itself will not be included in the Hash.
17
+ # as parameter to define a custom root node. The root node itself will not
18
+ # be included in the Hash.
19
19
  def to_hash(root_node = "//return")
20
20
  ApricotEatsGorilla(@response.body, root_node)
21
21
  end
22
22
 
23
+ # Returns the SOAP response message as a Savon::Mash object. Call with
24
+ # XPath expession as parameter to define a custom root node. The root node
25
+ # itself will not be included in the Mash object.
26
+ def to_mash(root_node = "//return")
27
+ hash = to_hash(root_node)
28
+ Savon::Mash.new(hash)
29
+ end
30
+
23
31
  # Returns the raw XML response.
24
32
  def to_s
25
33
  @response.body
26
34
  end
27
35
 
28
36
  end
37
+
38
+ # Savon::Mash converts a given Hash into a Mash object.
39
+ class Mash
40
+
41
+ def initialize(hash)
42
+ hash.each do |key,value|
43
+ value = Savon::Mash.new value if value.is_a? Hash
44
+
45
+ if value.is_a? Array
46
+ value = value.map do |item|
47
+ if item.is_a?(Hash) then Savon::Mash.new(item) else item end
48
+ end
49
+ end
50
+
51
+ # Create and initialize an instance variable for this key/value pair
52
+ self.instance_variable_set("@#{key}", value)
53
+ # Create the getter that returns the instance variable
54
+ self.class.send(:define_method, key, proc{self.instance_variable_get("@#{key}")})
55
+ # Create the setter that sets the instance variable
56
+ self.class.send(:define_method, "#{key}=", proc{|value| self.instance_variable_set("@#{key}", value)})
57
+ end
58
+ end
59
+
60
+ end
61
+
29
62
  end
@@ -20,6 +20,14 @@ class SavonResponseTest < Test::Unit::TestCase
20
20
  assert_equal ApricotEatsGorilla(some_soap_response, "//return"), @response.to_hash
21
21
  end
22
22
 
23
+ should "return a Mash object on to_mash" do
24
+ assert_kind_of Savon::Mash, @response.to_mash
25
+ end
26
+
27
+ should "return a Mash object equal to the response on to_mash" do
28
+ assert_equal "secret", @response.to_mash.token
29
+ end
30
+
23
31
  should "return the raw XML response on to_s" do
24
32
  assert_equal some_soap_response, @response.to_s
25
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smacks-savon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.6
33
+ version: 0.2.7
34
34
  version:
35
35
  description: Savon is a lightweight SOAP client.
36
36
  email: