class Phony::CountryCodes

Handles determining the correct national code handler.

Attributes

countries[R]
international_absolute_format[RW]
international_relative_format[RW]
national_format[RW]

Public Class Methods

instance() click to toggle source

Singleton instance.

# File lib/phony/country_codes.rb, line 14
def self.instance
  @instance ||= new
end

Public Instance Methods

[](cc) click to toggle source

Get the Country object for the given CC.

# File lib/phony/country_codes.rb, line 32
def [](cc)
  countries[cc.size][cc]
end
add(country_code, country) click to toggle source

Add the given country to the mapping under the given country code.

# File lib/phony/country_codes.rb, line 21
def add(country_code, country)
  country_code = country_code.to_s
  optimized_country_code_access = country_code.size

  @countries ||= {}
  @countries[optimized_country_code_access] ||= {}
  @countries[optimized_country_code_access][country_code] = country
end
clean(number) click to toggle source

Clean number of all non-numeric characters, initial zeros or (0 and return it.

# File lib/phony/country_codes.rb, line 41
def clean(number)
  clean! number && number.dup
end
clean!(number) click to toggle source

Clean number of all non-numeric characters, initial zeros or (0 and return a copy.

# File lib/phony/country_codes.rb, line 47
def clean!(number)
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
end
format(number, options = {}) click to toggle source

Format the number.

# File lib/phony/country_codes.rb, line 88
def format(number, options = {})
  country, _, national_number = partial_split number
  country.format national_number, options
end
Also aliased as: formatted
formatted(number, options = {})
Alias for: format
normalize(number, options = {}) click to toggle source

00 for the standard international call prefix. en.wikipedia.org/wiki/List_of_international_call_prefixes

We can’t know from what country that person was calling, so we can’t remove the intl’ call prefix.

We remove:

* 0 or 00 at the very beginning.
* (0) anywhere.
* Non-digits.
# File lib/phony/country_codes.rb, line 62
def normalize(number, options = {})
  country = if (cc = options[:cc])
              self[cc]
            else
              clean! number
              country, cc, number = partial_split number
              country
            end
  number = country.normalize number, cc: cc
  countrify! number, cc
end
plausible?(number, hints = {}) click to toggle source

Is this number plausible?

# File lib/phony/country_codes.rb, line 96
def plausible?(number, hints = {})
  normalized = clean number

  # False if it fails the basic check.
  #
  return false unless (4..16).include?(normalized.size) # unless hints[:check_length] == false

  country, cc, rest = partial_split normalized

  # Was a country calling code given?
  #
  if (ccc = hints[:ccc])
    cc, ndc, *local = split ccc

    raise ArgumentError.new("The provided ccc option is too long and includes more than a cc ('#{cc}') and ndc ('#{ndc}'). It also includes '#{local.join}'.") unless local.size == 1 && local[0].empty?

    hints[:cc] = cc
    hints[:ndc] = ndc
  end

  # Country code plausible?
  #
  cc_needed = hints[:cc]
  return false if cc_needed && !(cc_needed === cc)

  # Country specific tests.
  #
  country.plausible? rest, hints
rescue ArgumentError
  raise
rescue StandardError
  false
end
split(number) click to toggle source

Splits this number into cc, ndc and locally split number parts.

# File lib/phony/country_codes.rb, line 76
def split(number)
  # Split the number into country, cc, and national part.
  country, cc, national_number = partial_split number

  # Split the national number into ndc and local part.
  _, ndc, *local = country.split national_number

  [cc, ndc, *local]
end
vanity?(number) click to toggle source

Is the given number a vanity number?

# File lib/phony/country_codes.rb, line 132
def vanity?(number)
  country, _, national = partial_split number
  country.vanity? national
end
vanity_to_number(vanity_number) click to toggle source

Converts a vanity number into a normalized E164 number.

# File lib/phony/country_codes.rb, line 139
def vanity_to_number(vanity_number)
  country, cc, national = partial_split vanity_number
  "#{cc}#{country.vanity_to_number(national)}"
end

Private Instance Methods

countrify(number, cc) click to toggle source

Adds the country code to the front if it does not already start with it.

Note: This won’t be correct in some cases, but it is the best we can do.

# File lib/phony/country_codes.rb, line 170
def countrify(number, cc)
  countrify!(number, cc) || number
end
countrify!(number, cc) click to toggle source
# File lib/phony/country_codes.rb, line 174
def countrify!(number, cc)
  number.sub!(/\A/, cc) # @countrify_regex, @cc
end
country_for(number) click to toggle source

Return a country for the number.

# File lib/phony/country_codes.rb, line 148
def country_for(number)
  country, = partial_split number
  country
end
partial_split(number) click to toggle source

Split off the country and the cc, and also return the national number part.

# File lib/phony/country_codes.rb, line 155
def partial_split(number)
  cc = +''
  1.upto(3) do |i|
    cc << number.slice!(0..0)
    country = countries[i][cc]
    return [country, cc, number] if country
  end
  # This line is never reached as CCs are in prefix code.
end