Module: Valvat::Utils

Defined in:
lib/valvat/utils.rb

Constant Summary collapse

EU_COUNTRIES =
%w(AT BE BG CY CZ DE DK EE ES FI FR GB GR HR HU IE IT LT LU LV MT NL PL PT RO SE SI SK)
COUNTRY_PATTERN =
/\A([A-Z]{2})(.+)\Z/
NORMALIZE_PATTERN =
/[[:space:][:punct:][:cntrl:]]+/

Class Method Summary collapse

Class Method Details

.iso_country_to_vat_country(iso_country) ⇒ Object


23
24
25
# File 'lib/valvat/utils.rb', line 23

def self.iso_country_to_vat_country(iso_country)
  iso_country == "GR" ? "EL" : iso_country
end

.normalize(vat) ⇒ Object


15
16
17
# File 'lib/valvat/utils.rb', line 15

def self.normalize(vat)
  vat.to_s.upcase.gsub(NORMALIZE_PATTERN, "")
end

.split(vat) ⇒ Object


8
9
10
11
12
13
# File 'lib/valvat/utils.rb', line 8

def self.split(vat)
  COUNTRY_PATTERN =~ vat
  result = [$1, $2]
  iso_country = vat_country_to_iso_country(result[0])
  EU_COUNTRIES.include?(iso_country) ? result : [nil, nil]
end

.vat_country_to_iso_country(vat_country) ⇒ Object


19
20
21
# File 'lib/valvat/utils.rb', line 19

def self.vat_country_to_iso_country(vat_country)
  vat_country == "EL" ? "GR" : vat_country
end