ItuCodes Build Status

Helper library for telephone country codes based on the official International Telecommunications Union specifications:

Country code lookup based on official ISO-3166-1 specifications:

Usage:

ItuCodes.valid_code? '8392813'
# => false

ItuCodes.valid_code? '7'
# => true

ItuCodes.parse_code '1818'
# => 1

ItuCodes.parse_code '822'
# => "82"

ItuCodes.parse_code '4'
# => nil

ItuCodes.find_by_name 'France'
# => "33"

ItuCodes.find_by_itu_code '995'
# => "Georgia"

ItuCodes.find_by_name 'Erewhon'
# => nil

ItuCodes.find_by_itu_code '123123995'
# => nil

# Despite the same 1st digit,
# these are for different
# countries ...
ItuCodes.compatriots? '822', '811'
# => false

# ... but these are for
# the same country ...
ItuCodes.compatriots? '1984', '1985'
# => true

# ... and then there's the
# North American 'situation' ...
ItuCodes.compatriots? '1264', '1818'
# => false

# =====================================================
# = Using 2-letter ISO country codes is most reliable =
# =====================================================

# Convert from and to ISO 2-letter country codes:
ItuCodes.iso2itu('US')
# => "1"

# Convert from and to ISO 2-letter country codes:
ItuCodes.itu2iso('1')
# => ["AS", "AI", "AG", "BS", "BB", "BM", "VG", "CA", "KY", "DM", "DO", "GD", "GU", "JM", "MS", "MP", "PR", "KN", "LC", "VC", "SX", "TT", "TC", "US", "VI"]

# Mexico
# ISO 3361 code : MX
# ITU code      : 52
ItuCodes.iso2itu('MX')
# => "52"

ItuCodes.itu2iso('52')
# => "MX"