phony 2.15.25 → 2.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.textile +35 -7
- data/lib/phony/config.rb +91 -0
- data/lib/phony/countries/argentina.rb +355 -0
- data/lib/phony/countries/austria.rb +32 -24
- data/lib/phony/countries/bangladesh.rb +2 -0
- data/lib/phony/countries/belarus.rb +2 -0
- data/lib/phony/countries/brazil.rb +6 -4
- data/lib/phony/countries/cambodia.rb +6 -4
- data/lib/phony/countries/china.rb +7 -2
- data/lib/phony/countries/croatia.rb +13 -16
- data/lib/phony/countries/georgia.rb +6 -4
- data/lib/phony/countries/germany.rb +5 -2
- data/lib/phony/countries/guinea.rb +8 -5
- data/lib/phony/countries/india.rb +2 -0
- data/lib/phony/countries/indonesia.rb +7 -2
- data/lib/phony/countries/ireland.rb +27 -23
- data/lib/phony/countries/italy.rb +30 -17
- data/lib/phony/countries/japan.rb +61 -8
- data/lib/phony/countries/kyrgyzstan.rb +2 -0
- data/lib/phony/countries/latvia.rb +2 -0
- data/lib/phony/countries/libya.rb +3 -1
- data/lib/phony/countries/malaysia.rb +22 -2
- data/lib/phony/countries/moldova.rb +2 -0
- data/lib/phony/countries/montenegro.rb +2 -0
- data/lib/phony/countries/myanmar.rb +2 -0
- data/lib/phony/countries/namibia.rb +2 -0
- data/lib/phony/countries/nepal.rb +2 -0
- data/lib/phony/countries/netherlands.rb +2 -0
- data/lib/phony/countries/pakistan.rb +2 -0
- data/lib/phony/countries/paraguay.rb +2 -0
- data/lib/phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb +24 -14
- data/lib/phony/countries/saudi_arabia.rb +3 -0
- data/lib/phony/countries/serbia.rb +7 -1
- data/lib/phony/countries/somalia.rb +5 -1
- data/lib/phony/countries/south_korea.rb +16 -9
- data/lib/phony/countries/sweden.rb +29 -17
- data/lib/phony/countries/taiwan.rb +22 -46
- data/lib/phony/countries/tajikistan.rb +2 -0
- data/lib/phony/countries/turkmenistan.rb +2 -0
- data/lib/phony/countries/ukraine.rb +3 -0
- data/lib/phony/countries/united_kingdom.rb +5 -2
- data/lib/phony/countries/uruguay.rb +2 -0
- data/lib/phony/countries/vietnam.rb +94 -92
- data/lib/phony/countries/zimbabwe.rb +2 -0
- data/lib/phony/countries.rb +181 -78
- data/lib/phony/country.rb +15 -3
- data/lib/phony/country_codes.rb +17 -4
- data/lib/phony/dsl.rb +5 -3
- data/lib/phony/local_splitters/fixed.rb +2 -0
- data/lib/phony/national_code.rb +1 -1
- data/lib/phony/national_splitters/none.rb +1 -3
- data/lib/phony/trunk_code.rb +5 -4
- data/lib/phony/vanity.rb +1 -1
- data/lib/phony.rb +94 -60
- data/spec/functional/config_spec.rb +44 -0
- data/spec/functional/plausibility_spec.rb +364 -32
- data/spec/lib/phony/countries_spec.rb +245 -52
- data/spec/lib/phony/country_codes_spec.rb +106 -33
- data/spec/lib/phony/country_spec.rb +54 -15
- data/spec/lib/phony/dsl_spec.rb +2 -2
- data/spec/lib/phony/local_splitters/regex_spec.rb +12 -5
- data/spec/lib/phony/national_splitters/default_spec.rb +1 -1
- data/spec/lib/phony/trunk_code_spec.rb +85 -0
- data/spec/lib/phony/vanity_spec.rb +30 -0
- data/spec/lib/phony_spec.rb +70 -0
- metadata +26 -18
data/lib/phony/national_code.rb
CHANGED
@@ -31,9 +31,7 @@ module Phony
|
|
31
31
|
# since using nil is dangerous and breaks
|
32
32
|
# abstraction)
|
33
33
|
#
|
34
|
-
# Note:
|
35
|
-
#
|
36
|
-
# TODO Flip nil/false?
|
34
|
+
# Note: Decided it stays in. When formatting, it's turned into nil.
|
37
35
|
#
|
38
36
|
def split national_number
|
39
37
|
[nil, false, national_number]
|
data/lib/phony/trunk_code.rb
CHANGED
@@ -6,8 +6,9 @@ module Phony
|
|
6
6
|
# * code: The trunk code, e.g. 0.
|
7
7
|
#
|
8
8
|
# Options:
|
9
|
-
# * normalize: Remove the trunk code when normalizing (only use if number scheme is defined unambiguously).
|
10
|
-
# * split: Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
|
9
|
+
# * normalize: [true (default), false] Remove the trunk code when normalizing (only use if number scheme is defined unambiguously).
|
10
|
+
# * split: [true, false (default)] Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
|
11
|
+
# * format: [true (default), false] Add the trunk code when formatting (passing `false` will not add it).
|
11
12
|
#
|
12
13
|
def initialize code, options = {}
|
13
14
|
@code = code
|
@@ -34,8 +35,8 @@ module Phony
|
|
34
35
|
|
35
36
|
# Normalize normalizes the given national number.
|
36
37
|
#
|
37
|
-
def normalize national_number
|
38
|
-
national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @normalize
|
38
|
+
def normalize national_number, options = {}
|
39
|
+
national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @normalize && options[:cc]
|
39
40
|
return national_number
|
40
41
|
end
|
41
42
|
|
data/lib/phony/vanity.rb
CHANGED
data/lib/phony.rb
CHANGED
@@ -1,70 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# NOTE We use Kernel.load here, as it's possible to redefine Phony via Phony::Config.
|
4
|
+
|
1
5
|
# Framework.
|
2
6
|
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
load File.expand_path '../phony/config.rb', __FILE__
|
8
|
+
load File.expand_path '../phony/vanity.rb', __FILE__
|
9
|
+
load File.expand_path '../phony/local_splitters/fixed.rb', __FILE__
|
10
|
+
load File.expand_path '../phony/local_splitters/regex.rb', __FILE__
|
11
|
+
load File.expand_path '../phony/national_splitters/dsl.rb', __FILE__
|
12
|
+
load File.expand_path '../phony/national_splitters/fixed.rb', __FILE__
|
13
|
+
load File.expand_path '../phony/national_splitters/variable.rb', __FILE__
|
14
|
+
load File.expand_path '../phony/national_splitters/regex.rb', __FILE__
|
15
|
+
load File.expand_path '../phony/national_splitters/default.rb', __FILE__
|
16
|
+
load File.expand_path '../phony/national_splitters/none.rb', __FILE__
|
17
|
+
load File.expand_path '../phony/national_code.rb', __FILE__
|
18
|
+
load File.expand_path '../phony/country.rb', __FILE__
|
19
|
+
load File.expand_path '../phony/trunk_code.rb', __FILE__
|
20
|
+
load File.expand_path '../phony/country_codes.rb', __FILE__
|
21
|
+
load File.expand_path '../phony/dsl.rb', __FILE__
|
18
22
|
|
19
23
|
# Countries.
|
20
24
|
#
|
21
25
|
# The ones that need more space to define.
|
22
26
|
#
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
27
|
+
load File.expand_path '../phony/countries/argentina.rb', __FILE__
|
28
|
+
load File.expand_path '../phony/countries/austria.rb', __FILE__
|
29
|
+
load File.expand_path '../phony/countries/bangladesh.rb', __FILE__
|
30
|
+
load File.expand_path '../phony/countries/belarus.rb', __FILE__
|
31
|
+
load File.expand_path '../phony/countries/brazil.rb', __FILE__
|
32
|
+
load File.expand_path '../phony/countries/cambodia.rb', __FILE__
|
33
|
+
load File.expand_path '../phony/countries/croatia.rb', __FILE__
|
34
|
+
load File.expand_path '../phony/countries/china.rb', __FILE__
|
35
|
+
load File.expand_path '../phony/countries/georgia.rb', __FILE__
|
36
|
+
load File.expand_path '../phony/countries/germany.rb', __FILE__
|
37
|
+
load File.expand_path '../phony/countries/guinea.rb', __FILE__
|
38
|
+
load File.expand_path '../phony/countries/india.rb', __FILE__
|
39
|
+
load File.expand_path '../phony/countries/indonesia.rb', __FILE__
|
40
|
+
load File.expand_path '../phony/countries/ireland.rb', __FILE__
|
41
|
+
load File.expand_path '../phony/countries/italy.rb', __FILE__
|
42
|
+
load File.expand_path '../phony/countries/japan.rb', __FILE__
|
43
|
+
load File.expand_path '../phony/countries/kyrgyzstan.rb', __FILE__
|
44
|
+
load File.expand_path '../phony/countries/latvia.rb', __FILE__
|
45
|
+
load File.expand_path '../phony/countries/libya.rb', __FILE__
|
46
|
+
load File.expand_path '../phony/countries/malaysia.rb', __FILE__
|
47
|
+
load File.expand_path '../phony/countries/moldova.rb', __FILE__
|
48
|
+
load File.expand_path '../phony/countries/montenegro.rb', __FILE__
|
49
|
+
load File.expand_path '../phony/countries/myanmar.rb', __FILE__
|
50
|
+
load File.expand_path '../phony/countries/namibia.rb', __FILE__
|
51
|
+
load File.expand_path '../phony/countries/nepal.rb', __FILE__
|
52
|
+
load File.expand_path '../phony/countries/netherlands.rb', __FILE__
|
53
|
+
load File.expand_path '../phony/countries/pakistan.rb', __FILE__
|
54
|
+
load File.expand_path '../phony/countries/paraguay.rb', __FILE__
|
55
|
+
load File.expand_path '../phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb', __FILE__
|
56
|
+
load File.expand_path '../phony/countries/saudi_arabia.rb', __FILE__
|
57
|
+
load File.expand_path '../phony/countries/serbia.rb', __FILE__
|
58
|
+
load File.expand_path '../phony/countries/somalia.rb', __FILE__
|
59
|
+
load File.expand_path '../phony/countries/south_korea.rb', __FILE__
|
60
|
+
load File.expand_path '../phony/countries/sweden.rb', __FILE__
|
61
|
+
load File.expand_path '../phony/countries/taiwan.rb', __FILE__
|
62
|
+
load File.expand_path '../phony/countries/tajikistan.rb', __FILE__
|
63
|
+
load File.expand_path '../phony/countries/turkmenistan.rb', __FILE__
|
64
|
+
load File.expand_path '../phony/countries/vietnam.rb', __FILE__
|
65
|
+
load File.expand_path '../phony/countries/ukraine.rb', __FILE__
|
66
|
+
load File.expand_path '../phony/countries/united_kingdom.rb', __FILE__
|
67
|
+
load File.expand_path '../phony/countries/uruguay.rb', __FILE__
|
68
|
+
load File.expand_path '../phony/countries/zimbabwe.rb', __FILE__
|
64
69
|
|
65
70
|
# All other countries.
|
66
71
|
#
|
67
|
-
|
72
|
+
load File.expand_path '../phony/countries.rb', __FILE__
|
68
73
|
|
69
74
|
# Phony is the main module and is generally used to process
|
70
75
|
# E164 phone numbers directly.
|
@@ -76,11 +81,33 @@ module Phony
|
|
76
81
|
# @example
|
77
82
|
# Phony.normalize("Fnork!") # Raises a Phony::NormalizationError.
|
78
83
|
#
|
79
|
-
class NormalizationError <
|
84
|
+
class NormalizationError < ArgumentError
|
80
85
|
def initialize
|
81
86
|
super %Q{Phony could not normalize the given number. Is it a phone number?}
|
82
87
|
end
|
83
88
|
end
|
89
|
+
|
90
|
+
# Raised in case Phony can't split a given number.
|
91
|
+
#
|
92
|
+
# @example
|
93
|
+
# Phony.split("Fnork!") # Raises a Phony::SplittingError.
|
94
|
+
#
|
95
|
+
class SplittingError < ArgumentError
|
96
|
+
def initialize number
|
97
|
+
super %Q{Phony could not split the given number. Is #{(number.nil? || number == '') ? 'it' : number.inspect} a phone number?}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Raised in case Phony can't format a given number.
|
102
|
+
#
|
103
|
+
# @example
|
104
|
+
# Phony.format("Fnork!") # Raises a Phony::FormattingError.
|
105
|
+
#
|
106
|
+
class FormattingError < ArgumentError
|
107
|
+
def initialize
|
108
|
+
super %Q{Phony could not format the given number. Is it a phone number?}
|
109
|
+
end
|
110
|
+
end
|
84
111
|
|
85
112
|
# Phony uses a single country codes instance.
|
86
113
|
#
|
@@ -121,6 +148,7 @@ module Phony
|
|
121
148
|
#
|
122
149
|
def normalize phone_number, options = {}
|
123
150
|
raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number)." unless phone_number
|
151
|
+
|
124
152
|
normalize! phone_number.dup, options
|
125
153
|
end
|
126
154
|
# A destructive version of {#normalize}.
|
@@ -162,7 +190,8 @@ module Phony
|
|
162
190
|
#
|
163
191
|
def split phone_number
|
164
192
|
raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.split(number)." unless phone_number
|
165
|
-
|
193
|
+
|
194
|
+
split! phone_number.dup, phone_number
|
166
195
|
end
|
167
196
|
# A destructive version of {#split}.
|
168
197
|
#
|
@@ -178,8 +207,11 @@ module Phony
|
|
178
207
|
# @example Split a NANP number.
|
179
208
|
# Phony.split!("13015550100") # => ["1", "301", "555", "0100"]
|
180
209
|
#
|
181
|
-
def split! phone_number
|
210
|
+
def split! phone_number, error_number = nil
|
182
211
|
@codes.split phone_number
|
212
|
+
rescue
|
213
|
+
# NB The error_number (reference) is used because phone_number is destructively handled.
|
214
|
+
raise SplittingError.new(error_number)
|
183
215
|
end
|
184
216
|
|
185
217
|
# Formats a normalized E164 number according to a country's formatting scheme.
|
@@ -231,6 +263,8 @@ module Phony
|
|
231
263
|
#
|
232
264
|
def format! phone_number, options = {}
|
233
265
|
@codes.format phone_number, options
|
266
|
+
rescue
|
267
|
+
raise FormattingError.new
|
234
268
|
end
|
235
269
|
alias formatted format
|
236
270
|
alias formatted! format!
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
describe 'Phony::Config' do
|
4
|
+
describe 'load' do
|
5
|
+
before do
|
6
|
+
# NOTE We redefine Phony as if it was not loaded for this set of tests.
|
7
|
+
Object.__send__(:remove_const, :Phony) if Object.constants.include?(:Phony)
|
8
|
+
|
9
|
+
load 'phony/config.rb'
|
10
|
+
end
|
11
|
+
after(:all) do
|
12
|
+
# After running this suite, we load all of Phony for the following tests.
|
13
|
+
load 'phony/config.rb'
|
14
|
+
|
15
|
+
Phony::Config.load
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'does not fail when loading all' do
|
19
|
+
Phony::Config.load
|
20
|
+
|
21
|
+
Phony.split('15551115511').should == ['1', '555', '111', '5511']
|
22
|
+
end
|
23
|
+
it 'raises when a CC is used that has not been loaded.' do
|
24
|
+
Phony::Config.load('41')
|
25
|
+
|
26
|
+
expect { Phony.split('15551115511') }.to raise_error
|
27
|
+
end
|
28
|
+
it 'raises when a CC is used that has not been loaded.' do
|
29
|
+
Phony::Config.load(only: ['41'])
|
30
|
+
|
31
|
+
expect { Phony.split('15551115511') }.to raise_error
|
32
|
+
end
|
33
|
+
it 'raises when a CC is used that has not been loaded.' do
|
34
|
+
Phony::Config.load(except: ['1'])
|
35
|
+
|
36
|
+
expect { Phony.split('15551115511') }.to raise_error
|
37
|
+
end
|
38
|
+
it 'does not raise when a CC is used that has been loaded.' do
|
39
|
+
Phony::Config.load(except: ['41'])
|
40
|
+
|
41
|
+
Phony.split('15551115511').should == ['1', '555', '111', '5511']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|