Sha256: 4af7d6c8308d81edf5cf16b1751f121efd995046d569beff8fd7240d1081b694

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

require "httpi"
require "savon/soap/response"

module Savon
  module SOAP

    # = Savon::SOAP::Request
    #
    # Executes SOAP requests.
    class Request

      # Content-Types by SOAP version.
      ContentType = { 1 => "text/xml;charset=UTF-8", 2 => "application/soap+xml;charset=UTF-8" }

      # Expects an <tt>HTTPI::Request</tt> and a <tt>Savon::SOAP::XML</tt> object
      # to execute a SOAP request and returns the response.
      def self.execute(request, soap)
        new(request, soap).response
      end

      # Expects an <tt>HTTPI::Request</tt> and a <tt>Savon::SOAP::XML</tt> object.
      def initialize(request, soap)
        self.request = setup(request, soap)
      end

      # Accessor for the <tt>HTTPI::Request</tt>.
      attr_accessor :request

      # Executes the request and returns the response.
      def response
        @response ||= with_logging { HTTPI.post request }
      end

    private

      # Sets up the +request+ using a given +soap+ object.
      def setup(request, soap)
        url, body = soap.endpoint, soap.to_xml

        request.url = url
        request.body = body
        request.headers["Content-Type"] ||= ContentType[soap.version]
        request.headers["Content-Length"] ||= body.length.to_s

        request
      end

      # Logs the HTTP request, yields to a given +block+ and returns a <tt>Savon::SOAP::Response</tt>.
      def with_logging
        log_request request.url, request.headers, request.body
        response = yield
        log_response response.code, response.body
        SOAP::Response.new response
      end

      # Logs the SOAP request +url+, +headers+ and +body+.
      def log_request(url, headers, body)
        Savon.log "SOAP request: #{url}"
        Savon.log headers.map { |key, value| "#{key}: #{value}" }.join(", ")
        Savon.log body
      end

      # Logs the SOAP response +code+ and +body+.
      def log_response(code, body)
        Savon.log "SOAP response (status #{code}):"
        Savon.log body
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
they-savon-0.9.7.1 lib/savon/soap/request.rb
they-savon-0.9.7 lib/savon/soap/request.rb
savon-0.9.7 lib/savon/soap/request.rb