Class: Addressing::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/addressing/country.rb

Constant Summary collapse

@@definitions =
{}
@@available_locales =
[
  "af", "am", "ar", "ar-LY", "ar-SA", "as", "az", "be", "bg", "bn",
  "bn-IN", "bs", "ca", "cs", "cy", "da", "de", "de-AT", "de-CH", "el",
  "en", "en-001", "en-CA", "en-GB", "es", "es-419", "es-AR",
  "es-BO", "es-CL", "es-CO", "es-CR", "es-DO", "es-EC", "es-GT", "es-HN",
  "es-MX", "es-NI", "es-PA", "es-PE", "es-PR", "es-PY", "es-SV", "es-US",
  "es-VE", "et", "eu", "fa", "fa-AF", "fi", "fil", "fr", "fr-BE", "fr-CA",
  "ga", "gl", "gu", "he", "hi", "hr", "hu", "hy", "id", "is", "it", "ja",
  "ka", "kk", "km", "ko", "ko-KP", "ky", "lo", "lt", "lv", "mk", "ml",
  "mn", "mr", "ms", "my", "ne", "nl", "nn", "no", "or", "pa", "pl", "ps",
  "ps-PK", "pt", "pt-PT", "ro", "ro-MD", "ru", "ru-UA", "sd", "si", "sk",
  "sl", "so", "sq", "sr", "sr-Cyrl-BA", "sr-Cyrl-ME", "sr-Cyrl-XK",
  "sr-Latn", "sr-Latn-BA", "sr-Latn-ME", "sr-Latn-XK", "sv", "sw",
  "sw-CD", "sw-KE", "ta", "te", "th", "tk", "tr", "uk", "ur", "ur-IN",
  "uz", "vi", "yue", "zh", "zh-Hant", "zh-Hant-HK", "zu"
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition = {}) ⇒ Country

Returns a new instance of Country.



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/addressing/country.rb', line 349

def initialize(definition = {})
  # Validate the presence of required properties.
  [:country_code, :name, :locale].each do |required_property|
    if definition[required_property].nil?
      raise ArgumentError, "Missing required property #{required_property}."
    end
  end

  @country_code = definition[:country_code]
  @name = definition[:name]
  @three_letter_code = definition[:three_letter_code]
  @numeric_code = definition[:numeric_code]
  @currency_code = definition[:currency_code]
  @locale = definition[:locale]
end

Instance Attribute Details

#country_codeObject (readonly)

Returns the value of attribute country_code.



347
348
349
# File 'lib/addressing/country.rb', line 347

def country_code
  @country_code
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



347
348
349
# File 'lib/addressing/country.rb', line 347

def currency_code
  @currency_code
end

#localeObject (readonly)

Returns the value of attribute locale.



347
348
349
# File 'lib/addressing/country.rb', line 347

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



347
348
349
# File 'lib/addressing/country.rb', line 347

def name
  @name
end

#numeric_codeObject (readonly)

Returns the value of attribute numeric_code.



347
348
349
# File 'lib/addressing/country.rb', line 347

def numeric_code
  @numeric_code
end

#three_letter_codeObject (readonly)

Returns the value of attribute three_letter_code.



347
348
349
# File 'lib/addressing/country.rb', line 347

def three_letter_code
  @three_letter_code
end

Class Method Details

.all(locale = "en", fallback_locale = "en") ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/addressing/country.rb', line 43

def all(locale = "en", fallback_locale = "en")
  locale = Locale.resolve(@@available_locales, locale, fallback_locale)
  definitions = load_definitions(locale)

  definitions.map do |country_code, country_name|
    country = new(
      country_code: country_code,
      name: country_name,
      three_letter_code: base_definitions[country_code][0],
      numeric_code: base_definitions[country_code][1],
      currency_code: base_definitions[country_code][2],
      locale: locale
    )

    [country_code, country]
  end.to_h
end

.get(country_code, locale = "en", fallback_locale = "en") ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/addressing/country.rb', line 25

def get(country_code, locale = "en", fallback_locale = "en")
  country_code = country_code.upcase

  raise UnknownCountryError, country_code unless base_definitions.key?(country_code)

  locale = Locale.resolve(@@available_locales, locale, fallback_locale)
  definitions = load_definitions(locale)

  new(
    country_code: country_code,
    name: definitions[country_code],
    three_letter_code: base_definitions[country_code][0],
    numeric_code: base_definitions[country_code][1],
    currency_code: base_definitions[country_code][2],
    locale: locale
  )
end

.list(locale = "en", fallback_locale = "en") ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/addressing/country.rb', line 61

def list(locale = "en", fallback_locale = "en")
  locale = Locale.resolve(@@available_locales, locale, fallback_locale)
  definitions = load_definitions(locale)

  definitions.map do |country_code, country_name|
    [country_code, country_name]
  end.to_h
end

Instance Method Details

#timezonesObject

Gets the timezones.

Note that a country can span more than one timezone. For example, Germany has ['Europe/Berlin', 'Europe/Busingen'].



369
370
371
# File 'lib/addressing/country.rb', line 369

def timezones
  @timezones ||= TZInfo::Country.get(@country_code).zone_identifiers
end

#to_sObject

Gets the string representation of the Country.



374
375
376
# File 'lib/addressing/country.rb', line 374

def to_s
  @country_code
end