phony 1.3.5 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/countries.rb +29 -24
- data/lib/phony.rb +52 -46
- data/spec/lib/phony_spec.rb +43 -37
- metadata +2 -2
data/lib/countries.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
# All countries, ordered by country code.
|
2
2
|
#
|
3
|
+
# Definitions are in the format:
|
4
|
+
# NDC >> National | NDC >> National | # ...
|
5
|
+
#
|
6
|
+
# As soon as a NDC matches, it goes on to the National part. Then breaks off.
|
7
|
+
# If the NDC does not match, it go on to the next (|, or "or") NDC.
|
8
|
+
#
|
3
9
|
# Available matching/splitting methods:
|
4
|
-
# * fixed: Always splits off a fixed length ndc. (Always use last in a | chain)
|
5
10
|
# * none: Does not have a national destination code, e.g. Denmark, Iceland.
|
6
11
|
# * one_of: Matches one of the following numbers. Splits if it does.
|
7
|
-
# Options:
|
8
|
-
# * max_length: Will try to match up to length max_length, then stop.
|
9
|
-
# (Only use one_of with this option when last in a | chain)
|
10
12
|
# * match: Try to match the regex, and if it matches, splits it off.
|
11
|
-
#
|
12
|
-
# * on_fail_take: If it does not match, take n digits, then stop.
|
13
|
-
# (When you use this option, match must be last in a | chain)
|
13
|
+
# * fixed: Always splits off a fixed length ndc. (Always use last in a | chain)
|
14
14
|
#
|
15
15
|
# For the national number part, there are two:
|
16
16
|
# * split: Use this number group splitting.
|
@@ -24,8 +24,9 @@ Phony.define do
|
|
24
24
|
country '1', fixed(3) >> split(3,4) # USA, Canada, etc.
|
25
25
|
country '7', fixed(3) >> split(3,2,2) # Kazakhstan (Republic of) & Russian Federation
|
26
26
|
|
27
|
-
country '20', one_of('800')
|
28
|
-
one_of('2', '3'
|
27
|
+
country '20', one_of('800') >> split(7) | # Egypt
|
28
|
+
one_of('2', '3') >> split(8) | # Cairo/Giza, Alexandria
|
29
|
+
fixed(2) >> split(8)
|
29
30
|
# :mobile? => /^10|11|12|14|16|17|18|19*$/, :service? => /^800.*$/
|
30
31
|
country '27', fixed(2) >> split(3,4) # South Africa
|
31
32
|
|
@@ -55,13 +56,15 @@ Phony.define do
|
|
55
56
|
country '48', fixed(3) >> split(3,3) # Poland
|
56
57
|
# country '49', Phony::Countries::Germany
|
57
58
|
|
58
|
-
country '51', one_of('103', '105')
|
59
|
-
one_of('1', '9'
|
59
|
+
country '51', one_of('103', '105') >> split(3,3) | # Peru
|
60
|
+
one_of('1', '9') >> split(4,4) | # Lima and mobile.
|
61
|
+
fixed(2) >> split(4,4)
|
60
62
|
country '52', match(/^(0\d{2})\d+$/) >> split(2,2,2,2) | # Mexico
|
61
63
|
match(/^(33|55|81)\d+$/) >> split(2,2,2,2) |
|
62
64
|
match(/^(\d{3})\d+$/) >> split(3,2,2)
|
63
|
-
country '53', match(/^(5\d{3})\d+$/)
|
64
|
-
match(/^(7|21|22|23|4[1-8]|3[1-3])
|
65
|
+
country '53', match(/^(5\d{3})\d+$/) >> split(4) | # Cuba. Mobile.
|
66
|
+
match(/^(7|21|22|23|4[1-8]|3[1-3])/) >> split(7) | #
|
67
|
+
fixed(3) >> split(7)
|
65
68
|
country '54', fixed(2) >> split(3,2,2) # TODO Argentine Republic
|
66
69
|
brazilian_service = /^(100|128|190|191|192|193|194|197|198|199)\d+$/
|
67
70
|
country '55', match(brazilian_service) >> split(3,3) | # Brazil (Federative Republic of)
|
@@ -106,8 +109,6 @@ Phony.define do
|
|
106
109
|
country '218', fixed(2) >> split(3,2,2) # Lybia
|
107
110
|
country '219', fixed(2) >> split(3,2,2) # -
|
108
111
|
|
109
|
-
# TODO From here on.
|
110
|
-
|
111
112
|
country '220', fixed(2) >> split(3,2,2) # Gambia
|
112
113
|
country '221', fixed(2) >> split(3,2,2) # Senegal
|
113
114
|
country '222', fixed(2) >> split(3,2,2) # Mauritania
|
@@ -186,18 +187,20 @@ Phony.define do
|
|
186
187
|
country '299', fixed(2) >> split(3,2,2) # Greenland
|
187
188
|
|
188
189
|
country '350', fixed(2) >> split(3,2,2) # Gibraltar
|
189
|
-
country '351', one_of('700', '800')
|
190
|
-
match(/^(9\d)\d+$/)
|
191
|
-
one_of('21', '22'
|
190
|
+
country '351', one_of('700', '800') >> split(3,3) | # Portugal
|
191
|
+
match(/^(9\d)\d+$/) >> split(3,4) | # mobile
|
192
|
+
one_of('21', '22') >> split(3,4) | # Lisboa & Porto
|
193
|
+
fixed(3) >> split(3,4)
|
192
194
|
country '352', fixed(2) >> split(3,2,2) # Luxembourg
|
193
195
|
country '353', fixed(2) >> split(3,2,2) # Ireland (0-3-4)
|
194
196
|
country '354', none >> split(3,4) # Iceland
|
195
197
|
country '355', fixed(2) >> split(3,2,2) # Albania
|
196
198
|
country '356', fixed(2) >> split(3,2,2) # Malta
|
197
199
|
country '357', fixed(2) >> split(3,2,2) # Cyprus
|
198
|
-
country '358', match(/^([6-8]00)\d+$/)
|
199
|
-
match(/^(4\d|50)\d+$/)
|
200
|
-
one_of('2','3','5','6','8','9'
|
200
|
+
country '358', match(/^([6-8]00)\d+$/) >> split(3,3) | # Finland
|
201
|
+
match(/^(4\d|50)\d+$/) >> split(3,2,2) |
|
202
|
+
one_of('2','3','5','6','8','9') >> split(3,3) |
|
203
|
+
fixed(2) >> split(3,3)
|
201
204
|
country '359', fixed(2) >> split(3,2,2) # Bulgaria
|
202
205
|
|
203
206
|
country '370', one_of('700', '800') >> split(2,3) | # Lithuania
|
@@ -220,15 +223,17 @@ Phony.define do
|
|
220
223
|
country '382', fixed(2) >> split(3,2,2) # -
|
221
224
|
country '383', fixed(2) >> split(3,2,2) # -
|
222
225
|
country '384', fixed(2) >> split(3,2,2) # -
|
223
|
-
country '385', one_of('1'
|
226
|
+
country '385', one_of('1') >> split(3,5) | # Croatia
|
227
|
+
fixed(2) >> split(3,5)
|
224
228
|
country '386', fixed(2) >> split(3,2,2) # Slovenia
|
225
229
|
country '387', fixed(2) >> split(3,2,2) # Bosnia and Herzegovina
|
226
230
|
country '388', fixed(2) >> split(3,2,2) # Group of countries, shared code
|
227
231
|
country '389', fixed(2) >> split(3,2,2) # The Former Yugoslav Republic of Macedonia
|
228
232
|
|
229
233
|
country '420', fixed(3) >> split(3,3) # Czech Republic
|
230
|
-
country '421', match(/^(9\d\d).+$/) >> split(
|
231
|
-
one_of('2'
|
234
|
+
country '421', match(/^(9\d\d).+$/) >> split(6) | # Slovak Republic
|
235
|
+
one_of('2') >> split(8) | # Bratislava
|
236
|
+
fixed(2) >> split(7)
|
232
237
|
country '422', fixed(2) >> split(3,2,2) # Spare code
|
233
238
|
country '423', none >> split(3,2,2) # Liechtenstein (Principality of)
|
234
239
|
country '424', fixed(2) >> split(3,2,2) # -
|
data/lib/phony.rb
CHANGED
@@ -44,57 +44,63 @@ module Phony
|
|
44
44
|
#
|
45
45
|
@codes = CountryCodes.instance
|
46
46
|
|
47
|
-
|
48
|
-
#
|
49
|
-
# Useful before inserting the number into a database.
|
50
|
-
#
|
51
|
-
def self.normalize phone_number
|
52
|
-
normalize! phone_number.dup
|
53
|
-
end
|
54
|
-
def self.normalize! phone_number
|
55
|
-
@codes.normalize phone_number
|
56
|
-
end
|
47
|
+
class << self
|
57
48
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
49
|
+
# Normalizes the given number.
|
50
|
+
#
|
51
|
+
# Useful before inserting the number into a database.
|
52
|
+
#
|
53
|
+
def normalize phone_number
|
54
|
+
normalize! phone_number.dup
|
55
|
+
end
|
56
|
+
def normalize! phone_number
|
57
|
+
@codes.normalize phone_number
|
58
|
+
end
|
66
59
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
60
|
+
# Splits the phone number into pieces according to the country codes.
|
61
|
+
#
|
62
|
+
def split phone_number
|
63
|
+
split! phone_number.dup
|
64
|
+
end
|
65
|
+
def split! phone_number
|
66
|
+
@codes.split phone_number
|
67
|
+
end
|
75
68
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
69
|
+
# Formats a E164 number according to local customs.
|
70
|
+
#
|
71
|
+
def format phone_number, options = {}
|
72
|
+
formatted! phone_number.dup, options
|
73
|
+
end
|
74
|
+
def format! phone_number, options = {}
|
75
|
+
@codes.formatted phone_number, options
|
76
|
+
end
|
77
|
+
alias formatted format
|
78
|
+
alias formatted! format!
|
85
79
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
@codes.
|
91
|
-
|
80
|
+
# def service? number
|
81
|
+
# @codes.service? number.dup
|
82
|
+
# end
|
83
|
+
# def mobile? number
|
84
|
+
# @codes.mobile? number.dup
|
85
|
+
# end
|
86
|
+
# def landline? number
|
87
|
+
# @codes.landline? number.dup
|
88
|
+
# end
|
89
|
+
|
90
|
+
# Returns true if there is a character in the number
|
91
|
+
# after the first four numbers.
|
92
|
+
#
|
93
|
+
def vanity? phone_number
|
94
|
+
@codes.vanity? phone_number.dup
|
95
|
+
end
|
96
|
+
|
97
|
+
# Converts any character in the vanity_number to its numeric representation.
|
98
|
+
# Does not check if the passed number is a valid vanity_number, simply does replacement.
|
99
|
+
#
|
100
|
+
def vanity_to_number vanity_number
|
101
|
+
@codes.vanity_to_number vanity_number.dup
|
102
|
+
end
|
92
103
|
|
93
|
-
# Converts any character in the vanity_number to its numeric representation.
|
94
|
-
# Does not check if the passed number is a valid vanity_number, simply does replacement.
|
95
|
-
#
|
96
|
-
def self.vanity_to_number vanity_number
|
97
|
-
@codes.vanity_to_number vanity_number.dup
|
98
104
|
end
|
99
105
|
|
100
106
|
end
|
data/spec/lib/phony_spec.rb
CHANGED
@@ -9,10 +9,10 @@ describe Phony do
|
|
9
9
|
it "should normalize an already normalized number" do
|
10
10
|
Phony.normalize('41443643533').should == '41443643533'
|
11
11
|
end
|
12
|
-
it "should normalize a
|
12
|
+
it "should normalize a format number" do
|
13
13
|
Phony.normalize('+41 44 364 35 33').should == '41443643533'
|
14
14
|
end
|
15
|
-
it "should normalize a
|
15
|
+
it "should normalize a format number" do
|
16
16
|
Phony.normalize('+41 44 364 35 33').should == '41443643533'
|
17
17
|
end
|
18
18
|
it "should normalize a service number" do
|
@@ -42,105 +42,111 @@ describe Phony do
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
describe
|
45
|
+
describe 'formatted' do
|
46
|
+
it 'is an alias of format' do
|
47
|
+
Phony.formatted('41443643532').should == '+41 44 364 35 32'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "format" do
|
46
52
|
describe "default" do
|
47
53
|
it "should format swiss numbers" do
|
48
|
-
Phony.
|
54
|
+
Phony.format('41443643532').should == '+41 44 364 35 32'
|
49
55
|
end
|
50
56
|
# TODO
|
51
57
|
#
|
52
58
|
it "should format swiss service numbers" do
|
53
|
-
Phony.
|
59
|
+
Phony.format('41800112233').should == '+41 800 112 233'
|
54
60
|
end
|
55
61
|
it "should format austrian numbers" do
|
56
|
-
Phony.
|
62
|
+
Phony.format('43198110').should == '+43 1 98110'
|
57
63
|
end
|
58
64
|
it "should format american numbers" do
|
59
|
-
Phony.
|
65
|
+
Phony.format('18705551122').should == '+1 870 555 1122'
|
60
66
|
end
|
61
67
|
end
|
62
68
|
describe "international" do
|
63
69
|
it "should format north american numbers" do
|
64
|
-
Phony.
|
70
|
+
Phony.format('18091231234', :format => :international).should == '+1 809 123 1234'
|
65
71
|
end
|
66
72
|
it "should format austrian numbers" do
|
67
|
-
Phony.
|
73
|
+
Phony.format('43198110', :format => :international).should == '+43 1 98110'
|
68
74
|
end
|
69
75
|
it "should format austrian numbers" do
|
70
|
-
Phony.
|
76
|
+
Phony.format('43198110', :format => :international_absolute).should == '+43 1 98110'
|
71
77
|
end
|
72
78
|
it "should format french numbers" do
|
73
|
-
Phony.
|
79
|
+
Phony.format('33142278186', :format => :+).should == '+33 1 42 27 81 86'
|
74
80
|
end
|
75
81
|
it "should format austrian numbers" do
|
76
|
-
Phony.
|
82
|
+
Phony.format('43198110', :format => :international_relative).should == '0043 1 98110'
|
77
83
|
end
|
78
84
|
it 'should format liechtensteiner numbers' do
|
79
|
-
Phony.
|
85
|
+
Phony.format('4233841148', :format => :international_relative).should == '00423 384 11 48'
|
80
86
|
end
|
81
87
|
context 'with no spaces' do
|
82
88
|
it "should format north american numbers" do
|
83
|
-
Phony.
|
89
|
+
Phony.format('18091231234', :format => :international, :spaces => '').should == '+18091231234'
|
84
90
|
end
|
85
91
|
it "should format austrian numbers" do
|
86
|
-
Phony.
|
92
|
+
Phony.format('43198110', :format => :international, :spaces => '').should == '+43198110'
|
87
93
|
end
|
88
94
|
it "should format austrian numbers" do
|
89
|
-
Phony.
|
95
|
+
Phony.format('43198110', :format => :international_absolute, :spaces => '').should == '+43198110'
|
90
96
|
end
|
91
97
|
it "should format french numbers" do
|
92
|
-
Phony.
|
98
|
+
Phony.format('33142278186', :format => :+, :spaces => '').should == '+33142278186'
|
93
99
|
end
|
94
100
|
it "should format austrian numbers" do
|
95
|
-
Phony.
|
101
|
+
Phony.format('43198110', :format => :international_relative, :spaces => '').should == '0043198110'
|
96
102
|
end
|
97
103
|
it 'should format liechtensteiner numbers' do
|
98
|
-
Phony.
|
104
|
+
Phony.format('4233841148', :format => :international_relative, :spaces => '').should == '004233841148'
|
99
105
|
end
|
100
106
|
end
|
101
107
|
context 'with special spaces' do
|
102
108
|
it "should format swiss numbers" do
|
103
|
-
Phony.
|
109
|
+
Phony.format('41443643532', :format => :international).should == '+41 44 364 35 32'
|
104
110
|
end
|
105
111
|
it "should format north american numbers" do
|
106
|
-
Phony.
|
112
|
+
Phony.format('18091231234', :format => :international, :spaces => :-).should == '+1-809-123-1234'
|
107
113
|
end
|
108
114
|
it "should format austrian numbers" do
|
109
|
-
Phony.
|
115
|
+
Phony.format('43198110', :format => :international, :spaces => :-).should == '+43-1-98110'
|
110
116
|
end
|
111
117
|
it "should format austrian numbers" do
|
112
|
-
Phony.
|
118
|
+
Phony.format('43198110', :format => :international_absolute, :spaces => :-).should == '+43-1-98110'
|
113
119
|
end
|
114
120
|
it "should format french numbers" do
|
115
|
-
Phony.
|
121
|
+
Phony.format('33142278186', :format => :+, :spaces => :-).should == '+33-1-42-27-81-86'
|
116
122
|
end
|
117
123
|
it "should format austrian numbers" do
|
118
|
-
Phony.
|
124
|
+
Phony.format('43198110', :format => :international_relative, :spaces => :-).should == '0043-1-98110'
|
119
125
|
end
|
120
126
|
it 'should format liechtensteiner numbers' do
|
121
|
-
Phony.
|
127
|
+
Phony.format('4233841148', :format => :international_relative, :spaces => :-).should == '00423-384-11-48'
|
122
128
|
end
|
123
129
|
end
|
124
130
|
end
|
125
131
|
describe "national" do
|
126
132
|
it "should format swiss numbers" do
|
127
|
-
Phony.
|
133
|
+
Phony.format('41443643532', :format => :national).should == '044 364 35 32'
|
128
134
|
end
|
129
135
|
# TODO
|
130
136
|
#
|
131
137
|
it "should format swiss service numbers" do
|
132
|
-
Phony.
|
138
|
+
Phony.format('41800112233', :format => :national).should == '0800 112 233'
|
133
139
|
end
|
134
140
|
it "should format austrian numbers" do
|
135
|
-
Phony.
|
141
|
+
Phony.format('43198110', :format => :national).should == '01 98110'
|
136
142
|
end
|
137
143
|
end
|
138
144
|
describe "local" do
|
139
145
|
it "should format swiss numbers" do
|
140
|
-
Phony.
|
146
|
+
Phony.format('41443643532', :format => :local).should == '364 35 32'
|
141
147
|
end
|
142
148
|
it "should format german numbers" do
|
143
|
-
Phony.
|
149
|
+
Phony.format('493038625454', :format => :local).should == '386 25454'
|
144
150
|
end
|
145
151
|
end
|
146
152
|
end
|
@@ -164,13 +170,13 @@ describe Phony do
|
|
164
170
|
end
|
165
171
|
context 'formatting' do
|
166
172
|
it 'handles completely missing numbers well enough' do
|
167
|
-
Phony.
|
173
|
+
Phony.format('4144').should == '+41 44 '
|
168
174
|
end
|
169
175
|
it 'handles a missing number part' do
|
170
|
-
Phony.
|
176
|
+
Phony.format('4144364').should == '+41 44 364'
|
171
177
|
end
|
172
178
|
it 'handles a missing number part' do
|
173
|
-
Phony.
|
179
|
+
Phony.format('414436435').should == '+41 44 364 35'
|
174
180
|
end
|
175
181
|
end
|
176
182
|
end
|
@@ -203,13 +209,13 @@ describe Phony do
|
|
203
209
|
performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00016
|
204
210
|
end
|
205
211
|
end
|
206
|
-
describe '
|
212
|
+
describe 'format' do
|
207
213
|
it 'is fast' do
|
208
214
|
number = @phone_numbers.first
|
209
|
-
performance_of { Phony.
|
215
|
+
performance_of { Phony.format(number) }.should < 0.000075
|
210
216
|
end
|
211
217
|
it 'is fast' do
|
212
|
-
performance_of { @phone_numbers.each { |number| Phony.
|
218
|
+
performance_of { @phone_numbers.each { |number| Phony.format(number) } }.should < 0.00016
|
213
219
|
end
|
214
220
|
end
|
215
221
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: phony
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Florian Hanke
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-02 00:00:00 +10:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|