smacks-savon 0.1.2 → 0.1.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,9 +4,73 @@ require "apricoteatsgorilla"
4
4
 
5
5
  module Savon
6
6
 
7
- # Savon::Response validates and represents the HTTP/SOAP response.
7
+ # Savon::HTTPResponse containes methods exclusive to a Net::HTTPResponse
8
+ # to be included in Savon::Response.
9
+ module HTTPResponse
10
+
11
+ # Returns the error code.
12
+ attr_reader :error_code
13
+
14
+ # Returns the error message.
15
+ attr_reader :error_message
16
+
17
+ # Returns +true+ in case the request was successful, +false+ otherwise
18
+ # or +nil+ in case there was no request at all.
19
+ def success?
20
+ return nil unless @response
21
+ @error_code.nil?
22
+ end
23
+
24
+ # Returns +false+ in case the request was not successful, +true+ otherwise
25
+ # or +nil+ in case there was no request at all.
26
+ def error?
27
+ return nil unless @response
28
+ !@error_code.nil?
29
+ end
30
+
31
+ private
32
+
33
+ # Validates the response against HTTP errors and SOAP faults and sets the
34
+ # +error_code+ and +error_message+ in case the request was not successful.
35
+ def validate_response
36
+ if @response.code.to_i >= 300
37
+ @error_message = @response.message
38
+ @error_code = @response.code
39
+ else
40
+ soap_fault = ApricotEatsGorilla[@response.body, "//soap:Fault"]
41
+ unless soap_fault.nil? || soap_fault.empty?
42
+ @error_message = soap_fault[:faultstring]
43
+ @error_code = soap_fault[:faultcode]
44
+ end
45
+ end
46
+ end
47
+
48
+ # Translates the response body into a Hash. Takes an optional +root_node+
49
+ # argument to start parsing the response body at a custom root node.
50
+ def response_to_hash(root_node = nil)
51
+ ApricotEatsGorilla[@response.body, root_node]
52
+ end
53
+
54
+ end
55
+
56
+ # Savon::Response represents the HTTP/SOAP response.
8
57
  class Response
9
58
 
59
+ include Savon::HTTPResponse
60
+
61
+ # The default root node to start parsing the SOAP response at.
62
+ @@default_root_node = "//return"
63
+
64
+ # Returns the default root node.
65
+ def self.default_root_node
66
+ @@default_root_node
67
+ end
68
+
69
+ # Sets the default root node.
70
+ def self.default_root_node=(root_node)
71
+ @@default_root_node = root_node if root_node.kind_of? String
72
+ end
73
+
10
74
  # The core (inherited) methods to shadow.
11
75
  @@core_methods_to_shadow = [:id]
12
76
 
@@ -20,12 +84,6 @@ module Savon
20
84
  @@core_methods_to_shadow = methods if methods.kind_of? Array
21
85
  end
22
86
 
23
- # Returns the error code.
24
- attr_reader :error_code
25
-
26
- # Returns the error message.
27
- attr_reader :error_message
28
-
29
87
  # Initializer expects the +source+ to initialize from. Sets up the instance
30
88
  # from a given Net::HTTPResponse or a Hash depending on the given +source+.
31
89
  # May be called with a custom +root_node+ to start parsing the response at
@@ -43,23 +101,11 @@ module Savon
43
101
  value_from_hash(key)
44
102
  end
45
103
 
46
- # Returns +true+ in case the request was successful, +false+ otherwise
47
- # or +nil+ in case there's no request at all.
48
- def success?
49
- return nil unless @response
50
- @error_code.nil?
51
- end
52
-
53
- # Returns +false+ in case the request was not successful, +false+ otherwise
54
- # or +nil+ in case there's no request at all.
55
- def error?
56
- return nil unless @response
57
- !@error_code.nil?
58
- end
59
-
60
- # Returns the response Hash.
61
- def to_hash
62
- @hash
104
+ # Returns the response Hash. Takes an optional +root_node+ to start parsing
105
+ # the SOAP response at instead of the +@@default_root_node+.
106
+ def to_hash(root_node = nil)
107
+ return @hash if root_node.nil?
108
+ response_to_hash(root_node)
63
109
  end
64
110
 
65
111
  # Returns the response body in case there is any, +nil+ otherwise.
@@ -73,9 +119,9 @@ module Savon
73
119
  # Returns a new Savon::Response instance containing the value or returns
74
120
  # the actual value in case it is not a Hash.
75
121
  def method_missing(method, *args)
76
- value = value_from_hash method
122
+ value = value_from_hash(method)
77
123
  return value unless value.kind_of? Hash
