phony 2.12.11 → 2.12.12
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.
- checksums.yaml +4 -4
- data/README.textile +1 -0
- data/lib/phony.rb +119 -6
- data/lib/phony/countries.rb +19 -0
- data/lib/phony/countries/austria.rb +3 -3
- data/lib/phony/country_codes.rb +11 -4
- data/lib/phony/dsl.rb +75 -57
- data/lib/phony/national_splitters/default.rb +20 -0
- data/lib/phony/national_splitters/dsl.rb +1 -6
- data/lib/phony/national_splitters/variable.rb +3 -1
- data/spec/functional/plausibility_spec.rb +6 -1
- data/spec/lib/phony/countries_spec.rb +1 -0
- data/spec/lib/phony/national_splitters/default_spec.rb +34 -0
- data/spec/lib/phony/national_splitters/fixed_spec.rb +6 -0
- data/spec/lib/phony/national_splitters/none_spec.rb +13 -3
- data/spec/lib/phony/national_splitters/variable_spec.rb +6 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0413986973e6c4d3f359a59ae739c43efc8ef2b5
|
|
4
|
+
data.tar.gz: f0cb839c8fb1f6f718a713b01e8e9c0fbc66538a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e3a65f282d2feb6c846fad5b870c759e99ed60939b649326f96ced72b253e62223624c4f3d202c1466c5e871f4f2346295f1b31ce2f182d11d36d9c978b4e306
|
|
7
|
+
data.tar.gz: 6ccbb11ac1c8590ffd4a47ef260eda590fe1c5524dfbb9e22b0be8c6a364683864b8cd09d139e5bc6ebb3a3bdc94f909d56dec7442c9e5cf648d25fff4b413ee
|
data/README.textile
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"!{float:right}https://secure.travis-ci.org/floere/phony.png!":http://travis-ci.org/floere/phony
|
|
2
|
+
!https://coveralls.io/repos/floere/phony/badge.svg?branch=master(Coverage Status)!:https://coveralls.io/r/floere/phony?branch=master
|
|
2
3
|
"!https://codeclimate.com/github/floere/phony.png!":https://codeclimate.com/github/floere/phony
|
|
3
4
|
!http://inch-ci.org/github/floere/phony.png!:http://inch-ci.org/github/floere/phony
|
|
4
5
|
|
data/lib/phony.rb
CHANGED
|
@@ -59,13 +59,21 @@ require File.expand_path '../phony/countries/ukraine', __FILE__
|
|
|
59
59
|
require File.expand_path '../phony/countries/united_kingdom', __FILE__
|
|
60
60
|
require File.expand_path '../phony/countries/uruguay', __FILE__
|
|
61
61
|
require File.expand_path '../phony/countries/zimbabwe', __FILE__
|
|
62
|
-
|
|
62
|
+
|
|
63
63
|
# All other countries.
|
|
64
64
|
#
|
|
65
65
|
require File.expand_path '../phony/countries', __FILE__
|
|
66
66
|
|
|
67
|
+
# Phony is the main module and is generally used to process
|
|
68
|
+
# E164 phone numbers directly.
|
|
69
|
+
#
|
|
67
70
|
module Phony
|
|
68
71
|
|
|
72
|
+
# Raised in case Phony can't normalize a given number.
|
|
73
|
+
#
|
|
74
|
+
# @example
|
|
75
|
+
# Phony.normalize("Fnork!") # Raises a Phony::NormalizationError.
|
|
76
|
+
#
|
|
69
77
|
class NormalizationError < StandardError
|
|
70
78
|
def initialize
|
|
71
79
|
super %Q{Phony could not normalize the given number. Is it a phone number?}
|
|
@@ -80,22 +88,56 @@ module Phony
|
|
|
80
88
|
|
|
81
89
|
# Get the Country for the given CC.
|
|
82
90
|
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
91
|
+
# @param [String] cc A valid country code.
|
|
92
|
+
#
|
|
93
|
+
# @return [Country] for the given CC.
|
|
94
|
+
#
|
|
95
|
+
# @example Country for the NANP (includes the US)
|
|
96
|
+
# nanp = Phony['1']
|
|
97
|
+
# normalized_number = nanp.normalize number
|
|
86
98
|
#
|
|
87
99
|
def [] cc
|
|
88
100
|
@codes[cc]
|
|
89
101
|
end
|
|
90
102
|
|
|
91
|
-
# Normalizes the given number.
|
|
103
|
+
# Normalizes the given number into a digits-only String.
|
|
92
104
|
#
|
|
93
105
|
# Useful before inserting the number into a database.
|
|
94
106
|
#
|
|
107
|
+
# @param [String] phone_number An E164 number.
|
|
108
|
+
# @param [Hash] options An options hash (With :cc as the only used key).
|
|
109
|
+
#
|
|
110
|
+
# @return [String] A normalized E164 number.
|
|
111
|
+
#
|
|
112
|
+
# @raise [Phony::NormalizationError] If phony can't normalize the given number.
|
|
113
|
+
#
|
|
114
|
+
# @example Normalize a Swiss number.
|
|
115
|
+
# Phony.normalize("+41 (044) 123 45 67") # => "41441234567"
|
|
116
|
+
#
|
|
117
|
+
# @example Normalize a phone number assuming it's a NANP number.
|
|
118
|
+
# Phony.normalize("301 555 0100", cc: '1') # => "13015550100"
|
|
119
|
+
#
|
|
95
120
|
def normalize phone_number, options = {}
|
|
96
121
|
raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number)." unless phone_number
|
|
97
122
|
normalize! phone_number.dup, options
|
|
98
123
|
end
|
|
124
|
+
# A destructive version of {#normalize}.
|
|
125
|
+
#
|
|
126
|
+
# @see #normalize
|
|
127
|
+
#
|
|
128
|
+
# @param [String] phone_number An E164 number.
|
|
129
|
+
# @param [Hash] options An options hash (With :cc as the only used key).
|
|
130
|
+
#
|
|
131
|
+
# @return [String] The normalized E164 number.
|
|
132
|
+
#
|
|
133
|
+
# @raise [Phony::NormalizationError] If phony can't normalize the given number.
|
|
134
|
+
#
|
|
135
|
+
# @example Normalize a Swiss number.
|
|
136
|
+
# Phony.normalize!("+41 (044) 123 45 67") # => "41441234567"
|
|
137
|
+
#
|
|
138
|
+
# @example Normalize a phone number assuming it's a NANP number.
|
|
139
|
+
# Phony.normalize!("301 555 0100", cc: '1') # => "13015550100"
|
|
140
|
+
#
|
|
99
141
|
def normalize! phone_number, options = {}
|
|
100
142
|
@codes.normalize phone_number, options
|
|
101
143
|
rescue
|
|
@@ -104,22 +146,86 @@ module Phony
|
|
|
104
146
|
|
|
105
147
|
# Splits the phone number into pieces according to the country codes.
|
|
106
148
|
#
|
|
149
|
+
# Useful for manually processing the CC, NDC, and local pieces.
|
|
150
|
+
#
|
|
151
|
+
# @param [String] phone_number An E164 number.
|
|
152
|
+
#
|
|
153
|
+
# @return [Array<String>] The pieces of a phone number.
|
|
154
|
+
#
|
|
155
|
+
# @example Split a Swiss number.
|
|
156
|
+
# Phony.split("41441234567") # => ["41", "44", "123", "45", "67"]
|
|
157
|
+
#
|
|
158
|
+
# @example Split a NANP number.
|
|
159
|
+
# Phony.split("13015550100") # => ["1", "301", "555", "0100"]
|
|
160
|
+
#
|
|
107
161
|
def split phone_number
|
|
108
162
|
raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.split(number)." unless phone_number
|
|
109
163
|
split! phone_number.dup
|
|
110
164
|
end
|
|
165
|
+
# A destructive version of {#split}.
|
|
166
|
+
#
|
|
167
|
+
# @see #split
|
|
168
|
+
#
|
|
169
|
+
# @param [String] phone_number An E164 number.
|
|
170
|
+
#
|
|
171
|
+
# @return [Array<String>] The pieces of the phone number.
|
|
172
|
+
#
|
|
173
|
+
# @example Split a Swiss number.
|
|
174
|
+
# Phony.split!("41441234567") # => ["41", "44", "123", "45", "67"]
|
|
175
|
+
#
|
|
176
|
+
# @example Split a NANP number.
|
|
177
|
+
# Phony.split!("13015550100") # => ["1", "301", "555", "0100"]
|
|
178
|
+
#
|
|
111
179
|
def split! phone_number
|
|
112
180
|
parts = @codes.split phone_number
|
|
113
181
|
parts.delete_at 1
|
|
114
182
|
parts
|
|
115
183
|
end
|
|
116
184
|
|
|
117
|
-
# Formats a E164 number according to
|
|
185
|
+
# Formats a normalized E164 number according to a country's formatting scheme.
|
|
186
|
+
#
|
|
187
|
+
# Absolutely needs a normalized E164 number.
|
|
188
|
+
#
|
|
189
|
+
# @param [String] phone_number A normalized E164 number.
|
|
190
|
+
# @param [Hash] options See the README for a list of options.
|
|
191
|
+
#
|
|
192
|
+
# @return [Array<String>] The pieces of a phone number.
|
|
193
|
+
#
|
|
194
|
+
# @example Format a Swiss number.
|
|
195
|
+
# Phony.format("41441234567") # => "+41 44 123 45 67"
|
|
196
|
+
#
|
|
197
|
+
# @example Format a NANP number.
|
|
198
|
+
# Phony.format("13015550100") # => "+1 301 555 0100"
|
|
199
|
+
#
|
|
200
|
+
# @example Format a NANP number in local format.
|
|
201
|
+
# Phony.format("13015550100", :format => :local) # => "555 0100"
|
|
118
202
|
#
|
|
119
203
|
def format phone_number, options = {}
|
|
120
204
|
raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.format(number)." unless phone_number
|
|
121
205
|
format! phone_number.dup, options
|
|
122
206
|
end
|
|
207
|
+
# A destructive version of {#format}.
|
|
208
|
+
#
|
|
209
|
+
# @see #format
|
|
210
|
+
#
|
|
211
|
+
# Formats a normalized E164 number according to a country's formatting scheme.
|
|
212
|
+
#
|
|
213
|
+
# Absolutely needs a normalized E164 number.
|
|
214
|
+
#
|
|
215
|
+
# @param [String] phone_number A normalized E164 number.
|
|
216
|
+
# @param [Hash] options See the README for a list of options.
|
|
217
|
+
#
|
|
218
|
+
# @return [Array<String>] The pieces of the phone number.
|
|
219
|
+
#
|
|
220
|
+
# @example Format a Swiss number.
|
|
221
|
+
# Phony.format!("41441234567") # => "+41 44 123 45 67"
|
|
222
|
+
#
|
|
223
|
+
# @example Format a NANP number.
|
|
224
|
+
# Phony.format!("13015550100") # => "+1 301 555 0100"
|
|
225
|
+
#
|
|
226
|
+
# @example Format a NANP number in local format.
|
|
227
|
+
# Phony.format!("13015550100", :format => :local) # => "555 0100"
|
|
228
|
+
#
|
|
123
229
|
def format! phone_number, options = {}
|
|
124
230
|
@codes.format phone_number, options
|
|
125
231
|
end
|
|
@@ -146,6 +252,13 @@ module Phony
|
|
|
146
252
|
# Converts any character in the vanity_number to its numeric representation.
|
|
147
253
|
# Does not check if the passed number is a valid vanity_number, simply does replacement.
|
|
148
254
|
#
|
|
255
|
+
# @param [String] vanity_number A vanity number.
|
|
256
|
+
#
|
|
257
|
+
# @return [String] The de-vanitized phone number.
|
|
258
|
+
#
|
|
259
|
+
# @example De-vanitize a number.
|
|
260
|
+
# Phony.vanity_to_number("1-800-HELLOTHERE") # => "1-800-4355684373"
|
|
261
|
+
#
|
|
149
262
|
def vanity_to_number vanity_number
|
|
150
263
|
@codes.vanity_to_number vanity_number.dup
|
|
151
264
|
end
|
data/lib/phony/countries.rb
CHANGED
|
@@ -23,6 +23,24 @@
|
|
|
23
23
|
#
|
|
24
24
|
# Note: The ones that are commented are defined in their special files.
|
|
25
25
|
#
|
|
26
|
+
# @example Switzerland (simplified)
|
|
27
|
+
# country('41', fixed(2) >> local([3,2,2]))
|
|
28
|
+
#
|
|
29
|
+
# @example Germany. Too big, we use a separate file
|
|
30
|
+
# Phony.define do
|
|
31
|
+
# country '49', match(...) >> split([...]) ||
|
|
32
|
+
# one_of([...], :max_length => 5) >> split([...])
|
|
33
|
+
# end
|
|
34
|
+
#
|
|
35
|
+
# @example Denmark
|
|
36
|
+
# country('45', none >> split([2,2,2,2]))
|
|
37
|
+
#
|
|
38
|
+
# @example Hungary
|
|
39
|
+
# country('36',
|
|
40
|
+
# match(/^104|105|107|112/) >> split([3,3]) ||
|
|
41
|
+
# one_of([1], :max_length => 2) >> split([3,4])
|
|
42
|
+
# )
|
|
43
|
+
#
|
|
26
44
|
Phony.define do
|
|
27
45
|
|
|
28
46
|
# Reserved.
|
|
@@ -336,6 +354,7 @@ Phony.define do
|
|
|
336
354
|
none >> matched_split(
|
|
337
355
|
/\A[23]\d+\z/ => [4,4], # LIBTELCO, TEMAS
|
|
338
356
|
/\A[4568]\d+\z/ => [4,3], # mobile Lonestar, Libercell, Comium Liberia Inc.
|
|
357
|
+
/\A77\d+\z/ => [2,3,4], # http://monrovia.usembassy.gov/contact.html
|
|
339
358
|
/\A7\d+\z/ => [4,4], # mobile Cellcom
|
|
340
359
|
/\A9\d+\z/ => [4,4], # premium rate Telelinks International SAL, Interactive Media Corporation
|
|
341
360
|
)
|
|
@@ -78,7 +78,7 @@ service = [
|
|
|
78
78
|
'939'
|
|
79
79
|
]
|
|
80
80
|
|
|
81
|
-
# https://www.rtr.at/en/tk/E129/
|
|
81
|
+
# https://www.rtr.at/en/tk/E129/2312_Austrian_Numbering_Plan_2011-03-30.pdf
|
|
82
82
|
#
|
|
83
83
|
# TODO Add more details.
|
|
84
84
|
#
|
|
@@ -91,5 +91,5 @@ Phony.define do
|
|
|
91
91
|
one_of('663') >> split(6..6) | # 6 digit mobile.
|
|
92
92
|
one_of(mobile) >> split(4,3..9) |
|
|
93
93
|
one_of(mobile_2digit) >> split(7..7) | # Separate as mobile contains 676 - 67 violates the prefix rule.
|
|
94
|
-
fixed(4) >> split(7..
|
|
95
|
-
end
|
|
94
|
+
fixed(4) >> split(3..9) # Number length is 7..13.
|
|
95
|
+
end
|
data/lib/phony/country_codes.rb
CHANGED
|
@@ -121,12 +121,12 @@ module Phony
|
|
|
121
121
|
format % { :cc => cc, :ndc => ndc, :local => local }
|
|
122
122
|
when nil, :international_absolute, :international, :+
|
|
123
123
|
ndc ?
|
|
124
|
-
@international_absolute_format
|
|
125
|
-
@international_absolute_format
|
|
124
|
+
format_with_ndc(@international_absolute_format, cc, ndc, local, space) :
|
|
125
|
+
format_without_ndc(@international_absolute_format, cc, local, space)
|
|
126
126
|
when :international_relative
|
|
127
127
|
ndc ?
|
|
128
|
-
@international_relative_format
|
|
129
|
-
@international_relative_format
|
|
128
|
+
format_with_ndc(@international_relative_format, cc, ndc, local, space) :
|
|
129
|
+
format_without_ndc(@international_relative_format, cc, local, space)
|
|
130
130
|
when :national
|
|
131
131
|
# Replaces the %s in the trunk code with a "space".
|
|
132
132
|
trunk = trunk % space if trunk && trunk.size > 1
|
|
@@ -141,6 +141,13 @@ module Phony
|
|
|
141
141
|
parts_ary.compact!
|
|
142
142
|
parts_ary.join local_space.to_s
|
|
143
143
|
end
|
|
144
|
+
|
|
145
|
+
def format_with_ndc format, cc, ndc, local, space
|
|
146
|
+
format % [cc, space, ndc, space, local]
|
|
147
|
+
end
|
|
148
|
+
def format_without_ndc format, cc, local, space
|
|
149
|
+
format % [cc, space, local, nil, nil]
|
|
150
|
+
end
|
|
144
151
|
|
|
145
152
|
#
|
|
146
153
|
#
|
data/lib/phony/dsl.rb
CHANGED
|
@@ -1,27 +1,3 @@
|
|
|
1
|
-
# # Switzerland (simplified):
|
|
2
|
-
# #
|
|
3
|
-
# country('41', fixed(2) >> local([3,2,2]))
|
|
4
|
-
#
|
|
5
|
-
# # Germany. Too big, we use a separate file:
|
|
6
|
-
# #
|
|
7
|
-
# Phony.define do
|
|
8
|
-
# country '49', match(...) >> split([...]) ||
|
|
9
|
-
# one_of([...], :max_length => 5) >> split([...])
|
|
10
|
-
# end
|
|
11
|
-
#
|
|
12
|
-
# # Denmark:
|
|
13
|
-
# #
|
|
14
|
-
# country('45', none >> split([2,2,2,2]))
|
|
15
|
-
#
|
|
16
|
-
# # Hungary:
|
|
17
|
-
# #
|
|
18
|
-
# country('36',
|
|
19
|
-
# match(/^104|105|107|112/) >> split([3,3]) ||
|
|
20
|
-
# one_of([1], :max_length => 2) >> split([3,4])
|
|
21
|
-
# )
|
|
22
|
-
#
|
|
23
|
-
# Note: Perhaps the DSL should operate directly on country codes.
|
|
24
|
-
#
|
|
25
1
|
module Phony
|
|
26
2
|
|
|
27
3
|
# For country definitions.
|
|
@@ -42,11 +18,26 @@ module Phony
|
|
|
42
18
|
dsl
|
|
43
19
|
end
|
|
44
20
|
|
|
21
|
+
# Contains a number of DSL methods. Is used e.g. in the countries.rb file.
|
|
22
|
+
#
|
|
23
|
+
# Do not use directly, but instead use {Phony.define} to execute a block
|
|
24
|
+
# in the DSL instance's context.
|
|
25
|
+
#
|
|
26
|
+
# @example
|
|
27
|
+
# Phony.define { ... }
|
|
28
|
+
#
|
|
45
29
|
class DSL
|
|
46
30
|
|
|
47
|
-
#
|
|
31
|
+
# Define a country's rules.
|
|
32
|
+
#
|
|
33
|
+
# Use the other DSL methods to define the country's rules.
|
|
34
|
+
#
|
|
35
|
+
# @param [String] country_code The country code of the country.
|
|
36
|
+
# @param [Phony::CountryCodes] definition Rules for this country.
|
|
37
|
+
#
|
|
38
|
+
# @return Undefined.
|
|
48
39
|
#
|
|
49
|
-
#
|
|
40
|
+
# @example Add a country with country code 27.
|
|
50
41
|
# country '27', # CC, followed by rules, for example fixed(2) >> ...
|
|
51
42
|
#
|
|
52
43
|
def country country_code, definition, options = {}
|
|
@@ -55,12 +46,25 @@ module Phony
|
|
|
55
46
|
end
|
|
56
47
|
|
|
57
48
|
# Designates a country code as reserved.
|
|
49
|
+
# A reserved country will result in an exception when trying to be used.
|
|
50
|
+
#
|
|
51
|
+
# @param [String] country_code The country code of the country.
|
|
52
|
+
#
|
|
53
|
+
# @return nil
|
|
54
|
+
#
|
|
55
|
+
# @example Designate country code 27 as reserved.
|
|
56
|
+
# reserved('27')
|
|
58
57
|
#
|
|
59
58
|
def reserved country_code
|
|
60
59
|
# Does nothing, will just fail with an exception.
|
|
61
60
|
end
|
|
62
61
|
|
|
63
|
-
#
|
|
62
|
+
# Define a country to use default rules (and to be done at some point).
|
|
63
|
+
#
|
|
64
|
+
# @return Rules for a country.
|
|
65
|
+
#
|
|
66
|
+
# @example Define country 27 to use default rules.
|
|
67
|
+
# country '27', todo
|
|
64
68
|
#
|
|
65
69
|
def todo
|
|
66
70
|
none >> split(10)
|
|
@@ -68,10 +72,19 @@ module Phony
|
|
|
68
72
|
|
|
69
73
|
# This country uses a trunk code.
|
|
70
74
|
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
+
# @param [String] code The trunk code.
|
|
76
|
+
# @param [Hash] options Options hash. Pass :normalize (true/false) to indicate whether it needs to be normalized.
|
|
77
|
+
#
|
|
78
|
+
# @return TrunkCode A trunk code handler.
|
|
79
|
+
#
|
|
80
|
+
# @example Hungary uses 06.
|
|
81
|
+
# country '36', trunk('06', normalize: false) | ...
|
|
82
|
+
#
|
|
83
|
+
# @example North America uses 1.
|
|
84
|
+
# country '1', trunk('1%s', normalize: true) | ...
|
|
85
|
+
#
|
|
86
|
+
# @example Most countries which use a trunk code use 0. E.g. Romania.
|
|
87
|
+
# country '40', trunk('0') | ...
|
|
75
88
|
#
|
|
76
89
|
def trunk code, options = {}
|
|
77
90
|
TrunkCode.new code, options
|
|
@@ -84,12 +97,12 @@ module Phony
|
|
|
84
97
|
# uses a zero prefix when formatted
|
|
85
98
|
# with format :national.
|
|
86
99
|
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
# * options: Set :zero to false to not add a zero on format :national.
|
|
100
|
+
# @param [Fixnum] length The length of the fixed length NDC.
|
|
101
|
+
# @param [Hash] options An options hash (set zero: false to not add a zero on format :national).
|
|
90
102
|
#
|
|
91
|
-
#
|
|
92
|
-
#
|
|
103
|
+
# @return NationalSplitters::Fixed A fixed length national splitter.
|
|
104
|
+
#
|
|
105
|
+
# @example France. Uses a fixed NDC of size 1.
|
|
93
106
|
# country '33', fixed(1) >> split(2,2,2,2)
|
|
94
107
|
#
|
|
95
108
|
def fixed length, options = {}
|
|
@@ -97,13 +110,11 @@ module Phony
|
|
|
97
110
|
NationalSplitters::Fixed.instance_for length, options
|
|
98
111
|
end
|
|
99
112
|
|
|
100
|
-
#
|
|
113
|
+
# Marks the country as not using an NDC. This rule will always match.
|
|
114
|
+
#
|
|
115
|
+
# @return NationalSplitters::None A no-ndc national splitter.
|
|
101
116
|
#
|
|
102
|
-
#
|
|
103
|
-
# * Denmark
|
|
104
|
-
# * Norway
|
|
105
|
-
# * Iceland
|
|
106
|
-
# (and more)
|
|
117
|
+
# @example Denmark, Norway, Iceland.
|
|
107
118
|
#
|
|
108
119
|
def none
|
|
109
120
|
NationalSplitters::None.instance_for
|
|
@@ -112,11 +123,11 @@ module Phony
|
|
|
112
123
|
# If you have a number of (possibly) variable length NDCs
|
|
113
124
|
# that cannot be well expressed via regexp, use this.
|
|
114
125
|
#
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
#
|
|
126
|
+
# @param [Array<String>] ndcs A list of NDCs of variable length. Can contain :max_length => number to denote a maximum NDC length.
|
|
127
|
+
#
|
|
128
|
+
# @return NationalSplitters::Variable A variable size national splitter.
|
|
118
129
|
#
|
|
119
|
-
#
|
|
130
|
+
# @example With two NDCs
|
|
120
131
|
# country '51',
|
|
121
132
|
# # If it's either 103 or 105, then split ...
|
|
122
133
|
# one_of('103', '105') >> split(3,3)
|
|
@@ -134,10 +145,11 @@ module Phony
|
|
|
134
145
|
# If you have a number of (possibly) variable length NDCs
|
|
135
146
|
# that can be well expressed via regexp, use this.
|
|
136
147
|
#
|
|
137
|
-
#
|
|
138
|
-
# * regex A regexp describing the NDCs (First group must contain the NDC, eg. /^(0\d{2})\d+$/) contains all NDCs with 0xx...
|
|
148
|
+
# @param [Regexp] regex A Regexp describing the NDCs (First group must contain the NDC, eg. /^(0\d{2})\d+$/) contains all NDCs with 0xx...
|
|
139
149
|
#
|
|
140
|
-
#
|
|
150
|
+
# @return NationalSplitters::Regex A regexp-based national splitter.
|
|
151
|
+
#
|
|
152
|
+
# @example With several NDC matchers.
|
|
141
153
|
# country '52',
|
|
142
154
|
# match(/^(0\d{2})\d+$/) >> split(2,2,2,2) |
|
|
143
155
|
# match(/^(33|55|81)\d+$/) >> split(2,2,2,2) |
|
|
@@ -158,7 +170,11 @@ module Phony
|
|
|
158
170
|
#
|
|
159
171
|
# Also takes ranges.
|
|
160
172
|
#
|
|
161
|
-
#
|
|
173
|
+
# @param [Array<Fixnum>] local The local group sizes to split the local number part into.
|
|
174
|
+
#
|
|
175
|
+
# @return LocalSplitters::Fixed A fixed size local splitter.
|
|
176
|
+
#
|
|
177
|
+
# @example Splitting in 4 even groups of two digits.
|
|
162
178
|
# match(/^(0\d{2})\d+$/) >> split(2,2,2,2) # If it matches, split in 4 groups of size 2.
|
|
163
179
|
#
|
|
164
180
|
def split *local
|
|
@@ -171,18 +187,18 @@ module Phony
|
|
|
171
187
|
#
|
|
172
188
|
# Also takes ranges.
|
|
173
189
|
#
|
|
174
|
-
#
|
|
175
|
-
# * fallback A fallback amount of group sizes in case it doesn't match.
|
|
190
|
+
# @param [Hash] options Can contain option :fallback A fallback amount of group sizes in case it doesn't match.
|
|
176
191
|
#
|
|
177
|
-
#
|
|
178
|
-
#
|
|
192
|
+
# @return LocalSplitters::Regex A regexp-based local splitter.
|
|
193
|
+
#
|
|
194
|
+
# @example Norway
|
|
179
195
|
# country '47',
|
|
180
196
|
# none >> matched_split(/^[1].*$/ => [3],
|
|
181
197
|
# /^[489].*$/ => [3,2,3],
|
|
182
198
|
# :fallback => [2,2,2,2])
|
|
183
199
|
#
|
|
184
200
|
def matched_split options = {}
|
|
185
|
-
|
|
201
|
+
LocalSplitters::Regex.instance_for options
|
|
186
202
|
end
|
|
187
203
|
|
|
188
204
|
# Validators
|
|
@@ -190,9 +206,11 @@ module Phony
|
|
|
190
206
|
|
|
191
207
|
# Which NDCs are explicitly invalid?
|
|
192
208
|
#
|
|
193
|
-
#
|
|
209
|
+
# @param [Regexp, String] ndc A regexp or a string of invalid NDCs.
|
|
210
|
+
#
|
|
211
|
+
# @return Validators::NDC An NDC validator
|
|
194
212
|
#
|
|
195
|
-
#
|
|
213
|
+
# @example NANP
|
|
196
214
|
# country '1',
|
|
197
215
|
# fixed(3, :zero => false) >> split(3,4),
|
|
198
216
|
# invalid_ndcs('911') # The regexp /911/ would also work.
|
|
@@ -10,10 +10,30 @@ module Phony
|
|
|
10
10
|
@instance ||= new
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
+
# "Splits" the national part of a phone number into a single piece.
|
|
14
|
+
#
|
|
15
|
+
# @param [String] national_number An national part of a number.
|
|
16
|
+
#
|
|
17
|
+
# @return [Array<String>] An Array with the given number part as its element.
|
|
18
|
+
#
|
|
19
|
+
# @example Split the national part of a Swiss number.
|
|
20
|
+
# Phony.split("1234567") # => ["1234567"]
|
|
21
|
+
#
|
|
13
22
|
def split national_number
|
|
14
23
|
[national_number]
|
|
15
24
|
end
|
|
16
25
|
|
|
26
|
+
# By default, the national part of a number is always plausible.
|
|
27
|
+
#
|
|
28
|
+
# @param [String] rest An national part of a number (ignored).
|
|
29
|
+
# @param [Fixnum] size Size (ignored).
|
|
30
|
+
# @param [Hash] hints Hints (ignored).
|
|
31
|
+
#
|
|
32
|
+
# @return [Boolean] Always true.
|
|
33
|
+
#
|
|
34
|
+
# @example Split the national part of a Swiss number.
|
|
35
|
+
# Phony.plausible?("1234567") # => true
|
|
36
|
+
#
|
|
17
37
|
def plausible? rest, size, hints = {}
|
|
18
38
|
true
|
|
19
39
|
end
|
|
@@ -9,12 +9,7 @@ module Phony
|
|
|
9
9
|
#
|
|
10
10
|
#
|
|
11
11
|
def >> local_splitter
|
|
12
|
-
|
|
13
|
-
country_for local_splitter
|
|
14
|
-
else
|
|
15
|
-
local_splitter.national_splitter = self
|
|
16
|
-
local_splitter
|
|
17
|
-
end
|
|
12
|
+
country_for local_splitter
|
|
18
13
|
end
|
|
19
14
|
|
|
20
15
|
def country_for local_splitter
|
|
@@ -18,8 +18,10 @@ module Phony
|
|
|
18
18
|
|
|
19
19
|
# Extract a starting point.
|
|
20
20
|
#
|
|
21
|
+
# This if can possibly be removed.
|
|
22
|
+
#
|
|
21
23
|
presumed_code = if @mapped_ndc_min_length > 0
|
|
22
|
-
national_number.slice!(0..@mapped_ndc_min_length-1)
|
|
24
|
+
presumed_code = national_number.slice!(0..@mapped_ndc_min_length-1)
|
|
23
25
|
else
|
|
24
26
|
''
|
|
25
27
|
end
|
|
@@ -140,6 +140,7 @@ describe 'plausibility' do
|
|
|
140
140
|
Phony.plausible?('+43 501 1234').should be_false # too short
|
|
141
141
|
Phony.plausible?('+43 501 123456').should be_false # too long
|
|
142
142
|
Phony.plausible?('+43 800 123456789').should be_true
|
|
143
|
+
Phony.plausible?('+43 3115 3307').should be_true # See issue #246 on Github.
|
|
143
144
|
|
|
144
145
|
# Mobile
|
|
145
146
|
Phony.plausible?('+43 676 0000000').should be_true
|
|
@@ -743,7 +744,11 @@ describe 'plausibility' do
|
|
|
743
744
|
'+961 90 123 456',
|
|
744
745
|
'+961 81 123 456']
|
|
745
746
|
it_is_correct_for 'Lesotho', :samples => '+266 7612 6866'
|
|
746
|
-
|
|
747
|
+
it 'is correct for Liberia' do
|
|
748
|
+
Phony.plausible?('+231 2 123 4567').should be_true
|
|
749
|
+
Phony.plausible?('+231 4 123 456').should be_true
|
|
750
|
+
Phony.plausible?('+231 77 123 4567').should be_true
|
|
751
|
+
end
|
|
747
752
|
it_is_correct_for 'Madagascar', :samples => ['+261 20 012 345 678',
|
|
748
753
|
'+261 20 124 3456',
|
|
749
754
|
'+261 512 345 678']
|
|
@@ -861,6 +861,7 @@ describe 'country descriptions' do
|
|
|
861
861
|
describe 'Liberia' do
|
|
862
862
|
it_splits '23121234567', ['231', false, '2123', '4567']
|
|
863
863
|
it_splits '2314123456', ['231', false, '4123', '456']
|
|
864
|
+
it_splits '231771234567', ['231', false, '77', '123', '4567']
|
|
864
865
|
end
|
|
865
866
|
describe 'Libya' do
|
|
866
867
|
it_splits '21820512345', %w(218 205 123 45)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Phony::NationalSplitters::Default do
|
|
4
|
+
|
|
5
|
+
describe 'instance_for' do
|
|
6
|
+
it 'caches' do
|
|
7
|
+
described_class.instance_for.should equal(described_class.instance_for)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context 'with instance' do
|
|
12
|
+
let(:splitter) { described_class.instance_for }
|
|
13
|
+
|
|
14
|
+
describe 'split' do
|
|
15
|
+
it 'does not split' do
|
|
16
|
+
splitter.split(:anything).should == [:anything]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe 'plausible?' do
|
|
21
|
+
it 'is always plausible' do
|
|
22
|
+
splitter.plausible?(:anything, :anything).should be_true
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe 'length' do
|
|
27
|
+
it 'needs to be at least 3' do
|
|
28
|
+
splitter.length.should == 3
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
@@ -24,6 +24,9 @@ describe Phony::NationalSplitters::Fixed do
|
|
|
24
24
|
it 'splits correctly even when the number is too short' do
|
|
25
25
|
@splitter.split('443').should == [nil, '44','3']
|
|
26
26
|
end
|
|
27
|
+
it 'has a length of 2' do
|
|
28
|
+
@splitter.length.should == 2
|
|
29
|
+
end
|
|
27
30
|
end
|
|
28
31
|
describe 'split' do
|
|
29
32
|
before(:each) do
|
|
@@ -38,6 +41,9 @@ describe Phony::NationalSplitters::Fixed do
|
|
|
38
41
|
it 'splits correctly even when the number is too short' do
|
|
39
42
|
@splitter.split('443').should == [nil, '443']
|
|
40
43
|
end
|
|
44
|
+
it 'has a length of nil' do
|
|
45
|
+
@splitter.length.should == nil
|
|
46
|
+
end
|
|
41
47
|
end
|
|
42
48
|
|
|
43
49
|
end
|
|
@@ -8,11 +8,21 @@ describe Phony::NationalSplitters::None do
|
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
context 'with instance' do
|
|
12
12
|
let(:splitter) { described_class.instance_for }
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
|
|
14
|
+
describe 'split' do
|
|
15
|
+
it 'splits correctly into ndc and rest' do
|
|
16
|
+
splitter.split('123456789').should == [nil, false, '123456789']
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe 'length' do
|
|
21
|
+
it 'is always 0' do
|
|
22
|
+
splitter.length.should be_zero
|
|
23
|
+
end
|
|
15
24
|
end
|
|
25
|
+
|
|
16
26
|
end
|
|
17
27
|
|
|
18
28
|
end
|
|
@@ -19,6 +19,9 @@ describe Phony::NationalSplitters::Variable do
|
|
|
19
19
|
it "handles Rohrau" do
|
|
20
20
|
@splitter.split('2164123456789').should == [nil, '2164', '123456789']
|
|
21
21
|
end
|
|
22
|
+
it 'has an NDC length of 3' do
|
|
23
|
+
@splitter.length.should == (1..3)
|
|
24
|
+
end
|
|
22
25
|
end
|
|
23
26
|
context 'special handling for using the variable size splitter for Swiss service numbers' do
|
|
24
27
|
before(:each) do
|
|
@@ -27,6 +30,9 @@ describe Phony::NationalSplitters::Variable do
|
|
|
27
30
|
it "should handle swiss service numbers" do
|
|
28
31
|
@splitter.split('800223344').should == [nil, '800', '223344']
|
|
29
32
|
end
|
|
33
|
+
it 'has an NDC length of 3' do
|
|
34
|
+
@splitter.length.should == (3..3)
|
|
35
|
+
end
|
|
30
36
|
end
|
|
31
37
|
end
|
|
32
38
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phony
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.12.
|
|
4
|
+
version: 2.12.12
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Florian Hanke
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-04-
|
|
11
|
+
date: 2015-04-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: 'Fast international phone number (E164 standard) normalizing, splitting
|
|
14
14
|
and formatting. Lots of formatting options: International (+.., 00..), national
|
|
@@ -86,6 +86,7 @@ files:
|
|
|
86
86
|
- spec/lib/phony/local_splitters/fixed_spec.rb
|
|
87
87
|
- spec/lib/phony/local_splitters/regex_spec.rb
|
|
88
88
|
- spec/lib/phony/national_code_spec.rb
|
|
89
|
+
- spec/lib/phony/national_splitters/default_spec.rb
|
|
89
90
|
- spec/lib/phony/national_splitters/fixed_spec.rb
|
|
90
91
|
- spec/lib/phony/national_splitters/none_spec.rb
|
|
91
92
|
- spec/lib/phony/national_splitters/regex_spec.rb
|
|
@@ -129,6 +130,7 @@ test_files:
|
|
|
129
130
|
- spec/lib/phony/local_splitters/fixed_spec.rb
|
|
130
131
|
- spec/lib/phony/local_splitters/regex_spec.rb
|
|
131
132
|
- spec/lib/phony/national_code_spec.rb
|
|
133
|
+
- spec/lib/phony/national_splitters/default_spec.rb
|
|
132
134
|
- spec/lib/phony/national_splitters/fixed_spec.rb
|
|
133
135
|
- spec/lib/phony/national_splitters/none_spec.rb
|
|
134
136
|
- spec/lib/phony/national_splitters/regex_spec.rb
|