Class: Phony::CountryCodes
- Inherits:
-
Object
- Object
- Phony::CountryCodes
- 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
-
#countries ⇒ Object
readonly
Returns the value of attribute countries.
-
#international_absolute_format ⇒ Object
Returns the value of attribute international_absolute_format.
-
#international_relative_format ⇒ Object
Returns the value of attribute international_relative_format.
-
#national_format ⇒ Object
Returns the value of attribute national_format.
Class Method Summary collapse
-
.instance ⇒ Object
Singleton instance.
Instance Method Summary collapse
-
#[](cc) ⇒ Object
Get the Country object for the given CC.
-
#add(country_code, country) ⇒ Object
Add the given country to the mapping under the given country code.
-
#clean(number) ⇒ Object
Clean number of all non-numeric characters, initial zeros or (0 and return it..
-
#clean!(number) ⇒ Object
Clean number of all non-numeric characters, initial zeros or (0 and return a copy..
-
#format(number, options = {}) ⇒ Object
(also: #formatted)
Format the number.
-
#normalize(number, options = {}) ⇒ Object
00 for the standard international call prefix.
-
#plausible?(number, hints = {}) ⇒ Boolean
Is this number plausible?.
-
#split(number) ⇒ Object
Splits this number into cc, ndc and locally split number parts.
-
#vanity?(number) ⇒ Boolean
Is the given number a vanity number?.
-
#vanity_to_number(vanity_number) ⇒ Object
Converts a vanity number into a normalized E164 number.
Instance Attribute Details
#countries ⇒ Object (readonly)
Returns the value of attribute countries.
9 10 11 |
# File 'lib/phony/country_codes.rb', line 9 def countries @countries end |
#international_absolute_format ⇒ Object
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_format ⇒ Object
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_format ⇒ Object
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
.instance ⇒ Object
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.
47 48 49 |
# File 'lib/phony/country_codes.rb', line 47 def clean!(number) number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number end |
#format(number, options = {}) ⇒ Object Also known as: formatted
Format the number.
88 89 90 91 |
# File 'lib/phony/country_codes.rb', line 88 def format(number, = {}) country, _, national_number = partial_split number country.format national_number, 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.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/phony/country_codes.rb', line 62 def normalize(number, = {}) country = if (cc = [: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 = {}) ⇒ Boolean
Is this number plausible?
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# 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) ⇒ Object
Splits this number into cc, ndc and locally split number parts.
76 77 78 79 80 81 82 83 84 |
# 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) ⇒ Boolean
Is the given number a vanity number?
132 133 134 135 |
# 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) ⇒ Object
Converts a vanity number into a normalized E164 number.
139 140 141 142 |
# 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 |