Sha256: 43685be2ccb2ac7821f7927555bdd1e5068ed325989391a05b59a845fd603934

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

require "savon"

module DpdApi
  module Client
    class Response
      def initialize(url)
        @url    = url
        @client = Savon.client(wsdl: @url)
      end

      def response(method, params = {}, options = {})
        builder = ResourceBuilder.new(@client, method, params, options)
        builder.resources
      rescue Savon::SOAPFault => error
        { errors: error.to_s }
      end

      private

      class ResourceBuilder
        def initialize(client, method, params, options = {})
          @client  = client
          @method  = method
          @namespace = options.delete(:namespace)
          @merged_params = merge_auth_params(params)
        end

        def resources
          namespace = "#{@method}_response".to_sym
          resources = response.body[namespace][:return]
          resources.is_a?(Array) ? resources : [] << resources
        end

        private

        def merge_auth_params(params)
          auth_params = DpdApi.configuration.auth_params.clone
          auth_params.deep_merge!(params)
        end

        def response
          @client.call(@method, message: request)
        end

        def request
          namespace = @namespace || :request
          @merged_params.blank? ? @merged_params : { namespace => @merged_params  }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dpd_api-0.1.2 lib/dpd_api/client/response.rb
dpd_api-0.1.1 lib/dpd_api/client/response.rb
dpd_api-0.1.0 lib/dpd_api/client/response.rb