Class: Savon::SOAP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/savon/soap/response.rb

Overview

= Savon::SOAP::Response

Represents the SOAP response and contains the HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, response) ⇒ Response

Expects an HTTPI::Response and handles errors.



15
16
17
18
19
# File 'lib/savon/soap/response.rb', line 15

def initialize(config, response)
  self.config = config
  self.http = response
  raise_errors if config.raise_errors
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



21
22
23
# File 'lib/savon/soap/response.rb', line 21

def config
  @config
end

#httpObject

Returns the value of attribute http.



21
22
23
# File 'lib/savon/soap/response.rb', line 21

def http
  @http
end

Instance Method Details

#[](key) ⇒ Object

Shortcut accessor for the SOAP response body Hash.



49
50
51
# File 'lib/savon/soap/response.rb', line 49

def [](key)
  body[key]
end

#bodyObject Also known as: to_hash

Returns the SOAP response body as a Hash.



62
63
64
65
66
67
# File 'lib/savon/soap/response.rb', line 62

def body
  if !hash.has_key? :envelope
    raise Savon::SOAP::InvalidResponseError, "Unable to parse response body '#{to_xml}'"
  end
  hash[:envelope][:body]
end

#docObject

Returns a Nokogiri::XML::Document for the SOAP response XML.



94
95
96
# File 'lib/savon/soap/response.rb', line 94

def doc
  @doc ||= Nokogiri::XML(to_xml)
end

#hashObject

Returns the complete SOAP response XML without normalization.



84
85
86
# File 'lib/savon/soap/response.rb', line 84

def hash
  @hash ||= Nori.parse(to_xml)
end

#headerObject

Returns the SOAP response header as a Hash.



54
55
56
57
58
59
# File 'lib/savon/soap/response.rb', line 54

def header
  if !hash.has_key? :envelope
    raise Savon::SOAP::InvalidResponseError, "Unable to parse response body '#{to_xml}'"
  end
  hash[:envelope][:header]
end

#http_errorObject

Returns the Savon::HTTP::Error.



44
45
46
# File 'lib/savon/soap/response.rb', line 44

def http_error
  @http_error ||= HTTP::Error.new http
end

#http_error?Boolean

Returns whether there was an HTTP error.

Returns:

  • (Boolean)


39
40
41
# File 'lib/savon/soap/response.rb', line 39

def http_error?
  http_error.present?
end

#soap_faultObject

Returns the Savon::SOAP::Fault.



34
35
36
# File 'lib/savon/soap/response.rb', line 34

def soap_fault
  @soap_fault ||= Fault.new http
end

#soap_fault?Boolean

Returns whether there was a SOAP fault.

Returns:

  • (Boolean)


29
30
31
# File 'lib/savon/soap/response.rb', line 29

def soap_fault?
  soap_fault.present?
end

#success?Boolean

Returns whether the request was successful.

Returns:

  • (Boolean)


24
25
26
# File 'lib/savon/soap/response.rb', line 24

def success?
  !soap_fault? && !http_error?
end

#to_array(*path) ⇒ Object

Traverses the SOAP response body Hash for a given +path+ of Hash keys and returns the value as an Array. Defaults to return an empty Array in case the path does not exist or returns nil.



74
75
76
77
78
79
80
81
# File 'lib/savon/soap/response.rb', line 74

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

#to_xmlObject

Returns the SOAP response XML.



89
90
91
# File 'lib/savon/soap/response.rb', line 89

def to_xml
  http.body
end

#xpath(path, namespaces = nil) ⇒ Object

Returns an Array of Nokogiri::XML::Node objects retrieved with the given +path+. Automatically adds all of the document's namespaces unless a +namespaces+ hash is provided.



100
101
102
# File 'lib/savon/soap/response.rb', line 100

def xpath(path, namespaces = nil)
  doc.xpath(path, namespaces || xml_namespaces)
end