Sha256: 061f9910e8f91edb58a948d66d9e08834a02fe20473689bc4f0eed2d5a20e20c

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

require "builder"

require "savon"
require "savon/core_ext/object"
require "savon/core_ext/string"

module Savon
  module CoreExt
    module Hash

      # Returns the values from the soap:Body element or an empty Hash in case the soap:Body tag could
      # not be found.
      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

      # Maps keys and values of a Hash created from SOAP response XML to more convenient Ruby Objects.
      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

    end
  end
end

Hash.send :include, Savon::CoreExt::Hash

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
savon-0.8.1 lib/savon/core_ext/hash.rb
savon-0.8.0 lib/savon/core_ext/hash.rb