Sha256: ca3f7ea5ba3d7541e1315815d8f0c03a6b2b8c6996a780cd24c7a77b2e1747dc

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'savon'

class Valvat
  class Lookup
    class Request
      VIES_WSDL_URL = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'

      def initialize(vat, options)
        @vat = Valvat(vat)
        @options = options || {}
        @requester = @options[:requester] && Valvat(@options[:requester])
      end

      def perform
        Response.new(
          client.call(action, message: message, message_tag: message_tag, soap_action: nil)
        )
      rescue Savon::SOAPFault, Savon::HTTPError, Savon::UnknownOperationError => e
        Fault.new(e)
      end

      private

      def client
        Savon::Client.new({
          wsdl: VIES_WSDL_URL, log: false, follow_redirects: true
        }.merge(@options[:savon] || {}))
      end

      def message
        add_requester({
                        country_code: @vat.vat_country_code,
                        vat_number: @vat.to_s_wo_country
                      })
      end

      def message_tag
        @requester ? :checkVatApprox : :checkVat
      end

      def action
        @requester ? :check_vat_approx : :check_vat
      end

      def add_requester(message)
        return message unless @requester

        message[:requester_country_code] = @requester.vat_country_code
        message[:requester_vat_number]   = @requester.to_s_wo_country

        message
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
valvat-1.1.5 lib/valvat/lookup/request.rb
valvat-1.1.4 lib/valvat/lookup/request.rb
valvat-1.1.3 lib/valvat/lookup/request.rb