Module: SunDawg::CountryIsoTranslater

Defined in:
lib/country_iso_translater.rb

Defined Under Namespace

Classes: NoCountryError, NoCurrencyError

Constant Summary collapse

FILE =

allows client application to override YAML hash

File.expand_path(File.join(File.dirname(__FILE__), 'countries.yml'))
COUNTRIES =
YAML.load_file(FILE)

Class Method Summary collapse

Class Method Details

.build_html_unicode(unicode_hex) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/country_iso_translater.rb', line 54

def self.build_html_unicode(unicode_hex)
  s = ""
  if unicode_hex.class == Fixnum
    s = "&#x#{unicode_hex.to_s(16)}"
  elsif unicode_hex.class == Array
    unicode_hex.each { |i|
      s += "&#x#{i.to_s(16)}"
    }
  end 
  s
end

.get_iso4217_currency_by_iso3166_alpha2(code) ⇒ Object

O(1) find for iso 4217 currency information

Raises:



47
48
49
50
51
52
# File 'lib/country_iso_translater.rb', line 47

def self.get_iso4217_currency_by_iso3166_alpha2(code)
  country = COUNTRIES[code]
  raise NoCountryError.new("[#{code}] IS NOT VALID") if country.nil?
  raise NoCurrencyError.new("[#{code}] HAS NO ISO4217 CURRENCY") if country["currency_iso4217"].nil?
  country["currency_iso4217"]
end

.translate_iso3166_alpha2_to_alpha3(code) ⇒ Object



23
24
25
# File 'lib/country_iso_translater.rb', line 23

def self.translate_iso3166_alpha2_to_alpha3(code)
  translate_standard(code, "alpha2", "alpha3")
end

.translate_iso3166_alpha2_to_name(code) ⇒ Object

O(1) translation of iso3166 2-digit code to name

Raises:



40
41
42
43
44
# File 'lib/country_iso_translater.rb', line 40

def self.translate_iso3166_alpha2_to_name(code)
  country = COUNTRIES[code]
  raise NoCountryError.new("[#{code}] IS NOT VALID") if country.nil?
  country["name"]  
end

.translate_iso3166_alpha3_to_name(code) ⇒ Object



27
28
29
# File 'lib/country_iso_translater.rb', line 27

def self.translate_iso3166_alpha3_to_name(code)
  translate_standard(code, "alpha3", "name")
end

.translate_iso3166_name_to_alpha2(name) ⇒ Object

O(N) translation from iso3166 name to 2-digit code



15
16
17
# File 'lib/country_iso_translater.rb', line 15

def self.translate_iso3166_name_to_alpha2(name)
  translate_standard(name, "name", "alpha2")
end

.translate_iso3166_name_to_alpha3(name) ⇒ Object



19
20
21
# File 'lib/country_iso_translater.rb', line 19

def self.translate_iso3166_name_to_alpha3(name)
  translate_standard(name, "name", "alpha3")
end

.translate_standard(s, from_standard, to_standard) ⇒ Object

O(N) translation from one convention standard to another

Raises:



32
33
34
35
36
37
# File 'lib/country_iso_translater.rb', line 32

def self.translate_standard(s, from_standard, to_standard)
  COUNTRIES.each_pair { |key, value| 
    return value[to_standard] if value[from_standard] == s 
  }
  raise NoCountryError.new("[#{s}] IS NOT VALID")
end