Class: Savon::Response
Overview
Savon::Response
Savon::Response represents both HTTP and SOAP response.
SOAP fault
Assuming the default behavior of raising errors is disabled, you can ask the response object if there was a SOAP fault or an HTTP error and get the SOAP fault or HTTP error message.
response.soap_fault?
# => true
response.soap_fault
# => "(soap:Server) Fault occurred while processing."
response.http_error?
# => true
response.http_error
# => "Not found (404)"
Response as XML
To get the raw SOAP response XML, you can call to_xml or to_s on the response object.
response.to_xml
=> "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
=> "..."
=> "</soap:Envelope>"
Response as a Hash
You can also let Savon translate the SOAP response body to a Hash.
response.to_hash
=> { :findUserByIdResponse => {
=> :id => "123",
=> :username => "eve"
=> :active => true
=> }
When translating the SOAP response to a Hash, some XML tags and values are converted to more convenient Ruby objects. Translation is done through John Nunemaker’s Crack library along with some custom mapping.
-
XML tags (Hash keys) are converted to snake_case Symbols and namespaces are stripped off
-
SOAP xs:nil values are converted to nil objects
-
XML values specified in xs:DateTime format are converted to DateTime objects
-
XML values of “true” and “false” are converted to TrueClass and FalseClass
Net::HTTP response
If for some reason you need to access the Net::HTTP response object … you can.
bc. response.http
=> #<Net::HTTPOK:0x7f749a1aa4a8>
Constant Summary collapse
- MaxNonErrorResponseCode =
The maximum HTTP response code considered to be OK.
299
- @@raise_errors =
The global setting of whether to raise errors.
true
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the HTTP response object.
-
#http_error ⇒ Object
readonly
Returns the HTTP error message.
-
#soap_fault ⇒ Object
readonly
Returns the SOAP fault message.
Class Method Summary collapse
-
.raise_errors=(raise_errors) ⇒ Object
Sets the global setting of whether to raise errors.
-
.raise_errors? ⇒ Boolean
Returns the global setting of whether to raise errors.
Instance Method Summary collapse
-
#http_error? ⇒ Boolean
Returns whether there was an HTTP error.
-
#initialize(http) ⇒ Response
constructor
Expects a Net::HTTPResponse and handles errors.
-
#soap_fault? ⇒ Boolean
Returns whether there was a SOAP fault.
-
#to_hash ⇒ Object
Returns the SOAP response body as a Hash.
-
#to_xml ⇒ Object
(also: #to_s)
Returns the SOAP response XML.
Constructor Details
#initialize(http) ⇒ Response
Expects a Net::HTTPResponse and handles errors.
78 79 80 81 82 83 |
# File 'lib/savon/response.rb', line 78 def initialize(http) @http = http handle_soap_fault handle_http_error end |
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the HTTP response object.
112 113 114 |
# File 'lib/savon/response.rb', line 112 def http @http end |
#http_error ⇒ Object (readonly)
Returns the HTTP error message.
99 100 101 |
# File 'lib/savon/response.rb', line 99 def http_error @http_error end |
#soap_fault ⇒ Object (readonly)
Returns the SOAP fault message.
91 92 93 |
# File 'lib/savon/response.rb', line 91 def soap_fault @soap_fault end |
Class Method Details
.raise_errors=(raise_errors) ⇒ Object
Sets the global setting of whether to raise errors.
68 69 70 |
# File 'lib/savon/response.rb', line 68 def self.raise_errors=(raise_errors) @@raise_errors = raise_errors end |
.raise_errors? ⇒ Boolean
Returns the global setting of whether to raise errors.
73 74 75 |
# File 'lib/savon/response.rb', line 73 def self.raise_errors? @@raise_errors end |
Instance Method Details
#http_error? ⇒ Boolean
Returns whether there was an HTTP error.
94 95 96 |
# File 'lib/savon/response.rb', line 94 def http_error? !@http_error.blank? end |
#soap_fault? ⇒ Boolean
Returns whether there was a SOAP fault.
86 87 88 |
# File 'lib/savon/response.rb', line 86 def soap_fault? !@soap_fault.blank? end |
#to_hash ⇒ Object
Returns the SOAP response body as a Hash.
102 103 104 |
# File 'lib/savon/response.rb', line 102 def to_hash @hash ||= (Crack::XML.parse(body) rescue {}).find_soap_body end |
#to_xml ⇒ Object Also known as: to_s
Returns the SOAP response XML.
107 108 109 |
# File 'lib/savon/response.rb', line 107 def to_xml body end |