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

Constructor Details

#initializeCountryCodes

Returns a new instance of CountryCodes


12
13
14
15
16
17
18
19
# File 'lib/phony/country_codes.rb', line 12

def initialize
  @international_absolute_format = '+%s%s%s%s%s'
  @international_relative_format = '00%s%s%s%s%s'
  @national_format               = '%s%s%s%s'

  @default_space = ' '
  @default_local_space = ' '
end

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


21
22
23
# File 'lib/phony/country_codes.rb', line 21

def self.instance
  @instance ||= new
end

Instance Method Details

#[](cc) ⇒ Object

Get the Country object for the given CC.


27
28
29
# File 'lib/phony/country_codes.rb', line 27

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

#add(country_code, country) ⇒ Object

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


213
214
215
216
217
218
219
220
# File 'lib/phony/country_codes.rb', line 213

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.


36
37
38
# File 'lib/phony/country_codes.rb', line 36

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.


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

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

#countrify(number, cc) ⇒ Object

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.


50
51
52
# File 'lib/phony/country_codes.rb', line 50

def countrify number, cc
  countrify!(number, cc) || number
end

#countrify!(number, cc) ⇒ Object


53
54
55
# File 'lib/phony/country_codes.rb', line 53

def countrify! number, cc
  number.sub!(/\A/, cc) # @countrify_regex, @cc
end

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


92
93
94
95
96
97
98
99
100
101
102
# File 'lib/phony/country_codes.rb', line 92

def format number, options = {}
  country, cc, trunk, ndc, *parts = internal_split number
  format_cc_ndc_local \
    options[:format]       || country.format,
    options[:spaces]       || country.space                           || @default_space,
    options[:local_spaces] || country.local_space || options[:spaces] || @default_local_space,
    cc,
    options[:trunk] == false ? nil : trunk,
    ndc,
    *parts
end

#format_cc_ndc(format, space, cc, trunk, ndc, local) ⇒ Object

cc_ndc = cc_ndc.slice 0…cc_ndc.rindex(space.to_s) if parts.empty?


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/phony/country_codes.rb', line 118

def format_cc_ndc format, space, cc, trunk, ndc, local
  case format
  when String
    format % { :cc => cc, :ndc => ndc, :local => local }
  when nil, :international_absolute, :international, :+
    ndc ?
      @international_absolute_format % [cc, space, ndc, space, local] :
      @international_absolute_format % [cc, space, local, nil, nil]
  when :international_relative
    ndc ?
      @international_relative_format % [cc, space, ndc, space, local] :
      @international_relative_format % [cc, space, local, nil, nil]
  when :national
    # Replaces the %s in the trunk code with a "space".
    trunk = trunk % space if trunk && trunk.size > 1
    ndc && !ndc.empty? ?
      @national_format % [trunk, ndc, space, local] :
      @national_format % [trunk, nil, nil,   local]
  when :local
    local
  end
end

#format_cc_ndc_local(format, space, local_space, cc, trunk, ndc, *parts) ⇒ Object

Formats country code and national destination code.


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/phony/country_codes.rb', line 107

def format_cc_ndc_local format, space, local_space, cc, trunk, ndc, *parts
  local = if parts.empty?
            EMPTY_STRING
          else
            format_local(local_space, parts) unless parts.empty?
          end
  
  format_cc_ndc format, space, cc, trunk, ndc, local
  
  # cc_ndc = cc_ndc.slice 0...cc_ndc.rindex(space.to_s) if parts.empty?
end

#format_local(local_space, parts_ary) ⇒ Object


140
141
142
143
# File 'lib/phony/country_codes.rb', line 140

def format_local local_space, parts_ary
  parts_ary.compact!
  parts_ary.join local_space.to_s
end

#internal_split(number) ⇒ Object


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

def internal_split number
  country, cc, rest = split_cc number
  [country, cc, *country.split(rest)]
end

#landline?(number) ⇒ Boolean

Returns:

  • (Boolean)

155
156
157
158
# File 'lib/phony/country_codes.rb', line 155

def landline? number
  country, _, rest = split_cc number
  country.landline? rest
end

#mobile?(number) ⇒ Boolean

Returns:

  • (Boolean)

151
152
153
154
# File 'lib/phony/country_codes.rb', line 151

def mobile? number
  country, _, rest = split_cc number
  country.mobile? rest
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.

68
69
70
71
72
73
74
75
76
77
78
# File 'lib/phony/country_codes.rb', line 68

def normalize number, options = {}
  country = if cc = options[:cc]
    self[cc]
  else
    clean! number
    country, cc, number = split_cc number
    country
  end
  number = country.normalize number
  countrify! number, cc
end

#plausible?(number, hints = {}) ⇒ Boolean

Returns:

  • (Boolean)

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/phony/country_codes.rb', line 183

def plausible? number, hints = {}
  normalized = clean number

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

  country, cc, rest = split_cc 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

#service?(number) ⇒ Boolean

Returns:

  • (Boolean)

147
148
149
150
# File 'lib/phony/country_codes.rb', line 147

def service? number
  country, _, rest = split_cc number
  country.service? rest
end

#split(number) ⇒ Object

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


82
83
84
85
# File 'lib/phony/country_codes.rb', line 82

def split number
  _, *cc_split_rest = internal_split number
  cc_split_rest
end

#split_cc(rest) ⇒ Object


173
174
175
176
177
178
179
180
181
# File 'lib/phony/country_codes.rb', line 173

def split_cc rest
  presumed_cc = ''
  1.upto(3) do |i|
    presumed_cc << rest.slice!(0..0)
    country = countries[i][presumed_cc]
    return [country, presumed_cc, rest] if country
  end
  # This line is never reached as CCs are in prefix code.
end

#vanity?(number) ⇒ Boolean

Is the given number a vanity number?

Returns:

  • (Boolean)

162
163
164
165
# File 'lib/phony/country_codes.rb', line 162

def vanity? number
  country, _, rest = split_cc number
  country.vanity? rest
end

#vanity_to_number(vanity_number) ⇒ Object

Converts a vanity number into a normalized E164 number.


168
169
170
171
# File 'lib/phony/country_codes.rb', line 168

def vanity_to_number vanity_number
  country, cc, rest = split_cc vanity_number
  "#{cc}#{country.vanity_to_number(rest)}"
end