Class: Phony::CountryCodes

Inherits:
Object
  • Object
show all
Defined in:
lib/phony/country_codes.rb

Overview

Handles determining the correct national code handler.

Constant Summary collapse

@@basic_cleaning_pattern =

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

/\A00?|\(0|\D/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countriesObject (readonly)

Returns the value of attribute countries


9
10
11
# File 'lib/phony/country_codes.rb', line 9

def countries
  @countries
end

#international_absolute_formatObject

Returns the value of attribute international_absolute_format


10
11
12
# File 'lib/phony/country_codes.rb', line 10

def international_absolute_format
  @international_absolute_format
end

#international_relative_formatObject

Returns the value of attribute international_relative_format


10
11
12
# File 'lib/phony/country_codes.rb', line 10

def international_relative_format
  @international_relative_format
end

#national_formatObject

Returns the value of attribute national_format


10
11
12
# File 'lib/phony/country_codes.rb', line 10

def national_format
  @national_format
end

Class Method Details

.instanceObject

Singleton instance.


14
15
16
# File 'lib/phony/country_codes.rb', line 14

def self.instance
  @instance ||= new
end

Instance Method Details

#[](cc) ⇒ Object

Get the Country object for the given CC.


32
33
34
# File 'lib/phony/country_codes.rb', line 32

def [] cc
  countries[cc.size][cc]
end

#add(country_code, country) ⇒ Object

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


21
22
23
24
25
26
27
28
# 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) ⇒ Object

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


41
42
43
# File 'lib/phony/country_codes.rb', line 41

def clean number
  clean! number && number.dup
end

#clean!(number) ⇒ Object

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


46
47
48
# File 'lib/phony/country_codes.rb', line 46

def clean! number
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
end

#format(number, options = {}) ⇒ Object Also known as: formatted

Format the number.


87
88
89
90
# File 'lib/phony/country_codes.rb', line 87

def format number, options = {}
  country, _, national_number = partial_split number
  country.format national_number, options
end

#normalize(number, options = {}) ⇒ Object

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.

61
62
63
64
65
66
67
68
69
70
71
# 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 = {}) ⇒ Boolean

Is this number plausible?

Returns:

  • (Boolean)

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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..16) === 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) ⇒ Object

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


75
76
77
78
79
80
81
82
83
# 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.
  _, ndc, *local = country.split national_number
  
  [cc, ndc, *local]
end

#vanity?(number) ⇒ Boolean

Is the given number a vanity number?

Returns:

  • (Boolean)

118
119
120
121
# 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) ⇒ Object

Converts a vanity number into a normalized E164 number.


124
125
126
127
# 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