Sha256: 6d3ad1d8beedffa8ea9f127beb3c964246b89d1644ef2f77cda5359866d542b7

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

require "savon/multipart/soap/part"

module Savon
  module SOAP
    class Response

      # Overwrite to +decode_multipart+.
      def initialize(config, response)
        self.http = response
        decode_multipart
        raise_errors if config.raise_errors
      end

      def parts
        @parts || []
      end

      attr_writer :parts

      # Returns +true+ if this is a multipart response.
      def multipart?
        http.headers["Content-Type"] =~ /^multipart/
      end

      # Returns the boundary declaration of the multipart response.
      def boundary
        return unless multipart?
        @boundary ||= Mail::Field.new("Content-Type", http.headers["Content-Type"]).parameters['boundary']
      end

      # Returns the Array of attachments if it was a multipart response.
      def attachments
        parts.attachments
      end

      # Overwrite to work with multipart response.
      def to_xml
        if multipart?
          parts.first.body.decoded  # we just assume the first part is the XML
        else
          http.body
        end
      end

    private

      # Decoding multipart responses.
      #
      # <tt>response.to_xml</tt> will point to the first part, hopefully the SOAP part of the multipart.
      # All attachments are available in the <tt>response.parts</tt> Array. Each is a Part from the mail gem.
      # See the docs there for details but:
      #
      # * response.parts[0].body is the contents
      # * response.parts[0].headers are the mime headers
      #
      # And you can do nesting:
      #
      # * response.parts[0].parts[2].body
      def decode_multipart
        return unless multipart?
        part_of_parts = Part.new(:headers => http.headers, :body => http.body)
        part_of_parts.body.split!(boundary)
        self.parts = part_of_parts.parts
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
savon-multipart-1.2.0 lib/savon/multipart/soap/response.rb
savon-multipart-1.1.0 lib/savon/multipart/soap/response.rb
savon-multipart-1.0.0 lib/savon/multipart/soap/response.rb