Module: Savon::CoreExt::Hash

Defined in:
lib/savon/core_ext/hash.rb

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 soap:Body tag could not be found.



13
14
15
16
17
# File 'lib/savon/core_ext/hash.rb', line 13

def find_soap_body
  envelope = 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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/savon/core_ext/hash.rb', line 20

def map_soap_response
  inject({}) do |hash, (key, value)|
    value = case value
      when ::Hash   then value["xsi:nil"] ? nil : value.map_soap_response
      when ::Array  then value.map { |val| val.map_soap_response rescue val }
      when ::String then value.map_soap_response
    end
    
    new_key = if Savon.strip_namespaces?
      key.strip_namespace.snakecase.to_sym
    else
      key.snakecase
    end
    
    if hash[new_key] # key already exists, value should be added as an Array
      hash[new_key] = [hash[new_key], value].flatten
      result = hash
    else
      result = hash.merge new_key => value
    end
    result
  end
end