Module: Savon::CoreExt::Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_merge!(other_hash) ⇒ Object

Returns a new Hash with self and other_hash merged recursively. Modifies the receiver in place.



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

def deep_merge!(other_hash)
  other_hash.each_pair do |k,v|
    tv = self[k]
    self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
  end
  self
end

#find_soap_bodyObject

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



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

def find_soap_body
  find_soap_element /.+:Body/
end

#find_soap_headerObject

Returns the values from the soap:Header element or an empty Hash in case the element could not be found.



23
24
25
# File 'lib/savon/core_ext/hash.rb', line 23

def find_soap_header
  find_soap_element /.+:Header/
end

#map_soap_responseObject

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/savon/core_ext/hash.rb', line 34

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