savon 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -10,20 +10,21 @@ module Savon
10
10
  # Default behavior for processing the SOAP response. Expects an instance
11
11
  # of Net::HTTPResponse and returns the SOAP response body as a Hash.
12
12
  @response_process = lambda do |response|
13
- response_hash = Crack::XML.parse response.body
14
- error_handling.call response, response_hash
13
+ body = Crack::XML.parse response.body
14
+ body = body.find_regexp [/.+:Envelope/, /.+:Body/]
15
+ error_handling.call response, body
15
16
 
16
- response_hash = response_hash["soap:Envelope"]["soap:Body"]
17
- response_hash = response_hash[response_hash.keys.first]
18
- (response_hash["return"] || response_hash).map_soap_response
17
+ body = body.find_regexp /.+/
18
+ (body["return"] || body).map_soap_response
19
19
  end
20
20
 
21
21
  # Default error handling. Expects an instance of Net::HTTPResponse and
22
22
  # a SOAP response body Hash. Raises a Savon::SOAPFault in case of a SOAP
23
23
  # fault or a Savon::HTTPError in case of an HTTP error.
24
- @error_handling = lambda do |response, response_hash|
25
- soap_fault = response_hash.to_soap_fault_message
26
- raise Savon::SOAPFault, soap_fault if soap_fault
24
+ @error_handling = lambda do |response, body|
25
+ soap_fault = body.find_regexp [/.+:Fault/]
26
+ soap_fault_message = soap_fault.to_soap_fault_message
27
+ raise Savon::SOAPFault, soap_fault_message if soap_fault_message
27
28
 
28
29
  http_error = "#{response.message} (#{response.code})"
29
30
  http_error += ": #{response.body}" unless response.body.empty?
@@ -1,5 +1,20 @@
1
1
  class Hash
2
2
 
3
+ # Expects an Array of Regexp Objects of which every Regexp recursively
4
+ # matches a key to be accessed. Returns the value of the last Regexp filter
5
+ # found in the Hash or an empty Hash in case the path of Regexp filters
6
+ # did not match the Hash structure.
7
+ def find_regexp(regexp)
8
+ regexp = [regexp] unless regexp.kind_of? Array
9
+ result = dup
10
+
11
+ regexp.each do |pattern|
12
+ result_key = result.keys.find { |key| key.to_s.match pattern }
13
+ result = result[result_key] ? result[result_key] : {}
14
+ end
15
+ result
16
+ end
17
+
3
18
  # Returns the Hash translated into SOAP request compatible XML.
4
19
  #
5
20
  # === Example
@@ -15,13 +30,10 @@ class Hash
15
30
  # Tries to generate a SOAP fault message from the Hash. Returns nil in
16
31
  # case no SOAP fault could be found or generated.
17
32
  def to_soap_fault_message
18
- soap_fault = self["soap:Envelope"]["soap:Body"]["soap:Fault"] rescue {}
19
- return nil unless soap_fault
20
-
21
- if soap_fault.keys.include? "faultcode"
22
- "(#{soap_fault['faultcode']}) #{soap_fault['faultstring']}"
23
- elsif soap_fault.keys.include? "code"
24
- "(#{soap_fault['code']['value']}) #{soap_fault['reason']['text']}"
33
+ if keys.include? "faultcode"
34
+ "(#{self['faultcode']}) #{self['faultstring']}"
35
+ elsif keys.include? "code"
36
+ "(#{self['code']['value']}) #{self['reason']['text']}"
25
37
  else
26
38
  nil
27
39
  end
@@ -2,6 +2,29 @@ require "spec_helper"
2
2
 
3
3
  describe Hash do
4
4
 
5
+ describe "find_regexp" do
6
+ before do
7
+ @soap_fault_hash = { "soap:Envelope" => { "soap:Body" => { "soap:Fault" => {
8
+ "faultcode" => "soap:Server", "faultstring" => "Fault occurred while processing."
9
+ } } } }
10
+ end
11
+
12
+ it "returns an empty Hash in case it did not find the specified value" do
13
+ result = @soap_fault_hash.find_regexp "soap:Fault"
14
+
15
+ result.should be_a Hash
16
+ result.should be_empty
17
+ end
18
+
19
+ it "returns the value of the last Regexp filter found in the Hash" do
20
+ @soap_fault_hash.find_regexp([".+:Envelope", ".+:Body"]).
21
+ should == @soap_fault_hash["soap:Envelope"]["soap:Body"]
22
+
23
+ @soap_fault_hash.find_regexp([/.+:Envelope/, /.+:Body/, /.+Fault/]).
24
+ should == @soap_fault_hash["soap:Envelope"]["soap:Body"]["soap:Fault"]
25
+ end
26
+ end
27
+
5
28
  describe "to_soap_xml" do
6
29
  describe "returns SOAP request compatible XML" do
7
30
  it "for a simple Hash" do
@@ -74,20 +97,20 @@ describe Hash do
74
97
 
75
98
  describe "to_soap_fault_message" do
76
99
  it "returns a SOAP fault message for SOAP version 1" do
77
- soap_fault = soap_fault_hash(
100
+ soap_fault = {
78
101
  "faultcode" => "soap:Server",
79
102
  "faultstring" => "Fault occurred while processing."
80
- )
103
+ }
81
104
 
82
105
  soap_fault.to_soap_fault_message.should be_a String
83
106
  soap_fault.to_soap_fault_message.should_not be_empty
84
107
  end
85
108
 
86
109
  it "returns a SOAP fault message for SOAP version 2" do
87
- soap_fault = soap_fault_hash(
110
+ soap_fault = {
88
111
  "code" => { "value" => "soap:Server" },
89
112
  "reason" => { "text" => "Fault occurred while processing." }
90
- )
113
+ }
91
114
 
92
115
  soap_fault.to_soap_fault_message.should be_a String
93
116
  soap_fault.to_soap_fault_message.should_not be_empty
@@ -96,14 +119,6 @@ describe Hash do
96
119
  it "returns nil in case the Hash does not include a SOAP fault" do
97
120
  { :soap_fault => false }.to_soap_fault_message.should be_nil
98
121
  end
99
-
100
- it "returns nil for unknown SOAP faults" do
101
- soap_fault_hash.to_soap_fault_message.should be_nil
102
- end
103
-
104
- def soap_fault_hash(details = {})
105
- { "soap:Envelope" => { "soap:Body" => { "soap:Fault" => details } } }
106
- end
107
122
  end
108
123
 
109
124
  describe "map_soap_response" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: savon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Harrington