Sha256: 0de9acb341645a1f5ba6c8ecaed453f37b93b68a089597d61e389e7f4f245600

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module CorreiosSigep
  module LogisticReverse
    class BaseClient
      DEFAULT_TIMEOUT = 30

      def initialize
        timeout     = CorreiosSigep.configuration.timeout || DEFAULT_TIMEOUT
        user        = CorreiosSigep.configuration.user
        password    = CorreiosSigep.configuration.password  

        options = {
          adapter: :net_http_persistent,
          proxy: CorreiosSigep.configuration.proxy,
          wsdl: wsdl,
          open_timeout: timeout,
          read_timeout: timeout,
          basic_auth: [user, password]
        }
        options.delete(:proxy) unless options[:proxy]

        options.merge!({ headers: { 'SOAPAction' => '' }}) if ENV['GEM_ENV'] == 'test'

        @client = Savon.client(options)
      end

      def wsdl
        @wsdl ||= if ENV['GEM_ENV'] == 'test'
                    'https://apphom.correios.com.br/logisticaReversaWS/logisticaReversaService/logisticaReversaWS?wsdl'
                  else
                    'https://cws.correios.com.br/logisticaReversaWS/logisticaReversaService/logisticaReversaWS?wsdl'
                  end
      end

      def invoke(method, message)
        @client.instance_variable_set(
          :@wsdl,
          Wasabi::Document.new(CorreiosSigep.configuration.wsdl_base_url)
        ) if wsdl_base_url_changed?

        @client.call(method, message: message)
      end

      private
      def wsdl_base_url_changed?
        wsdl_base_url && wsdl_base_url != @client.instance_variable_get(:@wsdl).document
      end

      def wsdl_base_url
        CorreiosSigep.configuration.wsdl_base_url
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
correios_sigep-1.0.0 lib/correios_sigep/logistic_reverse/base_client.rb