Module: Fortnox::Mappers::CountryCode

Defined in:
lib/fortnox/mappers/country_code.rb

Class Method Summary collapse

Class Method Details

.parse(country) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fortnox/mappers/country_code.rb', line 10

def self.parse(country)
  return '' if country.nil? || country == ''

  # Fortnox only supports Swedish translation of Sweden
  return 'SE' if country =~ /^s(e$|we|ve)/i

  country = ::ISO3166::Country[country] ||
            ::ISO3166::Country.find_country_by_iso_short_name(country) ||
            ::ISO3166::Country.find_country_by_translated_names(country)

  raise Fortnox::AttributeError, '"Country" violates constraints' if country.nil?

  country.alpha2
end

.serialise(country_code) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/fortnox/mappers/country_code.rb', line 25

def self.serialise(country_code)
  return '' if country_code.nil? || country_code == ''

  return 'Sverige' if country_code == 'SE'

  ::ISO3166::Country.new(country_code).translations['en']
end