module Europe::Vat
VAT
VAT
Constants
- BODY
- HEADERS
- WSDL
Public Class Methods
Source
# File lib/europe/vat/vat.rb, line 78 def self.charge_vat?(origin_country, number) return false if number.nil? || number.empty? Europe::Vat::Fromat::VAT_REGEX.key?(origin_country.to_sym) || Europe::Vat::Fromat::VAT_REGEX.key?(number[0..1].to_sym) end
Source
# File lib/europe/vat/vat.rb, line 68 def self.convert_date(date) return unless date Date.parse(date) end
Source
# File lib/europe/vat/vat.rb, line 74 def self.extract_data(body, position) body[position]&.text end
Source
# File lib/europe/vat/vat.rb, line 63 def self.response_xml(response) xml = REXML::Document.new(response.body) xml.elements.first.elements[2].elements[1] end
Source
# File lib/europe/vat/vat.rb, line 85 def self.send_request(country_code, number) uri = URI.parse(WSDL) body = BODY.dup.gsub('{COUNTRY_CODE}', country_code) body = body.gsub('{NUMBER}', number) # Create the HTTP objects http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri.request_uri, HEADERS) request.body = body # Send the request http.request(request) end
Source
# File lib/europe/vat/vat.rb, line 51 def self.setup_response(response) body = response_xml(response) { valid: extract_data(body, 3) == 'true', country_code: extract_data(body, 0), vat_number: extract_data(body, 1), request_date: convert_date(extract_data(body, 2)), name: extract_data(body, 4), address: extract_data(body, 5) } end
Source
# File lib/europe/vat/vat.rb, line 35 def self.validate(number) return :failed if number.size < 4 response = send_request(number[0..1], number[2..-1]) return :failed unless response.is_a? Net::HTTPSuccess return :failed if response.body.include?('soap:Fault') return :timeout if response.body.include?('TIMEOUT') return :timeout if response.body.include?('MS_MAX_CONCURRENT_REQ') setup_response(response) rescue Net::OpenTimeout :timeout rescue Net::HTTPServerError :server_error end