class GoogleAdsSavon::SOAP::XML

GoogleAdsSavon::SOAP::XML

Represents the SOAP request XML. Contains various global and per request/instance settings like the SOAP version, header, body and namespaces.

Constants

SCHEMA_TYPES

XML Schema Type namespaces.

Attributes

body[W]

Sets the SOAP body. Expected to be a Hash that can be translated to XML via `Gyoku.xml` or any other Object responding to to_s.

config[RW]
element_form_default[RW]

Accessor for whether all local elements should be namespaced.

encoding[W]

Sets the SOAP request encoding.

endpoint[RW]

Accessor for the SOAP endpoint.

env_namespace[W]

Sets the SOAP envelope namespace.

header[W]

Sets the SOAP header Hash.

input[RW]

Accessor for the SOAP input tag.

namespace[RW]

Accessor for the default namespace URI.

namespace_identifier[W]

Sets the default namespace identifier.

namespaces[W]

Sets the namespaces Hash.

wsse[RW]

Accessor for the GoogleAdsSavon::WSSE object.

xml[W]

Accepts an XML String and lets you specify a completely custom request body.

Public Class Methods

new(config) click to toggle source

Expects a config object.

# File lib/ads_savon/soap/xml.rb, line 24
def initialize(config)
  self.config = config
end

Public Instance Methods

body() { |builder(nil)| ... } click to toggle source

Accepts a block and yields a Builder::XmlMarkup object to let you create custom body XML.

# File lib/ads_savon/soap/xml.rb, line 133
def body
  @body = yield builder(nil) if block_given?
  @body
end
encoding() click to toggle source

Returns the SOAP request encoding. Defaults to “UTF-8”.

# File lib/ads_savon/soap/xml.rb, line 124
def encoding
  @encoding ||= "UTF-8"
end
env_namespace() click to toggle source

Returns the SOAP envelope namespace. Uses the global namespace if set Defaults to :env.

# File lib/ads_savon/soap/xml.rb, line 59
def env_namespace
  @env_namespace ||= config.env_namespace.nil? ? :env : config.env_namespace
end
header() click to toggle source

Returns the SOAP header. Defaults to an empty Hash.

# File lib/ads_savon/soap/xml.rb, line 51
def header
  @header ||= config.soap_header.nil? ? {} : config.soap_header
end
namespace_by_uri(uri) click to toggle source
# File lib/ads_savon/soap/xml.rb, line 75
def namespace_by_uri(uri)
  namespaces.each do |candidate_identifier, candidate_uri|
    return candidate_identifier.gsub(/^xmlns:/, '') if candidate_uri == uri
  end
  nil
end
namespace_identifier() click to toggle source

Returns the default namespace identifier.

# File lib/ads_savon/soap/xml.rb, line 106
def namespace_identifier
  @namespace_identifier ||= :wsdl
end
namespaces() click to toggle source

Returns the namespaces. Defaults to a Hash containing the SOAP envelope namespace.

# File lib/ads_savon/soap/xml.rb, line 67
def namespaces
  @namespaces ||= begin
    key = ["xmlns"]
    key << env_namespace if env_namespace && env_namespace != ""
    { key.join(":") => SOAP::NAMESPACE[version] }
  end
end
signature?() click to toggle source
# File lib/ads_savon/soap/xml.rb, line 119
def signature?
  wsse.respond_to?(:signature?) && wsse.signature?
end
to_xml(clear_cache = false) click to toggle source

Returns the XML for a SOAP request.

# File lib/ads_savon/soap/xml.rb, line 152
def to_xml(clear_cache = false)
  if clear_cache
    @xml = nil
    @header_for_xml = nil
  end

  @xml ||= tag(builder, :Envelope, complete_namespaces) do |xml|
    tag(xml, :Header) { xml << header_for_xml } unless header_for_xml.empty?

    # TODO: Maybe there should be some sort of plugin architecture where
    #       classes like WSSE::Signature can hook into this process.
    body_attributes = (signature? ? wsse.signature.body_attributes : {})

    if input.nil?
      tag(xml, :Body, body_attributes)
    else
      tag(xml, :Body, body_attributes) { xml.tag!(*add_namespace_to_input) { xml << body_to_xml } }
    end
  end
end
types() click to toggle source
# File lib/ads_savon/soap/xml.rb, line 98
def types
  @types ||= {}
end
use_namespace(path, uri) click to toggle source
# File lib/ads_savon/soap/xml.rb, line 86
def use_namespace(path, uri)
  @internal_namespace_count ||= 0

  unless identifier = namespace_by_uri(uri)
    identifier = "ins#{@internal_namespace_count}"
    namespaces["xmlns:#{identifier}"] = uri
    @internal_namespace_count += 1
  end

  used_namespaces[path] = identifier
