Module: Europe::Vat::Format

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

Overview

Format

Constant Summary collapse

VAT_REGEX =
{
  AT: /^ATU\d{8}$/,
  BE: /^BE[0-1]\d{9}$/,
  BG: /^BG(\d{10}|\d{9})$/,
  CY: /^CY\d{8}[A-Z]$/,
  CZ: /^CZ(\d{8}|\d{9}|\d{10})$/,
  DE: /^DE\d{9}$/,
  DK: /^DK\d{2}\d{2}\d{2}\d{2}$/,
  EE: /^EE\d{9}$/,
  EL: /^EL\d{9}$/,
  ES: /^ES([A-Z0-9]\d{7}[A-Z0-9])$/,
  FI: /^FI\d{8}$/,
  FR: /^FR[A-Z0-9][A-Z0-9]\d{9}$/,
  HR: /^HR\d{11}$/,
  HU: /^HU\d{8}$/,
  IE: /^IE(\d[A-Z]\d{5}[A-Z]|\d{7}[A-Z]{2})$/,
  IT: /^IT\d{11}$/,
  LT: /^LT(\d{9}|\d{12})$/,
  LU: /^LU\d{8}$/,
  LV: /^LV\d{11}$/,
  MT: /^MT\d{8}$/,
  NL: /^NL\d{9}B\d\d$/,
  PL: /^PL\d{10}$/,
  PT: /^PT\d{9}$/,
  RO: /^RO\d{2,10}$/,
  SE: /^SE\d{12}$/,
  SI: /^SI\d{8}$/,
  SK: /^SK\d{10}$/
}.freeze

Class Method Summary collapse

Class Method Details

.match_vat_number(number, country_code) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/europe/vat/format.rb', line 51

def self.match_vat_number(number, country_code)
  if VAT_REGEX[country_code.to_sym].is_a?(Array)
    VAT_REGEX[country_code.to_sym].each do |regex|
      return true if regex.match(number)
    end
  elsif VAT_REGEX[country_code.to_sym].match(number)
    return true
  end
  false
end

.sanitize_number(number) ⇒ Object



47
48
49
# File 'lib/europe/vat/format.rb', line 47

def self.sanitize_number(number)
  number.gsub(/\.|\t|\s/, '').upcase
end

.validate(number) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/europe/vat/format.rb', line 39

def self.validate(number)
  country_code = number[0..1].to_sym
  number = sanitize_number(number)
  return false unless VAT_REGEX.key?(country_code)

  match_vat_number(number, country_code)
end