Sha256: 3adc69255696c12fe9e44de4ee7b91c31d20cfe700f1155354b2bafca40a0546

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

require "builder"

require "savon/core_ext/object"
require "savon/core_ext/string"
require "savon/core_ext/hash"
require "savon/core_ext/datetime"

module Savon
  module CoreExt
    module Array

      # Translates the Array into SOAP compatible XML. See: Hash.to_soap_xml.
      def to_soap_xml(key, escape_xml = true, attributes = {})
        xml = Builder::XmlMarkup.new
        
        each_with_index do |item, index|
          attrs = tag_attributes attributes, index
          case item
            when ::Hash   then xml.tag!(key, attrs) { xml << item.to_soap_xml }
            when NilClass then xml.tag!(key, "xsi:nil" => "true")
            else               xml.tag!(key, attrs) { xml << (escape_xml ? item.to_soap_value : item.to_soap_value!) }
          end
        end
        
        xml.target!
      end

    private

      # Takes a Hash of +attributes+ and the +index+ for which to return attributes
      # for duplicate tags.
      def tag_attributes(attributes, index)
        return {} if attributes.empty?
        
        attributes.inject({}) do |hash, (key, value)|
          value = value[index] if value.kind_of? ::Array
          hash.merge key => value
        end
      end

    end
  end
end

Array.send :include, Savon::CoreExt::Array

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
savon-0.8.0.beta.4 lib/savon/core_ext/array.rb
savon-0.8.0.beta.3 lib/savon/core_ext/array.rb
savon-0.8.0.beta.2 lib/savon/core_ext/array.rb
savon-0.8.0.beta.1 lib/savon/core_ext/array.rb