class GoogleAdsSavon::SOAP::Response

GoogleAdsSavon::SOAP::Response

Represents the SOAP response and contains the HTTP response.

Attributes

config[RW]
http[RW]

Public Class Methods

new(config, response) click to toggle source

Expects an HTTPI::Response and handles errors.

# File lib/ads_savon/soap/response.rb, line 15
def initialize(config, response)
  self.config = config
  self.http = response
  raise_errors if config.raise_errors
end

Public Instance Methods

[](key) click to toggle source

Shortcut accessor for the SOAP response body Hash.

# File lib/ads_savon/soap/response.rb, line 49
def [](key)
  body[key]
end
body() click to toggle source

Returns the SOAP response body as a Hash.

# File lib/ads_savon/soap/response.rb, line 62
def body
  if !hash.has_key? :envelope
    raise GoogleAdsSavon::SOAP::InvalidResponseError, "Unable to parse response body '#{to_xml}'"
  end
  hash[:envelope][:body]
end
Also aliased as: to_hash
doc() click to toggle source

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

# File lib/ads_savon/soap/response.rb, line 94
def doc
  @doc ||= Nokogiri::XML(to_xml)
end
hash() click to toggle source

Returns the complete SOAP response XML without normalization.

# File lib/ads_savon/soap/response.rb, line 84
def hash
  @hash ||= nori.parse(to_xml)
end
header() click to toggle source

Returns the SOAP response header as a Hash.

# File lib/ads_savon/soap/response.rb, line 54
def header
  if !hash.has_key? :envelope
    raise GoogleAdsSavon::SOAP::InvalidResponseError, "Unable to parse response body '#{to_xml}'"
  end
  hash[:envelope][:header]
end
http_error() click to toggle source

Returns the GoogleAdsSavon::HTTP::Error.

# File lib/ads_savon/soap/response.rb, line 44
def http_error
  @http_error ||= HTTP::Error.new http
end
http_error?() click to toggle source

Returns whether there was an HTTP error.

# File lib/ads_savon/soap/response.rb, line 39
def http_error?
  http_error.present?
end
soap_fault() click to toggle source

Returns the GoogleAdsSavon::SOAP::Fault.

# File lib/ads_savon/soap/response.rb, line 34
def soap_fault
  @soap_fault ||= Fault.new http
end
soap_fault?() click to toggle source

Returns whether there was a SOAP fault.

# File lib/ads_savon/soap/response.rb, line 29
def soap_fault?
  soap_fault.present?
end
success?() click to toggle source

Returns whether the request was successful.

# File lib/ads_savon/soap/response.rb, line 24
def success?
  !soap_fault? && !http_error?
end
to_array(*path) click to toggle source

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.

# File lib/ads_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_hash()
Alias for: body
to_xml() click to toggle source

Returns the SOAP response XML.

# File lib/ads_savon/soap/response.rb, line 89
def to_xml
  http.body
end
xpath(path, namespaces = nil) click to toggle source

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.

# File lib/ads_savon/soap/response.rb, line 100
def xpath(path, namespaces = nil)
  doc.xpath(path, namespaces || xml_namespaces)
end

Private Instance Methods

nori() click to toggle source
# File lib/ads_savon/soap/response.rb, line 115
def nori
  return @nori if @nori

  nori_options = {
    :strip_namespaces      => true,
    :convert_tags_to       => lambda { |tag| tag.snakecase.to_sym },
    :empty_tag_value       => "",
    :advanced_typecasting  => false
  }

  non_nil_nori_options = nori_options.reject { |_, value| value.nil? }
  @nori = Nori.new(non_nil_nori_options)
end
raise_errors() click to toggle source
# File lib/ads_savon/soap/response.rb, line 106
def raise_errors
  raise soap_fault if soap_fault?
  raise http_error if http_error?
end
xml_namespaces() click to toggle source
# File lib/ads_savon/soap/response.rb, line 111
def xml_namespaces
  @xml_namespaces ||= doc.collect_namespaces
end