Sha256: 6f82ce56d38393a51b661c733a5faf9a6c775c3caf5588070a4a1431dec8b05f

Contents?: true

Size: 752 Bytes

Versions: 1

Compression:

Stored size: 752 Bytes

Contents

class Valvat
  module Utils
    
    EU_COUNTRIES = %w(AT BE BG CY CZ DE DK EE ES FI FR GB GR HU IE IT LT LU LV MT NL PL PT RO SE SI SK)
    COUNTRY_PATTERN = /\A([A-Z]{2})(.+)\Z/
    NORMALIZE_PATTERN = /[-\.:_\s,;]+/
    
    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
    
    def self.normalize(vat)
      vat.upcase.gsub(NORMALIZE_PATTERN, "")
    end
    
    def self.vat_country_to_iso_country(vat_country)
      vat_country == "EL" ? "GR" : vat_country
    end
    
    def self.iso_country_to_vat_country(iso_country)
      iso_country == "GR" ? "EL" : iso_country
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
valvat-0.3.3 lib/valvat/utils.rb