Module: Europe::Vat::Batch

Defined in:
lib/europe/vat/batch.rb

Overview

Batch VAT validation via VIES REST API

Constant Summary collapse

BATCH_API_URL =
'https://ec.europa.eu/taxation_customs/vies/rest-api/vat-validation'
BATCH_REPORT_URL =
'https://ec.europa.eu/taxation_customs/vies/rest-api/vat-validation-report'
BATCH_ERRORS =
{
  'VOW-ERR-6000' => :invalid_file_extension,
  'VOW-ERR-6001' => :too_many_rows,
  'VOW-ERR-6002' => :too_few_rows,
  'VOW-ERR-6005' => :filter_violation,
  'VOW-ERR-6006' => :token_not_found,
  'VOW-ERR-6007' => :invalid_file_structure,
  'VOW-ERR-6008' => :invalid_file_structure,
  'VOW-ERR-6011' => :file_too_large
}.freeze
BATCH_MIN_ROWS =
3
BATCH_MAX_ROWS =
100
HEADERS =
{
  'Content-Type' => 'application/json',
  'Accept' => 'application/json'
}.freeze

Class Method Summary collapse

Class Method Details

.result(token) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/europe/vat/batch.rb', line 60

def result(token)
  with_error_handling do
    response = http_get("#{BATCH_REPORT_URL}/#{token}")
    return handle_error(response) unless response.is_a?(Net::HTTPSuccess)

    error = check_json_error(response)
    return error if error

    { data: response.body, content_type: response['Content-Type'], filename: extract_filename(response) }
  end
end

.status(token) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/europe/vat/batch.rb', line 47

def status(token)
  with_error_handling do
    body = parse_json_response(http_get("#{BATCH_API_URL}/#{token}", HEADERS))
    return body if body.is_a?(Symbol)

    {
      token: body['token'], filename: body['filename'],
      status: body['status']&.downcase&.to_sym, percentage: body['percentage'],
      created_at: body['creationDateTime'], completed_at: body['completionTime']
    }
  end
end

.validate(vat_numbers, requester: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/europe/vat/batch.rb', line 35

def validate(vat_numbers, requester: nil)
  return :too_few_rows if vat_numbers.size < BATCH_MIN_ROWS
  return :too_many_rows if vat_numbers.size > BATCH_MAX_ROWS

  with_error_handling do
    body = parse_json_response(upload(generate_csv(vat_numbers, requester)))
    return body if body.is_a?(Symbol)

    { token: body['token'] }
  end
end