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
Accessor for whether all local elements should be namespaced.
Sets the SOAP
request encoding.
Accessor for the SOAP
endpoint
.
Sets the SOAP
envelope namespace.
Sets the SOAP
header
Hash.
Accessor for the SOAP
input
tag.
Accessor for the default namespace URI.
Sets the default namespace identifier.
Sets the namespaces
Hash.
Accessor for the GoogleAdsSavon::WSSE
object.
Accepts an XML
String and lets you specify a completely custom request body.
Public Class Methods
Expects a config
object.
# File lib/ads_savon/soap/xml.rb, line 24 def initialize(config) self.config = config end
Public Instance Methods
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
Returns the SOAP
request encoding. Defaults to “UTF-8”.
# File lib/ads_savon/soap/xml.rb, line 124 def encoding @encoding ||= "UTF-8" end
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
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
# 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
Returns the default namespace identifier.
# File lib/ads_savon/soap/xml.rb, line 106 def namespace_identifier @namespace_identifier ||= :wsdl end
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
# File lib/ads_savon/soap/xml.rb, line 119 def signature? wsse.respond_to?(:signature?) && wsse.signature? end
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
# File lib/ads_savon/soap/xml.rb, line 98 def types @types ||= {} end
# 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
# File lib/ads_savon/soap/xml.rb, line 82 def used_namespaces @used_namespaces ||= {} end
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
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
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
# 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
# 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
# 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
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
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
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
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
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