Sha256: 047832961884aa429628788345eeadf2efd6504bf451961005e44c0b4167e09f

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
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 hash unless hash
      return hash.map { |value| to_hash(value, path) } if hash.is_a?(Array)
      return hash.to_s unless hash.is_a?(Hash)

      hash.each_with_object({}) do |(key, value), newhash|
        case key
        when :order!
          newhash[key] = add_namespaces_to_values(value, path)
        when :attributes!, :content!
          newhash[key] = to_hash(value, path)
        else
          if key.to_s =~ /!$/
            newhash[key] = value
          else
            translated_key  = translate_tag(key)
            newkey          = add_namespaces_to_values(key, path).first
            newpath         = path + [translated_key]
            newhash[newkey] = to_hash(value, @types[newpath] ? [@types[newpath]] : newpath)
          end
        end
        newhash
      end
    end

    private

    def translate_tag(key)
      Gyoku.xml_tag(key, :key_converter => @key_converter).to_s
    end

    def add_namespaces_to_values(values, path)
      Array(values).collect do |value|
        translated_value = translate_tag(value)
        namespace_path   = path + [translated_value]
        namespace        = @used_namespaces[namespace_path] || ''
        namespace.empty? ? value : "#{namespace}:#{translated_value}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
savon-3.0.0.rc1 lib/savon/qualified_message.rb
savon-2.15.1 lib/savon/qualified_message.rb
savon-2.15.0 lib/savon/qualified_message.rb
savon-2.14.0 lib/savon/qualified_message.rb
savon-2.13.1 lib/savon/qualified_message.rb
savon-2.13.0 lib/savon/qualified_message.rb