Sha256: d7fb29069814cbab7b8dae47dae003a7d9efb65d94c88faaa9828425925f841e

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require "gyoku"

module Savon
  class QualifiedMessage

    def initialize(types, used_namespaces, key_converter)
      @types = types
      @used_namespaces = used_namespaces
      @key_converter = key_converter
    end

    def to_hash(hash, path)
      return unless hash
      return hash.map { |value| add_namespaces_to_body(value, path) } if hash.kind_of?(Array)
      return hash.to_s unless hash.kind_of? Hash

      hash.inject({}) do |newhash, (key, value)|
        camelcased_key = Gyoku.xml_tag(key, :key_converter => @key_converter).to_s
        newpath = path + [camelcased_key]

        if @used_namespaces[newpath]
          newhash.merge(
            "#{@used_namespaces[newpath]}:#{camelcased_key}" =>
              add_namespaces_to_body(value, @types[newpath] ? [@types[newpath]] : newpath)
          )
        else
          add_namespaces_to_values(value, path) if key == :order!
          newhash.merge(key => value)
        end
      end
    end

    private

    def add_namespaces_to_body(value, path)
      QualifiedMessage.new(@types, @used_namespaces, @key_converter).to_hash(value, path)
    end

    def add_namespaces_to_values(values, path)
      values.collect! { |value|
        camelcased_value = Gyoku.xml_tag(value, :key_converter => @key_converter)
        namespace_path = path + [camelcased_value.to_s]
        namespace = @used_namespaces[namespace_path]
        "#{namespace.blank? ? '' : namespace + ":"}#{camelcased_value}"
      }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
savon-2.0.0 lib/savon/qualified_message.rb