78
- Savon::Response.new value
124
+ Savon::Response.new(value)
79
125
  end
80
126
 
81
127
  private
@@ -90,8 +136,8 @@ module Savon
90
136
  validate_response
91
137
 
92
138
  if success?
93
- root_node ||= "//return"
94
- hash = ApricotEatsGorilla[@response.body, root_node]
139
+ root_node ||= @@default_root_node
140
+ hash = response_to_hash(root_node)
95
141
  initialize_from_hash hash
96
142
  end
97
143
  end
@@ -102,21 +148,6 @@ module Savon
102
148
  shadow_core_methods
103
149
  end
104
150
 
105
- # Validates the response against HTTP errors and SOAP faults and sets the
106
- # +error_code+ and +error_message+ in case the request was not successful.
107
- def validate_response
108
- if @response.code.to_i >= 300
109
- @error_message = @response.message
110
- @error_code = @response.code
111
- else
112
- soap_fault = ApricotEatsGorilla[@response.body, "//soap:Fault"]
113
- unless soap_fault.nil?
114
- @error_message = soap_fault[:faultstring]
115
- @error_code = soap_fault[:faultcode]
116
- end
117
- end
118
- end
119
-
120
151
  # Dynamically defines methods from the Array of +@@core_methods_to_shadow+
121
152
  # to "shadow" inherited methods. Returns a value from the response Hash in
122
153
  # case a matching public method and a key from the Hash could be found.
@@ -131,8 +162,9 @@ module Savon
131
162
  # Returns a value from the response Hash. Tries to convert the given +key+
132
163
  # into a Symbol or a String to find the value to return.
133
164
  def value_from_hash(key)
165
+ return nil unless @hash
134
166
  @hash[key.to_sym] || @hash[key.to_s]
135
167
  end
136
-
137
168
  end
169
+
138
170
  end
data/lib/savon/service.rb CHANGED
@@ -5,7 +5,7 @@ require "apricoteatsgorilla"
5
5
 
6
6
  module Savon
7
7
 
8
- # Ruby SOAP client library to enjoy.
8
+ # Savon - Ruby SOAP client library to enjoy.
9
9
  #
10
10
  # Communicating with a SOAP webservice can be done in two lines of code.
11
11
  # Instantiate a new Savon::Service passing in the URI to the WSDL of the
data/lib/savon/wsdl.rb CHANGED
@@ -4,7 +4,7 @@ require "hpricot"
4
4
 
5
5
  module Savon
6
6
 
7
- # Savon::Wsdl gets, parses and represents the WSDL.
7
+ # Savon::Wsdl represents the WSDL.
8
8
  class Wsdl
9
9
 
10
10
  # The namespace URI.
@@ -36,13 +36,13 @@ module SoapResponseFixture
36
36
  '<soap:Body>' <<
37
37
  '<soap:Fault>' <<
38
38
  '<faultcode>' << soap_fault_code << '</faultcode>' <<
39
- '<faultstring>' << soap_fault << '</faultstring>' <<
39
+ '<faultstring>' << soap_fault_message << '</faultstring>' <<
40
40
  '</soap:Fault>' <<
41
41
  '</soap:Body>' <<
42
42
  '</soap:Envelope>'
43
43
  end
44
44
 
45
- def soap_fault
45
+ def soap_fault_message
46
46
  "Failed to authenticate client."
47
47
  end
48
48
 
data/test/helper.rb CHANGED
@@ -35,7 +35,7 @@ module TestHelper
35
35
  build_response_mock("200", "OK", response_body)
36
36
  end
37
37
 
38
- def response_fault_mock
38
+ def response_error_mock
39
39
  build_response_mock("404", "NotFound")
40
40
  end
41
41
 
@@ -1,18 +1,18 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class SavonResponseTest < Test::Unit::TestCase
3
+ class TestSavonResponse < Test::Unit::TestCase
4
4
 
5
5
  include TestHelper
6
6
  include SoapResponseFixture
7
7
 
8
8
  context "A Savon::Response instance" do
9
+ setup do
10
+ ApricotEatsGorilla.sort_keys = true
11
+ Savon::Response.core_methods_to_shadow = [:id] # set to default
12
+ end
9
13
 
10
14
  context "initialized with a Net::HTTPResponse containing a successful SOAP response" do
11
- setup do
12
- ApricotEatsGorilla.sort_keys = true
13
- Savon::Response.core_methods_to_shadow = [:id] # set to default
14
- @response = Savon::Response.new response_mock(some_soap_response)
15
- end
15
+ setup { @response = Savon::Response.new response_mock(some_soap_response) }
16
16
 
