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 46
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 87
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 61
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
  countrify! number, cc
end
plausible?(number, hints = {}) click to toggle source

Is this number plausible?

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

  # False if it fails the basic check.
  #
  return false unless (4..15) === normalized.size

  country, cc, rest = partial_split normalized

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

  # Country specific tests.
  #
  country.plausible? rest, hints
rescue StandardError
  return 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 75
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.
  trunk, 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 118
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 124
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 155
def countrify number, cc
  countrify!(number, cc) || number
end
countrify!(number, cc) click to toggle source
# File lib/phony/country_codes.rb, line 158
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 133
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 140
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