end
used_namespaces() click to toggle source
# File lib/ads_savon/soap/xml.rb, line 82
def used_namespaces
  @used_namespaces ||= {}
end
version() click to toggle source

Returns the SOAP version. Defaults to GoogleAdsSavon.config.soap_version.

# File lib/ads_savon/soap/xml.rb, line 43
def version
  @version ||= config.soap_version
end
version=(version) click to toggle source

Sets the SOAP version.

# File lib/ads_savon/soap/xml.rb, line 37
def version=(version)
  raise ArgumentError, "Invalid SOAP version: #{version}" unless SOAP::VERSIONS.include? version
  @version = version
end
xml(directive_tag = :xml, attrs = {}) { |builder(directive_tag, attrs)| ... } click to toggle source

Accepts a block and yields a Builder::XmlMarkup object to let you create a completely custom XML.

# File lib/ads_savon/soap/xml.rb, line 144
def xml(directive_tag = :xml, attrs = {})
  @xml = yield builder(directive_tag, attrs) if block_given?
end

Private Instance Methods

add_namespace_to_input() click to toggle source
# File lib/ads_savon/soap/xml.rb, line 237
def add_namespace_to_input
  return input.compact unless used_namespaces[[input[1].to_s]]
  [used_namespaces[[input[1].to_s]], input[1], input[2]]
end
add_namespaces_to_body(hash, path = [input[1].to_s]) click to toggle source
# File lib/ads_savon/soap/xml.rb, line 216
def add_namespaces_to_body(hash, path = [input[1].to_s])
  return unless hash
  return hash.map { |value| add_namespaces_to_body(value, path) } if hash.kind_of?(Array)
  return hash.to_s unless hash.kind_of? Hash

  hash.inject({}) do |newhash, (key, value)|
    camelcased_key = Gyoku::XMLKey.create(key)
    newpath = path + [camelcased_key]

    if used_namespaces[newpath]
      newhash.merge(
        "#{used_namespaces[newpath]}:#{camelcased_key}" =>
          add_namespaces_to_body(value, types[newpath] ? [types[newpath]] : newpath)
      )
    else
      add_namespaces_to_values(value, path) if key == :order!
      newhash.merge(key => value)
    end
  end
end
add_namespaces_to_values(values, path) click to toggle source
# File lib/ads_savon/soap/xml.rb, line 242
def add_namespaces_to_values(values, path)
  values.collect! { |value|
    camelcased_value = Gyoku::XMLKey.create(value)
    namespace_path = path + [camelcased_value.to_s]
    namespace = used_namespaces[namespace_path]
    "#{namespace.blank? ? '' : namespace + ":"}#{camelcased_value}"
  }
end
body_to_xml() click to toggle source

Returns the SOAP body as an XML String.

# File lib/ads_savon/soap/xml.rb, line 210
def body_to_xml
  return body.to_s unless body.kind_of? Hash
  body_to_xml = element_form_default == :qualified ? add_namespaces_to_body(body) : body
  Gyoku.xml body_to_xml, :element_form_default => element_form_default, :namespace => namespace_identifier
end
builder(directive_tag = :xml, attrs = { :encoding => encoding }) click to toggle source

Returns a new Builder::XmlMarkup object.

# File lib/ads_savon/soap/xml.rb, line 176
def builder(directive_tag = :xml, attrs = { :encoding => encoding })
  builder = Builder::XmlMarkup.new
  builder.instruct!(directive_tag, attrs) if directive_tag
  builder
end
complete_namespaces() click to toggle source

Returns the complete Hash of namespaces.

# File lib/ads_savon/soap/xml.rb, line 193
def complete_namespaces
  defaults = SCHEMA_TYPES.dup
  defaults["xmlns:#{namespace_identifier}"] = namespace if namespace
  defaults.merge namespaces
end
header_for_xml() click to toggle source

Returns the SOAP header as an XML String.

# File lib/ads_savon/soap/xml.rb, line 200
def header_for_xml
  @header_for_xml ||= (Hash === header ? Gyoku.xml(header) : header) + wsse_header
end
tag(xml, name, namespaces = {}, &block) click to toggle source

Expects a builder xml instance, a tag name and accepts optional namespaces and a block to create an XML tag.

# File lib/ads_savon/soap/xml.rb, line 184
def tag(xml, name, namespaces = {}, &block)
  if env_namespace && env_namespace != ""
    xml.tag! env_namespace, name, namespaces, &block
  else
    xml.tag! name, namespaces, &block
  end
end
wsse_header() click to toggle source

Returns the WSSE header or an empty String in case WSSE was not set.

# File lib/ads_savon/soap/xml.rb, line 205
def wsse_header
  wsse.respond_to?(:to_xml) ? wsse.to_xml : ""
end