Class: Hash

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

Constant Summary collapse

InOrderMissing =

Error message for missing :@inorder elements.

"Missing elements in :@inorder %s"
InOrderSpurious =

Error message for spurious :@inorder elements.

"Spurious elements in :@inorder %s"

Instance Method Summary collapse

Instance Method Details

#find_soap_bodyObject

Returns the values from the ‘soap:Body’ element or an empty Hash in case the node could not be found.



11
12
13
14
15
# File 'lib/savon/core_ext/hash.rb', line 11

def find_soap_body
  envelope = self[self.keys.first] || {}
  body_key = envelope.keys.find { |key| /.+:Body/ =~ key } rescue nil
  body_key ? envelope[body_key].map_soap_response : {}
end

#map_soap_responseObject

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/savon/core_ext/hash.rb', line 37

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

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

#to_soap_xmlObject

Returns the Hash translated into SOAP request compatible XML.

To control the order of output, add a key of :@inorder with the value being an Array listing keys in order.

Examples

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

{ :find_user => { :name => "Lucy", :id => 666, :@inorder => [:id, :name] } }.to_soap_xml
=> "<findUser><id>666</id><name>Lucy</name></findUser>"


29
30
31
32
33
# File 'lib/savon/core_ext/hash.rb', line 29

def to_soap_xml
  @soap_xml = Builder::XmlMarkup.new
  inorder(self).each { |key| nested_data_to_soap_xml key, self[key] }
  @soap_xml.target!
end