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

SchemaTypes =

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = nil, input = nil, body = nil) ⇒ XML

Accepts an endpoint, an input tag and a SOAP body.



49
50
51
52
53
# File 'lib/savon/soap/xml.rb', line 49

def initialize(endpoint = nil, input = nil, body = nil)
  self.endpoint = endpoint if endpoint
  self.input = input if input
  self.body = body if body
end

Instance Attribute Details

#bodyObject

Accessor for 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.



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

def body
  @body
end

#element_form_defaultObject

Returns whether all local elements should be namespaced. Might be set to :qualified, but defaults to :unqualified.



109
110
111
# File 'lib/savon/soap/xml.rb', line 109

def element_form_default
  @element_form_default ||= :unqualified
end

#endpointObject

Accessor for the SOAP endpoint.



59
60
61
# File 'lib/savon/soap/xml.rb', line 59

def endpoint
  @endpoint
end

#env_namespaceObject

Returns the SOAP envelope namespace. Defaults to :env.



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

def env_namespace
  @env_namespace ||= :soap
end

#headerObject

Returns the SOAP header. Defaults to an empty Hash.



76
77
78
# File 'lib/savon/soap/xml.rb', line 76

def header
  @header ||= {}
end

#inputObject

Accessor for the SOAP input tag.



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

def input
  @input
end

#namespaceObject

Accessor for the default namespace URI.



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

def namespace
  @namespace
end

#namespace_identifierObject

Returns the default namespace identifier.



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

def namespace_identifier
  @namespace_identifier ||= :wsdl
end

#namespacesObject

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



92
93
94
95
96
97
# File 'lib/savon/soap/xml.rb', line 92

def namespaces
  @namespaces ||= begin
    key = env_namespace.blank? ? "xmlns" : "xmlns:#{env_namespace}"
    { key => SOAP::Namespace[version] }
  end
end

#wsseObject

Accessor for the Savon::WSSE object.



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

def wsse
  @wsse
end

#xmlObject

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



127
128
129
# File 'lib/savon/soap/xml.rb', line 127

def xml
  @xml = yield builder if block_given?
end

Class Method Details

.parse(xml) ⇒ Object

Converts a given SOAP response xml to a Hash.



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

def self.parse(xml)
  Crack::XML.parse(xml) rescue {}
end

.to_array(object, *path) ⇒ Object

Expects a SOAP response XML or Hash, traverses it 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.



37
38
39
40
41
42
43
44
45
46
# File 'lib/savon/soap/xml.rb', line 37

def self.to_array(object, *path)
  hash = object.kind_of?(Hash) ? object : to_hash(object)

  result = path.inject hash do |memo, key|
    return [] unless memo[key]
    memo[key]
  end

  result.kind_of?(Array) ? result.compact : [result].compact
end

.to_hash(value) ⇒ Object

Converts the given SOAP response value (XML or Hash) into a normalized Hash.



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

def self.to_hash(value)
  value = parse value unless value.kind_of? Hash
  value.find_soap_body
end

Instance Method Details

#to_xmlObject

Returns the XML for a SOAP request.



135
136
137
138
139
140
# File 'lib/savon/soap/xml.rb', line 135

def to_xml
  @xml ||= tag(builder, :Envelope, complete_namespaces) do |xml|
    tag(xml, :Header) { xml << header_for_xml } unless header_for_xml.empty?
    input.nil? ? tag(xml, :Body) : tag(xml, :Body) { xml.tag!(*input) { xml << body_to_xml } }
  end
end

#versionObject

Returns the SOAP version. Defaults to Savon.soap_version.



68
69
70
# File 'lib/savon/soap/xml.rb', line 68

def version
  @version ||= Savon.soap_version
end

#version=(version) ⇒ Object

Sets the SOAP version.

Raises:

  • (ArgumentError)


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

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