17
17
  should "return that the request was successful" do
18
18
  assert_equal true, @response.success?
@@ -74,11 +74,7 @@ class SavonResponseTest < Test::Unit::TestCase
74
74
  end
75
75
 
76
76
  context "initialized with a Hash" do
77
- setup do
78
- ApricotEatsGorilla.sort_keys = true
79
- Savon::Response.core_methods_to_shadow = [:id] # set to default
80
- @response = Savon::Response.new some_response_hash
81
- end
77
+ setup { @response = Savon::Response.new some_response_hash }
82
78
 
83
79
  should "return nil for HTTP::Response-specific methods" do
84
80
  assert_nil @response.success?
@@ -130,6 +126,66 @@ class SavonResponseTest < Test::Unit::TestCase
130
126
  end
131
127
  end
132
128
 
129
+ context "initialized with a Net::HTTPResponse containing a SOAP fault" do
130
+ setup { @response = Savon::Response.new response_mock(soap_fault_response) }
131
+
132
+ should "return that the request was not successful" do
133
+ assert_equal false, @response.success?
134
+ assert_equal true, @response.error?
135
+ end
136
+
137
+ should "return the error_code and error_message" do
138
+ assert_equal soap_fault_code, @response.error_code
139
+ assert_equal soap_fault_message, @response.error_message
140
+ end
141
+
142
+ should "return the SOAP response XML when calling to_s" do
143
+ assert_equal soap_fault_response, @response.to_s
144
+ end
145
+
146
+ should "return nil when calling to_hash" do
147
+ assert_nil @response.to_hash
148
+ end
149
+
150
+ should "return nil when trying to access Hash values through []" do
151
+ assert_nil @response[:some_key]
152
+ end
153
+
154
+ should "return nil when trying to access Hash values through method_missing" do
155
+ assert_nil @response.some_key
156
+ end
157
+ end
158
+
159
+ context "initialized with a Net::HTTPResponse error" do
160
+ setup { @response = Savon::Response.new response_error_mock }
161
+
162
+ should "return that the request was not successful" do
163
+ assert_equal false, @response.success?
164
+ assert_equal true, @response.error?
165
+ end
166
+
167
+ should "return the error_code and error_message" do
168
+ assert_equal "404", @response.error_code
169
+ assert_equal "NotFound", @response.error_message
170
+ end
171
+
172
+ should "return nil when calling to_s" do
173
+ assert_nil @response.to_s
174
+ end
175
+
176
+ should "return nil when calling to_hash" do
177
+ assert_nil @response.to_hash
178
+ end
179
+
180
+ should "return nil when trying to access Hash values through []" do
181
+ assert_nil @response[:some_key]
182
+ end
183
+
184
+ should "return nil when trying to access Hash values through method_missing" do
185
+ assert_nil @response.some_key
186
+ end
187
+ end
188
+
133
189
  end
134
190
 
135
191
  end
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class SavonServiceTest < Test::Unit::TestCase
3
+ class TestSavonService < Test::Unit::TestCase
4
4
 
5
5
  include TestHelper
6
6
  include SoapResponseFixture
@@ -1,6 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "helper")
2
2
 
3
- class SavonWsdlTest < Test::Unit::TestCase
3
+ class TestSavonWsdl < Test::Unit::TestCase
4
4
 
5
5
  include TestHelper
6
6
 
@@ -0,0 +1,4 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "savon"))
2
+ require "test_service"
3
+ require "test_wsdl"
4
+ require "test_response"
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.1.2
4
+ version: 0.1.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.4.3
33
+ version: 0.4.5
34
34
  version:
35
35
  description: Ruby SOAP client library to enjoy.
36
36
  email:
@@ -74,10 +74,10 @@ signing_key:
74
74
  specification_version: 2
75
75
  summary: Ruby SOAP client library to enjoy.
76
76
  test_files:
77
- - test/savon_test.rb
77
+ - test/test_savon.rb
78
78
  - test/helper.rb
79
79
  - test/factories/wsdl.rb
80
80
  - test/fixtures/soap_response_fixture.rb
81
- - test/savon/service_test.rb
82
- - test/savon/response_test.rb
83
- - test/savon/wsdl_test.rb
81
+ - test/savon/test_service.rb
82
+ - test/savon/test_response.rb
83
+ - test/savon/test_wsdl.rb
data/test/savon_test.rb DELETED
@@ -1,4 +0,0 @@
1
- $:.unshift(File.join(File.dirname(__FILE__), "savon"))
2
- require "service_test"
3
- require "wsdl_test"
4
- require "response_test"