Module: Kountries::America

Defined in:
lib/kountries/america.rb

Overview

America

Defined Under Namespace

Classes: AG, AI, AR, AW, BB, BL, BM, BO, BQ, BR, BS, BZ, CA, CL, CO, CR, CU, CW, DM, DO, EC, FK, GD, GF, GL, GP, GS, GT, GY, HN, HT, JM, KN, KY, LC, MF, MQ, MS, MX, NI, PA, PE, PM, PR, PY, SR, SV, SX, TC, TT, UM, US, UY, VC, VE, VG, VI

Class Method Summary collapse

Class Method Details

.as_struct(american_country_class) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/kountries/america.rb', line 25

def self.as_struct(american_country_class)
  OpenStruct.new(
    alpha2: american_country_class::ALPHA_2,
    alpha3: american_country_class::ALPHA_3,
    country_code: american_country_class::COUNTRY_CODE,
    name: american_country_class::NAME,
    unofficial_names: american_country_class::UN_OFFICIAL_NAMES
  )
end

.empty_structObject



35
36
37
38
39
40
41
42
43
# File 'lib/kountries/america.rb', line 35

def self.empty_struct
  OpenStruct.new(
    alpha2: nil,
    alpha3: nil,
    country_code: nil,
    name: nil,
    unofficial_names: nil
  )
end

.find_country_by_code(code) ⇒ Object



18
19
20
21
22
23
# File 'lib/kountries/america.rb', line 18

def self.find_country_by_code(code)
  return empty_struct unless Kountries::America.constants.include?(code.intern)

  american_country_class = Module.const_get "Kountries::America::#{code}"
  return as_struct(american_country_class) if american_country_class::ALPHA_2 == code
end

.find_country_by_name(name) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/kountries/america.rb', line 8

def self.find_country_by_name(name)
  constants.each do |country|
    american_country_class = Module.const_get "Kountries::America::#{country}"
    return as_struct(american_country_class) if american_country_class::NAME.upcase == name.upcase
    return as_struct(american_country_class) if american_country_class::UN_OFFICIAL_NAMES.include?(name.capitalize)
  end

  empty_struct
end