phony 1.2.6 → 1.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/phony/countries/italy.rb +1 -0
- data/lib/phony/country.rb +20 -4
- data/lib/phony/country_codes.rb +2 -12
- data/lib/phony/national_code.rb +11 -1
- data/spec/lib/phony/country_codes_spec.rb +0 -6
- data/spec/lib/phony/country_spec.rb +5 -0
- data/spec/lib/phony/national_code_spec.rb +42 -0
- data/spec/lib/phony_spec.rb +3 -0
- metadata +2 -2
data/lib/phony/country.rb
CHANGED
@@ -17,6 +17,20 @@ module Phony
|
|
17
17
|
[ndc, *rest]
|
18
18
|
end
|
19
19
|
|
20
|
+
# Removes 0s from partially normalized numbers
|
21
|
+
# such as 410443643533.
|
22
|
+
#
|
23
|
+
# Example:
|
24
|
+
# 410443643533 -> 41443643533
|
25
|
+
#
|
26
|
+
# In some cases it doesn't, like Italy.
|
27
|
+
#
|
28
|
+
def normalize national_number
|
29
|
+
normalized = @special_code.normalize national_number if @special_code
|
30
|
+
normalized = @national_code.normalize national_number unless normalized && !normalized.empty?
|
31
|
+
normalized
|
32
|
+
end
|
33
|
+
|
20
34
|
# Is this national number a vanity number?
|
21
35
|
#
|
22
36
|
def vanity? national_number
|
@@ -50,17 +64,18 @@ module Phony
|
|
50
64
|
# If that is not the case, I will expand the framework.
|
51
65
|
#
|
52
66
|
def self.configured options = {}
|
67
|
+
normalize = options[:normalize]
|
53
68
|
ndc_fallback_length = options[:ndc_fallback_length]
|
54
69
|
ndc_mapping = options[:ndc_mapping] || raise("No ndc_mapping given!")
|
55
70
|
|
56
71
|
national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
|
57
72
|
local_splitter = Phony::LocalSplitter.instance_for options[:local_format] || [3, 2, 2]
|
58
|
-
national_code = Phony::NationalCode.new national_splitter, local_splitter
|
73
|
+
national_code = Phony::NationalCode.new national_splitter, local_splitter, normalize
|
59
74
|
|
60
75
|
if ndc_mapping[:service]
|
61
76
|
service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => ndc_mapping[:service]
|
62
77
|
service_local_splitter = Phony::LocalSplitter.instance_for options[:service_local_format] || [3, 6]
|
63
|
-
service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter
|
78
|
+
service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
|
64
79
|
end
|
65
80
|
|
66
81
|
new national_code, service_code
|
@@ -78,20 +93,21 @@ module Phony
|
|
78
93
|
# :service_ndcs => ['800']
|
79
94
|
#
|
80
95
|
def self.fixed options = {}
|
96
|
+
normalize = options[:normalize]
|
81
97
|
ndc_length = options[:ndc_length]
|
82
98
|
service_ndcs = options[:service_ndcs]
|
83
99
|
local_format = options[:local_format]
|
84
100
|
|
85
101
|
national_splitter = Phony::NationalSplitters::Fixed.new ndc_length
|
86
102
|
local_splitter = Phony::LocalSplitter.instance_for local_format || [3, 2, 2]
|
87
|
-
national_code = Phony::NationalCode.new national_splitter, local_splitter
|
103
|
+
national_code = Phony::NationalCode.new national_splitter, local_splitter, normalize
|
88
104
|
|
89
105
|
service_code = nil
|
90
106
|
if service_ndcs
|
91
107
|
service_local_format = options[:service_local_format] || local_format
|
92
108
|
service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => service_ndcs
|
93
109
|
service_local_splitter = Phony::LocalSplitter.instance_for service_local_format || [3, 3]
|
94
|
-
service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter
|
110
|
+
service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
|
95
111
|
end
|
96
112
|
|
97
113
|
new national_code, service_code
|
data/lib/phony/country_codes.rb
CHANGED
@@ -8,7 +8,8 @@ module Phony
|
|
8
8
|
# Remove non-digit chars.
|
9
9
|
#
|
10
10
|
number.gsub! /\D*/, ''
|
11
|
-
|
11
|
+
national_handler, cc, rest = split_cc number
|
12
|
+
'%s%s' % [cc, national_handler.normalize(rest)]
|
12
13
|
end
|
13
14
|
|
14
15
|
# Splits this number into cc, ndc and locally split number parts.
|
@@ -79,17 +80,6 @@ module Phony
|
|
79
80
|
# This line is never reached as CCs are in prefix code.
|
80
81
|
end
|
81
82
|
|
82
|
-
# Removes 0s from partially normalized numbers
|
83
|
-
# such as 410443643533.
|
84
|
-
#
|
85
|
-
# Example:
|
86
|
-
# 410443643533 -> 41443643533
|
87
|
-
#
|
88
|
-
def remove_relative_zeros! phone_number
|
89
|
-
_, cc, rest = split_cc phone_number
|
90
|
-
'%s%s' % [cc, rest].collect! { |code| code.gsub(/^0+/, '') }
|
91
|
-
end
|
92
|
-
|
93
83
|
# Cached mapping of all countries.
|
94
84
|
#
|
95
85
|
def mapping
|
data/lib/phony/national_code.rb
CHANGED
@@ -8,9 +8,10 @@ module Phony
|
|
8
8
|
|
9
9
|
#
|
10
10
|
#
|
11
|
-
def initialize national_splitter, local_splitter
|
11
|
+
def initialize national_splitter, local_splitter, normalize = nil
|
12
12
|
@national_splitter = national_splitter
|
13
13
|
@local_splitter = local_splitter
|
14
|
+
@normalize = !(normalize == false) # if nil, true (default), if false, false, if true, true.
|
14
15
|
end
|
15
16
|
|
16
17
|
# Split gets a number without country code and splits it into
|
@@ -22,6 +23,15 @@ module Phony
|
|
22
23
|
[ndc, *@local_splitter.split(rest)]
|
23
24
|
end
|
24
25
|
|
26
|
+
# Split gets a number withou country code and removes a relative zero.
|
27
|
+
#
|
28
|
+
# Note: Some cases, like Italy, don't remove the relative zero.
|
29
|
+
#
|
30
|
+
def normalize national_number
|
31
|
+
return national_number unless @normalize
|
32
|
+
national_number.gsub(/^0+/, '')
|
33
|
+
end
|
34
|
+
|
25
35
|
end
|
26
36
|
|
27
37
|
end
|
@@ -12,12 +12,6 @@ describe Phony::CountryCodes do
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
describe 'remove_relative_zeros' do
|
16
|
-
it "should remove an ndc zero from an almost normalized number and return it" do
|
17
|
-
@countries.remove_relative_zeros!('410443643533').should == '41443643533'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
15
|
describe 'formatted' do
|
22
16
|
it 'formats correctly' do
|
23
17
|
@countries.formatted('41443643532', :format => :international, :spaces => :-).should == '+41-44-364-35-32'
|
@@ -63,6 +63,11 @@ describe Phony::Country do
|
|
63
63
|
@switzerland.split('443643532').should == ['44', '364', '35', '32']
|
64
64
|
end
|
65
65
|
end
|
66
|
+
describe 'normalize' do
|
67
|
+
it "should handle ZH" do
|
68
|
+
@switzerland.normalize('0443643532').should == '443643532'
|
69
|
+
end
|
70
|
+
end
|
66
71
|
end
|
67
72
|
|
68
73
|
context "without special cases" do
|
@@ -16,6 +16,12 @@ describe Phony::NationalCode do
|
|
16
16
|
it 'splits correctly' do
|
17
17
|
@national.split('44364353').should == ['44', '364', '35', '3']
|
18
18
|
end
|
19
|
+
it 'normalizes correctly' do
|
20
|
+
@national.normalize('044364353').should == '44364353'
|
21
|
+
end
|
22
|
+
it 'normalizes correctly' do
|
23
|
+
@national.normalize('44364353').should == '44364353'
|
24
|
+
end
|
19
25
|
end
|
20
26
|
context 'with fixed ndc (French)' do
|
21
27
|
before(:each) do
|
@@ -30,6 +36,42 @@ describe Phony::NationalCode do
|
|
30
36
|
it 'splits correctly' do
|
31
37
|
@national.split('14227818').should == ['1', '42', '27', '81', '8']
|
32
38
|
end
|
39
|
+
it 'normalizes correctly' do
|
40
|
+
@national.normalize('0142278186').should == '142278186'
|
41
|
+
end
|
42
|
+
it 'normalizes correctly' do
|
43
|
+
@national.normalize('142278186').should == '142278186'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context 'normalizing' do
|
47
|
+
context 'false' do
|
48
|
+
before(:each) do
|
49
|
+
@national = Phony::NationalCode.new nil, nil, false
|
50
|
+
end
|
51
|
+
it 'normalizes an italian case correctly' do
|
52
|
+
@national.normalize('0909709511').should == '0909709511'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
context 'true' do
|
56
|
+
before(:each) do
|
57
|
+
national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
|
58
|
+
local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
|
59
|
+
@national = Phony::NationalCode.new national_splitter, local_splitter, true
|
60
|
+
end
|
61
|
+
it 'normalizes a swiss case correctly' do
|
62
|
+
@national.normalize('044364353').should == '44364353'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context 'nil (true)' do
|
66
|
+
before(:each) do
|
67
|
+
national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
|
68
|
+
local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
|
69
|
+
@national = Phony::NationalCode.new national_splitter, local_splitter, nil
|
70
|
+
end
|
71
|
+
it 'normalizes a swiss case correctly' do
|
72
|
+
@national.normalize('044364353').should == '44364353'
|
73
|
+
end
|
74
|
+
end
|
33
75
|
end
|
34
76
|
end
|
35
77
|
|
data/spec/lib/phony_spec.rb
CHANGED
@@ -155,6 +155,9 @@ describe Phony do
|
|
155
155
|
it "should normalize a number with erroneous zero inside" do
|
156
156
|
Phony.normalize('+410443643533').should == '41443643533'
|
157
157
|
end
|
158
|
+
it "should not normalize a number with a correct zero inside" do
|
159
|
+
Phony.normalize('+390909709511').should == '390909709511'
|
160
|
+
end
|
158
161
|
end
|
159
162
|
end
|
160
163
|
|