module SunDawg::CountryIsoTranslater

Constants

COUNTRIES
FILE

allows client application to override YAML hash

Public Class Methods

build_html_unicode(unicode_hex) click to toggle source
# 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) click to toggle source

O(1) find for iso 4217 currency information

# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source

O(N) translation from one convention standard to another

# 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