require "savon/soap/xml"
require "savon/soap/fault"
require "savon/soap/invalid_response_error"
require "savon/http/error"
module Savon
module SOAP
class Response
def initialize(config, response)
self.config = config
self.http = response
raise_errors if config.raise_errors
end
attr_accessor :http, :config
def success?
!soap_fault? && !http_error?
end
def soap_fault?
soap_fault.present?
end
def soap_fault
@soap_fault ||= Fault.new http
end
def http_error?
http_error.present?
end
def http_error
@http_error ||= HTTP::Error.new http
end
def [](key)
body[key]
end
def
if !hash.has_key? :envelope
raise Savon::SOAP::InvalidResponseError, "Unable to parse response body '#{to_xml}'"
end
hash[:envelope][:header]
end
def body
if !hash.has_key? :envelope
raise Savon::SOAP::InvalidResponseError, "Unable to parse response body '#{to_xml}'"
end
hash[:envelope][:body]
end
alias to_hash body
def to_array(*path)
result = path.inject body do |memo, key|
return [] unless memo[key]
memo[key]
end
result.kind_of?(Array) ? result.compact : [result].compact
end
def hash
@hash ||= Nori.parse(to_xml)
end
def to_xml
http.body
end
def doc
@doc ||= Nokogiri::XML(to_xml)
end
def xpath(path, namespaces = nil)
doc.xpath(path, namespaces || xml_namespaces)
end
private
def raise_errors
raise soap_fault if soap_fault?
raise http_error if http_error?
end
def xml_namespaces
@xml_namespaces ||= doc.collect_namespaces
end
end
end
end