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
-
.error_handler(&blk) ⇒ Object
Sets a specific handler for errors to raise.
-
.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.
81 82 83 84 85 86 |
# File 'lib/savon/response.rb', line 81 def initialize(http) @http = http handle_soap_fault handle_http_error end |
Instance Attribute Details
#http ⇒ Object (readonly)
Returns the HTTP response object.
115 116 117 |
# File 'lib/savon/response.rb', line 115 def http @http end |
#http_error ⇒ Object (readonly)
Returns the HTTP error message.
102 103 104 |
# File 'lib/savon/response.rb', line 102 def http_error @http_error end |
#soap_fault ⇒ Object (readonly)
Returns the SOAP fault message.
94 95 96 |
# File 'lib/savon/response.rb', line 94 def soap_fault @soap_fault end |
Class Method Details
.error_handler(&blk) ⇒ Object
Sets a specific handler for errors to raise.
77 78 79 |
# File 'lib/savon/response.rb', line 77 def self.error_handler(&blk) @@error_handler = blk end |
.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.
97 98 99 |
# File 'lib/savon/response.rb', line 97 def http_error? !@http_error.blank? end |
#soap_fault? ⇒ Boolean
Returns whether there was a SOAP fault.
89 90 91 |
# File 'lib/savon/response.rb', line 89 def soap_fault? !@soap_fault.blank? end |
#to_hash ⇒ Object
Returns the SOAP response body as a Hash.
105 106 107 |
# File 'lib/savon/response.rb', line 105 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.
110 111 112 |
# File 'lib/savon/response.rb', line 110 def to_xml body end |