phony 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +14 -2
- data/lib/phony.rb +58 -463
- data/lib/phony/countries/all_other.rb +417 -0
- data/lib/phony/countries/austria.rb +69 -0
- data/lib/phony/countries/egypt.rb +40 -0
- data/lib/phony/countries/germany.rb +156 -0
- data/lib/phony/countries/greece.rb +30 -0
- data/lib/phony/countries/hungary.rb +23 -0
- data/lib/phony/countries/italy.rb +105 -0
- data/lib/phony/country.rb +102 -0
- data/lib/phony/country_codes.rb +102 -0
- data/lib/phony/local_splitter.rb +46 -0
- data/lib/phony/national_code.rb +27 -0
- data/lib/phony/national_splitters/fixed.rb +36 -0
- data/lib/phony/national_splitters/variable.rb +64 -0
- data/lib/phony/vanity.rb +35 -0
- data/spec/lib/phony/countries/austria_spec.rb +24 -0
- data/spec/lib/phony/countries/egypt_spec.rb +18 -0
- data/spec/lib/phony/countries/germany_spec.rb +24 -0
- data/spec/lib/phony/countries/greece_spec.rb +18 -0
- data/spec/lib/phony/countries/hungary_spec.rb +18 -0
- data/spec/lib/phony/countries/switzerland_spec.rb +18 -0
- data/spec/lib/phony/country_codes_spec.rb +110 -0
- data/spec/lib/phony/country_spec.rb +91 -0
- data/spec/lib/phony/local_splitter_spec.rb +48 -0
- data/spec/lib/phony/national_code_spec.rb +36 -0
- data/spec/lib/phony/national_splitters/fixed_spec.rb +43 -0
- data/spec/lib/phony/national_splitters/variable_spec.rb +35 -0
- data/spec/lib/phony/vanity_spec.rb +34 -0
- data/spec/lib/phony_spec.rb +117 -238
- metadata +45 -21
- data/lib/ndc/austria.rb +0 -69
- data/lib/ndc/fixed_size.rb +0 -113
- data/lib/ndc/germany.rb +0 -157
- data/lib/ndc/prefix.rb +0 -67
- data/lib/ndc/splitter.rb +0 -81
- data/spec/lib/ndc/austria_spec.rb +0 -40
- data/spec/lib/ndc/fixed_size_spec.rb +0 -65
- data/spec/lib/ndc/germany_spec.rb +0 -40
- data/spec/lib/ndc/prefix_spec.rb +0 -25
- data/spec/lib/ndc/splitter_spec.rb +0 -59
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phony::CountryCodes do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@countries = Phony::CountryCodes.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'split' do
|
10
|
+
it 'splits correctly' do
|
11
|
+
@countries.split('41443643532').should == ['41', '44', '364', '35', '32']
|
12
|
+
end
|
13
|
+
end
|
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
|
+
describe 'formatted' do
|
22
|
+
it 'formats correctly' do
|
23
|
+
@countries.formatted('41443643532', :format => :international, :spaces => :-).should == '+41-44-364-35-32'
|
24
|
+
end
|
25
|
+
it 'formats correctly' do
|
26
|
+
@countries.formatted('41443643532', :format => :international_relative, :spaces => :-).should == '0041-44-364-35-32'
|
27
|
+
end
|
28
|
+
it 'formats correctly' do
|
29
|
+
@countries.formatted('41443643532', :format => :national, :spaces => :-).should == '044-364-35-32'
|
30
|
+
end
|
31
|
+
context 'default' do
|
32
|
+
it "should format swiss numbers" do
|
33
|
+
@countries.formatted('41443643532').should == '+41 44 364 35 32'
|
34
|
+
end
|
35
|
+
it "should format swiss service numbers" do
|
36
|
+
@countries.formatted('41800112233').should == '+41 800 112 233'
|
37
|
+
end
|
38
|
+
it "should format austrian numbers" do
|
39
|
+
@countries.formatted('43198110').should == '+43 1 98110'
|
40
|
+
end
|
41
|
+
it "should format american numbers" do
|
42
|
+
@countries.formatted('18705551122').should == '+1 870 555 1122'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
describe "international" do
|
46
|
+
it "should format north american numbers" do
|
47
|
+
@countries.formatted('18091231234', :format => :international).should == '+1 809 123 1234'
|
48
|
+
end
|
49
|
+
it "should format austrian numbers" do
|
50
|
+
@countries.formatted('43198110', :format => :international).should == '+43 1 98110'
|
51
|
+
end
|
52
|
+
it "should format austrian numbers" do
|
53
|
+
@countries.formatted('43198110', :format => :international_absolute).should == '+43 1 98110'
|
54
|
+
end
|
55
|
+
it "should format french numbers" do
|
56
|
+
@countries.formatted('33142278186', :format => :+).should == '+33 1 42 27 81 86'
|
57
|
+
end
|
58
|
+
it "should format austrian numbers" do
|
59
|
+
@countries.formatted('43198110', :format => :international_relative).should == '0043 1 98110'
|
60
|
+
end
|
61
|
+
it 'should format liechtensteiner numbers' do
|
62
|
+
@countries.formatted('4233841148', :format => :international_relative).should == '00423 384 11 48'
|
63
|
+
end
|
64
|
+
context 'with no spaces' do
|
65
|
+
it "should format north american numbers" do
|
66
|
+
Phony.formatted('18091231234', :format => :international, :spaces => '').should == '+18091231234'
|
67
|
+
end
|
68
|
+
it "should format austrian numbers" do
|
69
|
+
Phony.formatted('43198110', :format => :international, :spaces => '').should == '+43198110'
|
70
|
+
end
|
71
|
+
it "should format austrian numbers" do
|
72
|
+
Phony.formatted('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'
|
73
|
+
end
|
74
|
+
it "should format french numbers" do
|
75
|
+
Phony.formatted('33142278186', :format => :+, :spaces => '').should == '+33142278186'
|
76
|
+
end
|
77
|
+
it "should format austrian numbers" do
|
78
|
+
Phony.formatted('43198110', :format => :international_relative, :spaces => '').should == '0043198110'
|
79
|
+
end
|
80
|
+
it 'should format liechtensteiner numbers' do
|
81
|
+
Phony.formatted('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'
|
82
|
+
end
|
83
|
+
end
|
84
|
+
context 'with special spaces' do
|
85
|
+
it "should format swiss numbers" do
|
86
|
+
Phony.formatted('41443643532', :format => :international).should == '+41 44 364 35 32'
|
87
|
+
end
|
88
|
+
it "should format north american numbers" do
|
89
|
+
Phony.formatted('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'
|
90
|
+
end
|
91
|
+
it "should format austrian numbers" do
|
92
|
+
Phony.formatted('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'
|
93
|
+
end
|
94
|
+
it "should format austrian numbers" do
|
95
|
+
Phony.formatted('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'
|
96
|
+
end
|
97
|
+
it "should format french numbers" do
|
98
|
+
Phony.formatted('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'
|
99
|
+
end
|
100
|
+
it "should format austrian numbers" do
|
101
|
+
Phony.formatted('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'
|
102
|
+
end
|
103
|
+
it 'should format liechtensteiner numbers' do
|
104
|
+
Phony.formatted('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phony::Country do
|
4
|
+
|
5
|
+
describe "configured" do
|
6
|
+
context "with variable ndcs" do
|
7
|
+
before(:each) do
|
8
|
+
@country = Phony::Country.configured :local_format => [3, 2, 2],
|
9
|
+
:service_local_format => [3, 3],
|
10
|
+
:ndc_fallback_length => 4,
|
11
|
+
:ndc_mapping => {
|
12
|
+
:normal => ['44'],
|
13
|
+
:service => ['800'],
|
14
|
+
:mobile => ['76']
|
15
|
+
}
|
16
|
+
end
|
17
|
+
it 'works for normal numbers' do
|
18
|
+
@country.split('443643532').should == ['44', '364', '35', '32']
|
19
|
+
end
|
20
|
+
it 'works with service numbers' do
|
21
|
+
@country.split('800333666').should == ['800', '333', '666']
|
22
|
+
end
|
23
|
+
it 'works with mobile numbers' do
|
24
|
+
@country.split('764333532').should == ['76', '433', '35', '32']
|
25
|
+
end
|
26
|
+
it 'uses the fallback if it is not in the mapping' do
|
27
|
+
@country.split('123456789').should == ['1234', '567', '89']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
context "with fixed ndcs" do
|
31
|
+
before(:each) do
|
32
|
+
@country = Phony::Country.fixed :ndc_length => 2,
|
33
|
+
:local_format => [3, 2, 2],
|
34
|
+
:service_local_format => [3, 3],
|
35
|
+
:service_ndcs => ['800']
|
36
|
+
end
|
37
|
+
it 'works for non-service numbers' do
|
38
|
+
@country.split('443643532').should == ['44', '364', '35', '32']
|
39
|
+
end
|
40
|
+
it 'works for service numbers' do
|
41
|
+
@country.split('800333666').should == ['800', '333', '666']
|
42
|
+
end
|
43
|
+
it 'works with mobile numbers' do
|
44
|
+
@country.split('764333532').should == ['76', '433', '35', '32']
|
45
|
+
end
|
46
|
+
it 'uses a fixed length' do
|
47
|
+
@country.split('123456789').should == ['12', '345', '67', '89']
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "without special cases" do
|
53
|
+
before(:each) do
|
54
|
+
national_splitter = Phony::NationalSplitters::Variable.new 4, { :normal => ['44'] }
|
55
|
+
local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
|
56
|
+
national_code = Phony::NationalCode.new national_splitter, local_splitter
|
57
|
+
|
58
|
+
@switzerland = Phony::Country.new national_code
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "split" do
|
62
|
+
it "should handle ZH" do
|
63
|
+
@switzerland.split('443643532').should == ['44', '364', '35', '32']
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "without special cases" do
|
69
|
+
before(:each) do
|
70
|
+
national_splitter = Phony::NationalSplitters::Variable.new 4, { :normal => ['44'] }
|
71
|
+
local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
|
72
|
+
national_code = Phony::NationalCode.new national_splitter, local_splitter
|
73
|
+
|
74
|
+
special_national_splitter = Phony::NationalSplitters::Variable.new nil, { :service => ['800'] }
|
75
|
+
special_local_splitter = Phony::LocalSplitter.instance_for [3, 3]
|
76
|
+
special_code = Phony::NationalCode.new special_national_splitter, special_local_splitter
|
77
|
+
|
78
|
+
@switzerland = Phony::Country.new national_code, special_code
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "split" do
|
82
|
+
it "should handle ZH" do
|
83
|
+
@switzerland.split('443643532').should == ['44', '364', '35', '32']
|
84
|
+
end
|
85
|
+
it "should handle 800" do
|
86
|
+
@switzerland.split('800333666').should == ['800', '333', '666']
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phony::LocalSplitter do
|
4
|
+
|
5
|
+
describe 'instance_for' do
|
6
|
+
it 'caches' do
|
7
|
+
Phony::LocalSplitter.instance_for([3,2,2]).should equal(Phony::LocalSplitter.instance_for([3,2,2]))
|
8
|
+
end
|
9
|
+
it 'caches correctly' do
|
10
|
+
Phony::LocalSplitter.instance_for([1,2,3]).should_not equal(Phony::LocalSplitter.instance_for([9,9,9]))
|
11
|
+
end
|
12
|
+
it 'caches correctly' do
|
13
|
+
Phony::LocalSplitter.instance_for.should equal(Phony::LocalSplitter.instance_for)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'split' do
|
18
|
+
context "without format" do
|
19
|
+
before(:each) do
|
20
|
+
@splitter = Phony::LocalSplitter.new
|
21
|
+
end
|
22
|
+
it 'splits correctly' do
|
23
|
+
@splitter.split('3643532').should == ['364','35','32']
|
24
|
+
end
|
25
|
+
it 'splits correctly even when the number is too long' do
|
26
|
+
@splitter.split('3643532111').should == ['364','35','32']
|
27
|
+
end
|
28
|
+
it 'splits correctly even when the number is too short' do
|
29
|
+
@splitter.split('364353').should == ['364','35','3'] # TODO Redo correctly.
|
30
|
+
end
|
31
|
+
end
|
32
|
+
context "with format" do
|
33
|
+
before(:each) do
|
34
|
+
@splitter = Phony::LocalSplitter.new [3, 2, 2]
|
35
|
+
end
|
36
|
+
it 'splits correctly' do
|
37
|
+
@splitter.split('3643532').should == ['364','35','32']
|
38
|
+
end
|
39
|
+
it 'splits correctly even when the number is too long' do
|
40
|
+
@splitter.split('3643532111').should == ['364','35','32']
|
41
|
+
end
|
42
|
+
it 'splits correctly even when the number is too short' do
|
43
|
+
@splitter.split('364353').should == ['364','35','3'] # TODO Redo correctly.
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phony::NationalCode do
|
4
|
+
|
5
|
+
describe 'split' do
|
6
|
+
context 'with fixed ndc (Swiss)' do
|
7
|
+
before(:each) do
|
8
|
+
national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
|
9
|
+
local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
|
10
|
+
|
11
|
+
@national = Phony::NationalCode.new national_splitter, local_splitter
|
12
|
+
end
|
13
|
+
it 'splits correctly' do
|
14
|
+
@national.split('443643532').should == ['44', '364', '35', '32']
|
15
|
+
end
|
16
|
+
it 'splits correctly' do
|
17
|
+
@national.split('44364353').should == ['44', '364', '35', '3']
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context 'with fixed ndc (French)' do
|
21
|
+
before(:each) do
|
22
|
+
national_splitter = Phony::NationalSplitters::Fixed.instance_for 1
|
23
|
+
local_splitter = Phony::LocalSplitter.instance_for [2, 2, 2, 2]
|
24
|
+
|
25
|
+
@national = Phony::NationalCode.new national_splitter, local_splitter
|
26
|
+
end
|
27
|
+
it 'splits correctly' do
|
28
|
+
@national.split('142278186').should == ['1', '42', '27', '81', '86']
|
29
|
+
end
|
30
|
+
it 'splits correctly' do
|
31
|
+
@national.split('14227818').should == ['1', '42', '27', '81', '8']
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phony::NationalSplitters::Fixed do
|
4
|
+
|
5
|
+
describe 'instance_for' do
|
6
|
+
it 'caches' do
|
7
|
+
Phony::NationalSplitters::Fixed.instance_for(3).should equal(Phony::NationalSplitters::Fixed.instance_for(3))
|
8
|
+
end
|
9
|
+
it 'caches correctly' do
|
10
|
+
Phony::NationalSplitters::Fixed.instance_for(1).should_not equal(Phony::NationalSplitters::Fixed.instance_for(2))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'split' do
|
15
|
+
before(:each) do
|
16
|
+
@splitter = Phony::NationalSplitters::Fixed.new 2
|
17
|
+
end
|
18
|
+
it 'splits correctly' do
|
19
|
+
@splitter.split('443643532').should == ['44', '3643532']
|
20
|
+
end
|
21
|
+
it 'splits correctly even when the number is too long' do
|
22
|
+
@splitter.split('44364353211').should == ['44', '364353211']
|
23
|
+
end
|
24
|
+
it 'splits correctly even when the number is too short' do
|
25
|
+
@splitter.split('443').should == ['44','3']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
describe 'split' do
|
29
|
+
before(:each) do
|
30
|
+
@splitter = Phony::NationalSplitters::Fixed.new nil
|
31
|
+
end
|
32
|
+
it 'splits correctly' do
|
33
|
+
@splitter.split('443643532').should == ['443643532']
|
34
|
+
end
|
35
|
+
it 'splits correctly even when the number is too long' do
|
36
|
+
@splitter.split('44364353211').should == ['44364353211']
|
37
|
+
end
|
38
|
+
it 'splits correctly even when the number is too short' do
|
39
|
+
@splitter.split('443').should == ['443']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phony::NationalSplitters::Variable do
|
4
|
+
|
5
|
+
describe 'split' do
|
6
|
+
context 'normal' do
|
7
|
+
before(:each) do
|
8
|
+
@splitter = Phony::NationalSplitters::Variable.new 4, :normal => ['1', '316'],
|
9
|
+
:mobile => ['67', '68',],
|
10
|
+
:service => ['669', '711']
|
11
|
+
end
|
12
|
+
it "handles Vienna" do
|
13
|
+
@splitter.split('198110').should == ['1', '98110']
|
14
|
+
end
|
15
|
+
it "handles some mobile services" do
|
16
|
+
@splitter.split('66914093902').should == ['669', '14093902']
|
17
|
+
end
|
18
|
+
it "handles Graz" do
|
19
|
+
@splitter.split('3161234567891').should == ['316', '1234567891']
|
20
|
+
end
|
21
|
+
it "handles Rohrau" do
|
22
|
+
@splitter.split('2164123456789').should == ['2164', '123456789']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context 'special handling for using the variable size splitter for Swiss service numbers' do
|
26
|
+
before(:each) do
|
27
|
+
@splitter = Phony::NationalSplitters::Variable.new 2, :service => ['800']
|
28
|
+
end
|
29
|
+
it "should handle swiss service numbers" do
|
30
|
+
@splitter.split('800223344').should == ['800', '223344']
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Phony::Vanity do
|
6
|
+
|
7
|
+
describe "vanity?" do
|
8
|
+
it 'recognizes a vanity number' do
|
9
|
+
Phony::Vanity.vanity?('800HELLOWORLD').should == true
|
10
|
+
end
|
11
|
+
it 'recognizes a non vanity number' do
|
12
|
+
Phony::Vanity.vanity?('444443322').should == false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "replace" do
|
17
|
+
it 'replaces all characters' do
|
18
|
+
Phony::Vanity.replace('0123456789abcdefghijklmnopqrstuvwxyz').should == '012345678922233344455566677778889999'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'mapping' do
|
23
|
+
it 'caches' do
|
24
|
+
Phony::Vanity.mapping.should equal(Phony::Vanity.mapping)
|
25
|
+
end
|
26
|
+
it 'returns the right thing' do
|
27
|
+
Phony::Vanity.mapping.should == [
|
28
|
+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.freeze,
|
29
|
+
'2223334445556667777888999922233344455566677778889999'.freeze
|
30
|
+
]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/spec/lib/phony_spec.rb
CHANGED
@@ -4,6 +4,49 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Phony do
|
6
6
|
|
7
|
+
describe "split" do
|
8
|
+
it "should handle austrian numbers" do
|
9
|
+
Phony.split('43198110').should == ['43', '1', '98110']
|
10
|
+
end
|
11
|
+
it "should handle egyptian numbers" do
|
12
|
+
Phony.split('20233453756').should == ['20', '2', '33453756']
|
13
|
+
end
|
14
|
+
it "should handle french numbers" do
|
15
|
+
Phony.split('33112345678').should == ['33', '1', '12','34','56','78']
|
16
|
+
end
|
17
|
+
it "should handle german numbers" do
|
18
|
+
Phony.split('4976112345').should == ['49', '761', '123', '45']
|
19
|
+
end
|
20
|
+
it "should handle greek numbers" do
|
21
|
+
Phony.split('3022631234').should == ['30', '2263', '1234']
|
22
|
+
end
|
23
|
+
it "should handle hungarian numbers" do
|
24
|
+
Phony.split('3612345678').should == ['36', '1', '234', '5678']
|
25
|
+
end
|
26
|
+
it "should handle italian numbers" do
|
27
|
+
Phony.split('3928061371').should == ['39', '2', '806', '1371']
|
28
|
+
end
|
29
|
+
it "should handle polish numbers" do
|
30
|
+
Phony.split('48121123123').should == ['48', '12', '1', '123', '123']
|
31
|
+
end
|
32
|
+
it "should handle swiss numbers" do
|
33
|
+
Phony.split('41443643532').should == ['41', '44', '364', '35', '32']
|
34
|
+
end
|
35
|
+
it "should handle US numbers" do
|
36
|
+
Phony.split('15551115511').should == ['1', '555', '111', '5511']
|
37
|
+
end
|
38
|
+
it "should handle new zealand numbers" do
|
39
|
+
Phony.split('6491234567').should == ['64', '9', '123', '4567']
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should handle french service numbers" do
|
43
|
+
Phony.split('33812345678').should == ['33', '8', '12','34','56','78']
|
44
|
+
end
|
45
|
+
it "should handle swiss service numbers" do
|
46
|
+
Phony.split('41800334455').should == ['41', '800', '334', '455']
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
7
50
|
describe "normalize" do
|
8
51
|
describe "some examples" do
|
9
52
|
it "should normalize an already normalized number" do
|
@@ -15,7 +58,7 @@ describe Phony do
|
|
15
58
|
it "should normalize a formatted number" do
|
16
59
|
Phony.normalize('+41 44 364 35 33').should == '41443643533'
|
17
60
|
end
|
18
|
-
it "should normalize a
|
61
|
+
it "should normalize a service number" do
|
19
62
|
Phony.normalize('+41 800 11 22 33').should == '41800112233'
|
20
63
|
end
|
21
64
|
it "should remove characters from the number" do
|
@@ -33,231 +76,21 @@ describe Phony do
|
|
33
76
|
it "should normalize a number with optional ndc" do
|
34
77
|
Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
|
35
78
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
describe "remove_relative_zeros" do
|
40
|
-
it "should remove an ndc zero from an almost normalized number and return it" do
|
41
|
-
in_the Phony do
|
42
|
-
remove_relative_zeros!('410443643533').should == '41443643533'
|
79
|
+
it "should normalize a number with erroneous zero inside" do
|
80
|
+
Phony.normalize('+410443643533').should == '41443643533'
|
43
81
|
end
|
44
82
|
end
|
45
83
|
end
|
46
84
|
|
47
|
-
describe "formatted_cc_ndc" do
|
48
|
-
describe "international" do
|
49
|
-
it "should return an internationally formatted cc-ndc combo" do
|
50
|
-
in_the Phony do
|
51
|
-
formatted_cc_ndc('41', '44', nil).should == '+41 44 '
|
52
|
-
end
|
53
|
-
end
|
54
|
-
it "should return an internationally formatted cc-ndc combo" do
|
55
|
-
in_the Phony do
|
56
|
-
formatted_cc_ndc('41', '44', :international_absolute).should == '+41 44 '
|
57
|
-
end
|
58
|
-
end
|
59
|
-
it "should return an internationally formatted cc-ndc combo" do
|
60
|
-
in_the Phony do
|
61
|
-
formatted_cc_ndc('41', '44', :international).should == '+41 44 '
|
62
|
-
end
|
63
|
-
end
|
64
|
-
it "should return an internationally formatted cc-ndc combo" do
|
65
|
-
in_the Phony do
|
66
|
-
formatted_cc_ndc('41', '44', :+).should == '+41 44 '
|
67
|
-
end
|
68
|
-
end
|
69
|
-
it "should return an relative internationally formatted cc-ndc combo" do
|
70
|
-
in_the Phony do
|
71
|
-
formatted_cc_ndc('41', '44', :international_relative).should == '0041 44 '
|
72
|
-
end
|
73
|
-
end
|
74
|
-
it "should return an internationally formatted cc-ndc combo (for special service number)" do
|
75
|
-
in_the Phony do
|
76
|
-
formatted_cc_ndc('41', '800', :international_absolute).should == '+41 800 '
|
77
|
-
end
|
78
|
-
end
|
79
|
-
context 'with a different space character' do
|
80
|
-
it "should return an internationally formatted cc-ndc combo (for special service number), with special space" do
|
81
|
-
in_the Phony do
|
82
|
-
formatted_cc_ndc('41', '800', :international_absolute, :-).should == '+41-800-'
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
context 'if the ndc is blank' do
|
87
|
-
it "should have only one space at the end (not two) / international" do
|
88
|
-
in_the Phony do
|
89
|
-
formatted_cc_ndc('423', '', :international).should == '+423 '
|
90
|
-
end
|
91
|
-
end
|
92
|
-
it "should have only one space at the end (not two) / international relative" do
|
93
|
-
in_the Phony do
|
94
|
-
formatted_cc_ndc('423', '', :international_relative).should == '00423 '
|
95
|
-
end
|
96
|
-
end
|
97
|
-
it "should have only one space at the end (not two) / national" do
|
98
|
-
in_the Phony do
|
99
|
-
formatted_cc_ndc('423', '', :national).should == ''
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
end
|
105
|
-
describe "national" do
|
106
|
-
it "should return a nationally formatted cc-ndc combo" do
|
107
|
-
in_the Phony do
|
108
|
-
formatted_cc_ndc('41', '44', :national).should == '044 '
|
109
|
-
end
|
110
|
-
end
|
111
|
-
it "should return a nationally formatted cc-ndc combo (for special service number)" do
|
112
|
-
in_the Phony do
|
113
|
-
formatted_cc_ndc('41', '800', :national).should == '0800 '
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
describe "local" do
|
118
|
-
it "should return a locally formatted cc-ndc combo" do
|
119
|
-
in_the Phony do
|
120
|
-
formatted_cc_ndc('41', '44', :local).should == ''
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
describe "split" do
|
127
|
-
it "should handle austrian numbers" do
|
128
|
-
Phony.split('43198110').should == ['43', '1', '98110']
|
129
|
-
end
|
130
|
-
it "should handle french numbers" do
|
131
|
-
Phony.split('33112345678').should == ['33', '1', '12','34','56','78']
|
132
|
-
end
|
133
|
-
it "should handle german numbers" do
|
134
|
-
Phony.split('4976112345').should == ['49', '761', '123', '45']
|
135
|
-
end
|
136
|
-
it "should handle italian numbers opinionatedly" do
|
137
|
-
Phony.split('3928061371').should == ['39', '280', '613', '71']
|
138
|
-
end
|
139
|
-
it "should handle swiss numbers" do
|
140
|
-
Phony.split('41443643532').should == ['41', '44', '364', '35', '32']
|
141
|
-
end
|
142
|
-
it "should handle US numbers" do
|
143
|
-
Phony.split('15551115511').should == ['1', '555', '111', '5511']
|
144
|
-
end
|
145
|
-
it "should handle new zealand numbers" do
|
146
|
-
Phony.split('6491234567').should == ['64', '9', '123', '4567']
|
147
|
-
end
|
148
|
-
it "should handle swiss special services numbers" do
|
149
|
-
Phony.split('41800334455').should == ['41', '800', '33', '44', '55']
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
describe "split_cc" do
|
154
|
-
it "should handle partial numbers" do
|
155
|
-
Phony.split_cc('4').should == ['', '']
|
156
|
-
end
|
157
|
-
it "should handle partial numbers" do
|
158
|
-
Phony.split_cc('43').should == ['43', '']
|
159
|
-
end
|
160
|
-
it "should handle partial numbers" do
|
161
|
-
Phony.split_cc('431').should == ['43', '1']
|
162
|
-
end
|
163
|
-
it "should handle partial numbers" do
|
164
|
-
Phony.split_cc('4319').should == ['43', '19']
|
165
|
-
end
|
166
|
-
it "should handle partial numbers" do
|
167
|
-
Phony.split_cc('43198').should == ['43', '198']
|
168
|
-
end
|
169
|
-
it "should handle partial numbers" do
|
170
|
-
Phony.split_cc('431981').should == ['43', '1981']
|
171
|
-
end
|
172
|
-
it "should handle partial numbers" do
|
173
|
-
Phony.split_cc('4319811').should == ['43', '19811']
|
174
|
-
end
|
175
|
-
|
176
|
-
it "should handle austrian numbers" do
|
177
|
-
Phony.split_cc('43198110').should == ['43', '198110']
|
178
|
-
end
|
179
|
-
it "should handle french numbers" do
|
180
|
-
Phony.split_cc('33112345678').should == ['33', '112345678']
|
181
|
-
end
|
182
|
-
it "should handle german numbers" do
|
183
|
-
Phony.split_cc('4976112345').should == ['49', '76112345']
|
184
|
-
end
|
185
|
-
it "should handle italian numbers opinionatedly" do
|
186
|
-
Phony.split_cc('3928061371').should == ['39', '28061371']
|
187
|
-
end
|
188
|
-
it "should handle swiss numbers" do
|
189
|
-
Phony.split_cc('41443643532').should == ['41', '443643532']
|
190
|
-
end
|
191
|
-
it "should handle swiss special services numbers" do
|
192
|
-
Phony.split_cc('41800223344').should == ['41', '800223344']
|
193
|
-
end
|
194
|
-
it "should handle US numbers" do
|
195
|
-
Phony.split_cc('15551115511').should == ['1', '5551115511']
|
196
|
-
end
|
197
|
-
it "should handle new zealand numbers" do
|
198
|
-
Phony.split_cc('6491234567').should == ['64', '91234567']
|
199
|
-
end
|
200
|
-
it 'should handle Liechtenstein' do
|
201
|
-
Phony.split_cc('4233841148').should == ['423', '3841148']
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
describe "split_cc_ndc" do
|
206
|
-
it "should handle partial numbers" do
|
207
|
-
Phony.split_cc_ndc('4').should == ['', '', '']
|
208
|
-
end
|
209
|
-
it "should handle partial numbers" do
|
210
|
-
Phony.split_cc_ndc('43').should == ['43', '', '']
|
211
|
-
end
|
212
|
-
it "should handle partial numbers" do
|
213
|
-
Phony.split_cc_ndc('431').should == ['43', '1', '']
|
214
|
-
end
|
215
|
-
it "should handle partial numbers" do
|
216
|
-
Phony.split_cc_ndc('4319').should == ['43', '1', '9']
|
217
|
-
end
|
218
|
-
it "should handle partial numbers" do
|
219
|
-
Phony.split_cc_ndc('43198').should == ['43', '1', '98']
|
220
|
-
end
|
221
|
-
it "should handle partial numbers" do
|
222
|
-
Phony.split_cc_ndc('431981').should == ['43', '1', '981']
|
223
|
-
end
|
224
|
-
it "should handle partial numbers" do
|
225
|
-
Phony.split_cc_ndc('4319811').should == ['43', '1', '9811']
|
226
|
-
end
|
227
|
-
|
228
|
-
it "should handle austrian numbers" do
|
229
|
-
Phony.split_cc_ndc('43198110').should == ['43', '1', '98110']
|
230
|
-
end
|
231
|
-
it "should handle french numbers" do
|
232
|
-
Phony.split_cc_ndc('33112345678').should == ['33', '1', '12345678']
|
233
|
-
end
|
234
|
-
it "should handle german numbers" do
|
235
|
-
Phony.split_cc_ndc('4976112345').should == ['49', '761', '12345']
|
236
|
-
end
|
237
|
-
it "should handle italian numbers opinionatedly" do
|
238
|
-
Phony.split_cc_ndc('3928061371').should == ['39', '280', '61371']
|
239
|
-
end
|
240
|
-
it "should handle swiss numbers" do
|
241
|
-
Phony.split_cc_ndc('41443643532').should == ['41', '44', '3643532']
|
242
|
-
end
|
243
|
-
it "should handle swiss special services numbers" do
|
244
|
-
Phony.split_cc_ndc('41800112233').should == ['41', '800', '112233']
|
245
|
-
end
|
246
|
-
it "should handle US numbers" do
|
247
|
-
Phony.split_cc_ndc('15551115511').should == ['1', '555', '1115511']
|
248
|
-
end
|
249
|
-
it "should handle new zealand numbers" do
|
250
|
-
Phony.split_cc_ndc('6491234567').should == ['64', '9', '1234567']
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
85
|
describe "formatted" do
|
255
86
|
describe "default" do
|
256
87
|
it "should format swiss numbers" do
|
257
88
|
Phony.formatted('41443643532').should == '+41 44 364 35 32'
|
258
89
|
end
|
259
|
-
|
260
|
-
|
90
|
+
# TODO
|
91
|
+
#
|
92
|
+
it "should format swiss service numbers" do
|
93
|
+
Phony.formatted('41800112233').should == '+41 800 112 233'
|
261
94
|
end
|
262
95
|
it "should format austrian numbers" do
|
263
96
|
Phony.formatted('43198110').should == '+43 1 98110'
|
@@ -333,8 +166,10 @@ describe Phony do
|
|
333
166
|
it "should format swiss numbers" do
|
334
167
|
Phony.formatted('41443643532', :format => :national).should == '044 364 35 32'
|
335
168
|
end
|
336
|
-
|
337
|
-
|
169
|
+
# TODO
|
170
|
+
#
|
171
|
+
it "should format swiss service numbers" do
|
172
|
+
Phony.formatted('41800112233', :format => :national).should == '0800 112 233'
|
338
173
|
end
|
339
174
|
it "should format austrian numbers" do
|
340
175
|
Phony.formatted('43198110', :format => :national).should == '01 98110'
|
@@ -350,30 +185,74 @@ describe Phony do
|
|
350
185
|
end
|
351
186
|
end
|
352
187
|
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
188
|
+
context "speed" do
|
189
|
+
before(:each) do
|
190
|
+
@phone_numbers = [
|
191
|
+
'41443643532',
|
192
|
+
'18091231234',
|
193
|
+
'43198110',
|
194
|
+
'33142278186',
|
195
|
+
'4233841148'
|
196
|
+
]
|
360
197
|
end
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
198
|
+
describe 'split' do
|
199
|
+
it 'is fast' do
|
200
|
+
number = @phone_numbers.first
|
201
|
+
performance_of { Phony.split(number) }.should < 0.00005
|
202
|
+
end
|
203
|
+
it 'is fast' do
|
204
|
+
performance_of { @phone_numbers.each { |number| Phony.split(number) } }.should < 0.00015
|
205
|
+
end
|
206
|
+
end
|
207
|
+
describe 'normalize' do
|
208
|
+
it 'is fast' do
|
209
|
+
number = @phone_numbers.first
|
210
|
+
performance_of { Phony.normalize(number) }.should < 0.0001
|
365
211
|
end
|
366
|
-
it
|
367
|
-
Phony
|
368
|
-
Phony.vanity_to_number @number
|
212
|
+
it 'is fast' do
|
213
|
+
performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00015
|
369
214
|
end
|
370
215
|
end
|
216
|
+
describe 'formatted' do
|
217
|
+
it 'is fast' do
|
218
|
+
number = @phone_numbers.first
|
219
|
+
performance_of { Phony.formatted(number) }.should < 0.000075
|
220
|
+
end
|
221
|
+
it 'is fast' do
|
222
|
+
performance_of { @phone_numbers.each { |number| Phony.formatted(number) } }.should < 0.00015
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
# describe "service?" do
|
228
|
+
#
|
229
|
+
# end
|
230
|
+
# describe "mobile?" do
|
231
|
+
# # TODO Test dirty numbers.
|
232
|
+
# #
|
233
|
+
# it 'is correct' do
|
234
|
+
# Phony.mobile?('49172123456').should == true
|
235
|
+
# end
|
236
|
+
# it 'is correct' do
|
237
|
+
# Phony.mobile?('12172123456').should == false
|
238
|
+
# end
|
239
|
+
# end
|
240
|
+
|
241
|
+
describe 'vanity' do
|
242
|
+
describe 'vanity_number?' do
|
243
|
+
it {Phony.vanity?('41800 WEGGLI').should be_true}
|
244
|
+
it {Phony.vanity?('41800WEGGLI').should be_true}
|
245
|
+
it {Phony.vanity?('41848 SUCCESSMATCH').should be_true}
|
246
|
+
it {Phony.vanity?('4180 NO NO NO').should be_false}
|
247
|
+
it {Phony.vanity?('41900 KURZ').should be_false}
|
248
|
+
it {Phony.vanity?('41 44 364 35 32').should be_false}
|
249
|
+
end
|
371
250
|
|
372
|
-
describe '
|
373
|
-
it {Phony
|
374
|
-
it {Phony
|
375
|
-
it {Phony
|
376
|
-
it {Phony
|
251
|
+
describe 'vanity_to_number' do
|
252
|
+
it {Phony.vanity_to_number('41800WEGGLI').should == '41800934454'}
|
253
|
+
it {Phony.vanity_to_number('41800weggli').should == '41800934454'}
|
254
|
+
it {Phony.vanity_to_number('41800SUCCESSMATCH').should == '41800782237'} # Cut off according to the swiss norms.
|
255
|
+
it {Phony.vanity_to_number('4180BLA').should == '4180252'} # Does not check for validity of number.
|
377
256
|
end
|
378
257
|
end
|
379
258
|
|