Class: Hash

Inherits:
Object show all
Defined in:
lib/savon/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#map_soap_responseObject

Maps keys and values of a Hash created from SOAP response XML to more convenient Ruby Objects.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/savon/core_ext/hash.rb', line 32

def map_soap_response
  inject({}) do |hash, (key, value)|
    key = key.strip_namespace.snakecase.to_sym

    value = case value
      when Hash
        value["xsi:nil"] ? nil : value.map_soap_response
      when Array
        value.map { |a_value| a_value.map_soap_response rescue a_value }
      when String
        value.map_soap_response
    end
    hash.merge key => value
  end
end

#to_soap_fault_messageObject

Tries to generate a SOAP fault message from the Hash. Returns nil in case no SOAP fault could be found or generated.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/savon/core_ext/hash.rb', line 17

def to_soap_fault_message
  soap_fault = self["soap:Envelope"]["soap:Body"]["soap:Fault"] rescue {}
  return nil unless soap_fault

  if soap_fault.keys.include? "faultcode"
    "(#{soap_fault['faultcode']}) #{soap_fault['faultstring']}"
  elsif soap_fault.keys.include? "code"
    "(#{soap_fault['code']['value']}) #{soap_fault['reason']['text']}"
  else
    nil
  end
end

#to_soap_xmlObject

Returns the Hash translated into SOAP request compatible XML.

Example

{ :find_user => { :id => 666 } }.to_soap_xml
=> "<findUser><id>666</id></findUser>"


9
10
11
12
13
# File 'lib/savon/core_ext/hash.rb', line 9

def to_soap_xml
  @soap_xml = Builder::XmlMarkup.new
  each { |key, value| nested_data_to_soap_xml key, value }
  @soap_xml.target!
end