Class: Savon::SOAP::XML

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

Overview

= Savon::SOAP::XML

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

Constant Summary collapse

SCHEMA_TYPES =

XML Schema Type namespaces.

{
  "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ XML

Expects a +config+ object.



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

def initialize(config)
  self.config = config
end

Instance Attribute Details

#bodyObject

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



138
139
140
141
# File 'lib/savon/soap/xml.rb', line 138

def body
  @body = yield builder(nil) if block_given?
  @body
end

#configObject

Returns the value of attribute config.



33
34
35
# File 'lib/savon/soap/xml.rb', line 33

def config
  @config
end

#element_form_defaultObject

Accessor for whether all local elements should be namespaced.



116
117
118
# File 'lib/savon/soap/xml.rb', line 116

def element_form_default
  @element_form_default
end

#encodingObject

Returns the SOAP request encoding. Defaults to "UTF-8".



129
130
131
# File 'lib/savon/soap/xml.rb', line 129

def encoding
  @encoding ||= "UTF-8"
end

#endpointObject

Accessor for the SOAP +endpoint+.



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

def endpoint
  @endpoint
end

#env_namespaceObject

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



64
65
66
# File 'lib/savon/soap/xml.rb', line 64

def env_namespace
  @env_namespace ||= config.env_namespace.nil? ? :env : config.env_namespace
end

#headerObject

Returns the SOAP +header+. Defaults to an empty Hash.



56
57
58
# File 'lib/savon/soap/xml.rb', line 56

def header
  @header ||= config.soap_header.nil? ? {} : config.soap_header
end

#inputObject

Accessor for the SOAP +input+ tag.



36
37
38
# File 'lib/savon/soap/xml.rb', line 36

def input
  @input
end

#namespaceObject

Accessor for the default namespace URI.



119
120
121
# File 'lib/savon/soap/xml.rb', line 119

def namespace
  @namespace
end

#namespace_identifierObject

Returns the default namespace identifier.



111
112
113
# File 'lib/savon/soap/xml.rb', line 111

def namespace_identifier
  @namespace_identifier ||= :wsdl
end

#namespacesObject

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



72
73
74
75
76
77
78
# File 'lib/savon/soap/xml.rb', line 72

def namespaces
  @namespaces ||= begin
    key = ["xmlns"]
    key << env_namespace if env_namespace && env_namespace != ""
    { key.join(":") => SOAP::NAMESPACE[version] }
  end
end

#wsseObject

Accessor for the Savon::WSSE object.



122
123
124
# File 'lib/savon/soap/xml.rb', line 122

def wsse
  @wsse
end

#xml(directive_tag = :xml, attrs = {}) ⇒ Object

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



149
150
151
# File 'lib/savon/soap/xml.rb', line 149

def xml(directive_tag = :xml, attrs = {})
  @xml = yield builder(directive_tag, attrs) if block_given?
end

Instance Method Details

#namespace_by_uri(uri) ⇒ Object



80
81
82
83
84
85
# File 'lib/savon/soap/xml.rb', line 80

def namespace_by_uri(uri)
  namespaces.each do |candidate_identifier, candidate_uri|
    return candidate_identifier.gsub(/^xmlns:/, '') if candidate_uri == uri
  end
  nil
end

#signature?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/savon/soap/xml.rb', line 124

def signature?
  wsse.respond_to?(:signature?) && wsse.signature?
end

#to_xml(clear_cache = false) ⇒ Object

Returns the XML for a SOAP request.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/savon/soap/xml.rb', line 157

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

#typesObject



103
104
105
# File 'lib/savon/soap/xml.rb', line 103

def types
  @types ||= {}
end

#use_namespace(path, uri) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/savon/soap/xml.rb', line 91

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_namespacesObject



87
88
89
# File 'lib/savon/soap/xml.rb', line 87

def used_namespaces
  @used_namespaces ||= {}
end

#versionObject

Returns the SOAP +version+. Defaults to Savon.config.soap_version.



48
49
50
# File 'lib/savon/soap/xml.rb', line 48

def version
  @version ||= config.soap_version
end

#version=(version) ⇒ Object

Sets the SOAP +version+.

Raises:

  • (ArgumentError)


42
43
44
45
# File 'lib/savon/soap/xml.rb', line 42

def version=(version)
  raise ArgumentError, "Invalid SOAP version: #{version}" unless SOAP::VERSIONS.include? version
  @version = version
end