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", "chr", "cs", "cy", "da", "de", "de-AT", "de-CH",
  "dsb", "el", "el-polyton", "en", "en-001", "en-AU", "en-CA", "en-ID",
  "en-MV", "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", "gd", "gl", "gu", "he",
  "hi", "hi-Latn", "hr", "hsb", "hu", "hy", "id", "ig", "is", "it", "ja",
  "ka", "kk", "km", "ko", "ko-KP", "kok", "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", "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", "yue-Hans", "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.



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

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.



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

def country_code
  @country_code
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



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

def currency_code
  @currency_code
end

#localeObject (readonly)

Returns the value of attribute locale.



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

def locale
  @locale
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#numeric_codeObject (readonly)

Returns the value of attribute numeric_code.



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

def numeric_code
  @numeric_code
end

#three_letter_codeObject (readonly)

Returns the value of attribute three_letter_code.



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

def three_letter_code
  @three_letter_code
end

Class Method Details

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



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

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



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

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



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

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”].



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

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

#to_sObject

Gets the string representation of the Country.



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

def to_s
  @country_code
end