Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

+ implement new syntax for length validation

  • Loading branch information...
commit db42b56d2498fab6cef202c7312edb6c2e05ac07 1 parent 2487960
@jcfischer jcfischer authored committed
View
100 lib/phony/countries.rb
@@ -20,7 +20,7 @@
# The third parameter to country are validations.
# Currently, there are two methods available:
# * invalid_ndcs: Give a regexp or string to describe invalid ndc(s).
-# * validate_lengths: Given a number or a hash, validates the length of the number
+# * length: Given a number or a hash, validates the length of the number
#
# Note: The ones that are commented are defined in their special files.
#
@@ -35,7 +35,7 @@
country '1',
fixed(3, :zero => false) >> split(3,4),
# invalid_ndcs('911'), # /911/ would also work.
- [invalid_ndcs('911'), validate_lengths(10)]
+ [invalid_ndcs('911'), length(10)]
# Kazakhstan (Republic of) & Russsian Federation.
# also Abhasia and South Osetia autonomous regions / recognized by some states as independent countries
@@ -48,12 +48,12 @@
one_of('2', '3') >> split(8) | # Cairo/Giza, Alexandria
fixed(2) >> split(8),
# :mobile? => /^1[01246-9]\d+$/, :service? => /^800\d+$/
- validate_lengths({ 3 => 10, 1 => 9, 2 => 10})
+ length( 3 => 10, 1 => 9, 2 => 10 )
# South Africa.
#
country '27', fixed(2) >> split(3,4),
- validate_lengths(9)
+ length(9)
# Greece.
#
@@ -72,12 +72,12 @@
# France.
#
country '33', fixed(1) >> split(2,2,2,2), # :service? => /^8.*$/, :mobile? => /^[67].*$/
- validate_lengths(9)
+ length(9)
# Spain.
#
country '34',
fixed(2) >> split(3,4),
- validate_lengths(9)
+ length(9)
# Hungary.
#
@@ -87,7 +87,7 @@
one_of('104','105','107','112') >> split(3,3) | # Service
one_of('1') >> split(3,4) | # Budapest
fixed(2) >> split(3,4), # 2-digit NDCs
- validate_lengths({ 3 => 9, 1 => 8, 2 => 9})
+ length( 3 => 9, 1 => 8, 2 => 9 )
# country '39' # Italy, see special file.
@@ -104,7 +104,7 @@
country '41',
match(/^(8(00|4[0248]))\d+$/) >> split(3,3) |
fixed(2) >> split(3,2,2),
- validate_lengths(9)
+ length(9)
# country '43' # Austria, see special file.
@@ -114,7 +114,7 @@
#
country '45',
none >> split(2,2,2,2),
- validate_lengths(8)
+ length(8)
# country '46' # Sweden, see special file.
@@ -141,7 +141,7 @@
one_of('103', '105') >> split(3,3) | # Service.
one_of('1', '9') >> split(4,4) | # Lima and mobile.
fixed(2) >> split(4,4), # 2-digit NDCs.
- validate_lengths({ 3 => 9, 1 => 9, 2 => 10})
+ length( 3 => 9, 1 => 9, 2 => 10 )
# Mexico.
#
@@ -185,7 +185,7 @@
#
country '58',
fixed(3) >> split(7),
- validate_lengths(10)
+ length(10)
# country '60' # Malaysia, see special file.
@@ -194,7 +194,7 @@
country '61',
match(/^(4\d\d)\d+$/) >> split(3,3) | # Mobile
fixed(1) >> split(4,4), # Rest
- validate_lengths(9)
+ length(9)
# Indonesia (Republic of)
country '62',
@@ -213,20 +213,20 @@
#
country '64',
fixed(1) >> split(3,4),
- validate_lengths(8)
+ length(8)
# Singapore (Republic of).
#
country '65',
none >> split(4,4), # TODO Short Codes.
- validate_lengths(8)
+ length(8)
# Thailand.
#
country '66',
one_of('2') >> split(3,4) | # Bangkok
fixed(2) >> split(3,3), # Rest
- validate_lengths(8)
+ length(8)
country '81', todo # TODO Japan
@@ -236,7 +236,7 @@
one_of('4', '8') >> split(7) |
match(/^(2[025679]|3[0136789]|5[23456789]|6[01234678]|7[02345679]|9[0-8])\d/) >> split(6) |
fixed(3) >> split(5),
- validate_lengths(8)
+ length(8)
@@ -246,7 +246,7 @@
#
country '90',
fixed(3) >> split(3,4), # Wiki says 7, but the examples say 3, 4.
- validate_lengths(10)
+ length(10)
# country '91' # India (Republic of) see special file
@@ -257,26 +257,26 @@
# From http://www.wtng.info/wtng-93-af.html
#
country '93', fixed(2) >> split(7), # Note: the document says 6, but the examples use 7.
- validate_lengths(9)
+ length(9)
country '94', # TODO Sri Lanka (Democratic Socialist Republic of)
fixed(2) >> split(3,2,2),
- validate_lengths(9)
+ length(9)
country '95', fixed(2) >> split(3,2,2) # TODO Myanmar (Union of)
country '98', # Iran (Islamic Republic of)
one_of('21') >> split(4,4) | # Teheran
fixed(3) >> split(3,4),
- validate_lengths(10)
+ length(10)
country '210', todo # -
country '211', todo # South Sudan
- country '212', fixed(2) >> split(4,3), validate_lengths(9) # Morocco
- country '213', fixed(2) >> split(3,4), validate_lengths(9) # Algeria
+ country '212', fixed(2) >> split(4,3), length(9) # Morocco
+ country '213', fixed(2) >> split(3,4), length(9) # Algeria
country '214', todo # -
country '215', todo # -
- country '216', fixed(1) >> split(3,4), validate_lengths(8) # Tunisia
+ country '216', fixed(1) >> split(3,4), length(8) # Tunisia
country '217', todo # -
country '218', todo # Lybia
country '219', todo # -
@@ -300,7 +300,7 @@
#
# From http://www.itu.int/oth/T0202000052/en
#
- country '233', fixed(2) >> split(3,4), validate_lengths(9)
+ country '233', fixed(2) >> split(3,4), length(9)
# Nigeria
# Wikipedia says 3 4 split, many local number with no splitting
@@ -325,13 +325,13 @@
country '244', todo # Angola
country '245', todo # Guinea-Bissau
country '246', todo # Diego Garcia
- country '247', none >> split(4), validate_lengths(4) # Ascension
+ country '247', none >> split(4), length(4) # Ascension
country '248', # Seychelles
fixed(1) >> split(2,2,2),
- validate_lengths(7)
+ length(7)
- country '249', fixed(2) >> split(3,4), validate_lengths(9) # Sudan
+ country '249', fixed(2) >> split(3,4), length(9) # Sudan
# Rwanda
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Rwanda
@@ -356,14 +356,14 @@
match(/^([89]\d\d)/) >> split(3,3) | # Special/Premium.
one_of('112', '118') >> split(3,3) | # Short Codes.
fixed(2) >> split(3,4), # Geographic.
- validate_lengths(9)
+ length(9)
# Uganda.
#
country '256',
match(/^(46[45]|4[78]\d)/) >> split(6) | # Geo 1.
fixed(2) >> split(7), # Geo 2.
- validate_lengths(9)
+ length(9)
country '257', todo # Burundi
country '258', todo # Mozambique
@@ -412,7 +412,7 @@
match(/^(2[012]\d)\d+$/) >> split(5) | # fixed
match(/^([56]\d)\d+$/) >> split(6) | # mobile
match(/^(8\d\d\d)$/) >> split(0),
- validate_lengths({ 2 => 8, 3 => 8, 4 => 0})
+ length( 2 => 8, 3 => 8, 4 => 0 )
# Portugal.
@@ -422,7 +422,7 @@
match(/^(9\d)\d+$/) >> split(3,4) | # Mobile.
one_of('21', '22') >> split(3,4) | # Lisboa & Porto
fixed(3) >> split(3,4), # 3-digit NDCs
- validate_lengths({ 2 => 9, 3 => [9, 10] })
+ length( 2 => 9, 3 => 9..10 )
# Luxembourg
@@ -433,11 +433,11 @@
match(/^(6\d[18])\d+$/) >> split(3,3) | # mobile
match(/^(60\d{2})\d{8}$/) >> split(2,2,2,2) | # mobile machine to machine
match(/^([2-9]\d)/) >> split(2,2,2), # 2-digit NDC
- validate_lengths( {1 => 7, 2 => 8, 4 => 10 })
+ length( 1 => 7, 2 => 8, 4 => 10 )
# country '353' # Republic of Ireland, see special file.
- country '354', none >> split(3,4), validate_lengths(7) # Iceland
+ country '354', none >> split(3,4), length(7) # Iceland
country '355', todo # Albania
# Malta
@@ -446,14 +446,14 @@
match(/^([79]\d)\d+$/) >> split(6) | # mobile
match(/^(2\d\d\d)\d+$/) >> split(4) | # fixed line
fixed(4) >> split(4), # eg. with voice mail
- validate_lengths(8)
+ length(8)
# Cyprus
# http://www.cytaglobal.com/cytaglobal/userfiles/NewNumberingPlan.pdf
country '357', one_of('121','122','123') >> split(2,6) | # voicemail
fixed(2) >> split(6), # fixed & mobile
- validate_lengths({ 2 => 8, 3 => 11 })
+ length( 2 => 8, 3 => 11 )
# Finland.
#
@@ -462,13 +462,13 @@
match(/^(4\d|50)\d+$/) >> split(3,2,2) | # Mobile
one_of('2','3','5','6','8','9') >> split(3,3) | # Short NDCs
fixed(2) >> split(3,3), # 2-digit NDCs
- validate_lengths({ 1 => 7, 2 => [8, 9], 3 => 9 })
+ length( 1 => 7, 2 => 8..9, 3 => 9 )
# Bulgaria.
#
country '359',
fixed(2) >> split(3,2,2), # Bulgaria
- validate_lengths(9)
+ length(9)
# Lithuania.
#
@@ -478,7 +478,7 @@
one_of('5') >> split(3,2,2) | # Vilnius
one_of('37','41') >> split(2,2,2) | # Kaunas, Šiauliai
fixed(3) >> split(1,2,2), # 3-digit NDCs.
- validate_lengths(8)
+ length(8)
country '371', todo # Latvia
@@ -487,7 +487,7 @@
match(/^(5\d\d\d)\d+$/) >> split(4) | # Mobile
match(/^((?:70|8[12])\d\d)\d+$/) >> split(4) | # Mobile
fixed(3) >> split(4), # 3-digit NDCs
- validate_lengths({ 2 => 6, 3 => 7, 4 => 8 })
+ length( 2 => 6, 3 => 7, 4 => 8 )
country '373', todo # Moldova
country '374', todo # Armenia
@@ -512,25 +512,25 @@
#
country = country '385', one_of('1') >> split(3, 5) | # Zagreb
fixed(2) >> split(3, 5), # 2-digit NDCs
- validate_lengths({ 1 => 9, 2 => 10})
+ length( 1 => 9, 2 => 10)
- country '386', fixed(2) >> split(3, 2, 2), validate_lengths(9) # Slovenia
- country '387', fixed(2) >> split(3,2,2), validate_lengths(9) # Bosnia and Herzegovina
- country '388', fixed(2) >> split(3,2,2), validate_lengths(9) # Group of countries, shared code
- country '389', fixed(2) >> split(3,2,2), validate_lengths(9) # The Former Yugoslav Republic of Macedonia
+ country '386', fixed(2) >> split(3, 2, 2), length(9) # Slovenia
+ country '387', fixed(2) >> split(3,2,2), length(9) # Bosnia and Herzegovina
+ country '388', fixed(2) >> split(3,2,2), length(9) # Group of countries, shared code
+ country '389', fixed(2) >> split(3,2,2), length(9) # The Former Yugoslav Republic of Macedonia
- country '420', fixed(3) >> split(3,3), validate_lengths(9) # Czech Republic
+ country '420', fixed(3) >> split(3,3), length(9) # Czech Republic
# Slovak Republic.
#
country '421', match(/^(9\d\d).+$/) >> split(6) | # Mobile
one_of('2') >> split(8) | # Bratislava
fixed(2) >> split(7), # 2-digit NDCs
- validate_lengths(9)
+ length(9)
country '422', todo # Spare code
- country '423', none >> split(3,2,2), validate_lengths(7) # Liechtenstein (Principality of)
+ country '423', none >> split(3,2,2), length(7) # Liechtenstein (Principality of)
country '424', todo # -
country '425', todo # -
country '426', todo # -
@@ -550,7 +550,7 @@
country '509', # Haiti (Republic of)
fixed(2) >> split(2,4),
- validate_lengths(8)
+ length(8)
country '590', todo # Guadeloupe (French Department of)
country '591', todo # Bolivia (Republic of)
@@ -625,7 +625,7 @@
country '852',
none >> split(4,4),
- validate_lengths(8)
+ length(8)
country '853', todo # Macao, China
country '854', todo # Spare code
@@ -633,7 +633,7 @@
# Cambodia (Kingdom of)
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Cambodia
country '855',
- fixed(2) >> split(3,4), validate_lengths(9)
+ fixed(2) >> split(3,4), length(9)
country '856', todo # Lao People's Democratic Republic
country '857', todo # Spare code
View
2  lib/phony/countries/india.rb
@@ -47,6 +47,6 @@
one_of(pagers) >> split(2, 3, 3) |
fixed(4) >> split(3, 3), # fallback (4 digit sdca)
- validate_lengths(10)
+ length(10)
end
View
8 lib/phony/dsl.rb
@@ -185,14 +185,14 @@ def invalid_ndcs ndc
# Example:
# country '1',
# fixed(3, :zero => false) >> split(3,4),
- # [invalid_ndcs('911'), length_validator(10)] # any number not 10 digits is invalid (and a 911 ndc too).
+ # [invalid_ndcs('911'), length(10)] # any number not 10 digits is invalid (and a 911 ndc too).
#
# country '45',
# none >> split(2,2,2,2),
- # length_validator(8) # danish phone numbers are always 8 digits long
+ # length(8) # danish phone numbers are always 8 digits long
- def validate_lengths length
- LengthValidator.new length
+ def length lengths
+ LengthValidator.new lengths
end
end
View
14 lib/phony/validators/length_validator.rb
@@ -15,14 +15,12 @@ def initialize lengths
end
def plausible? ndc, rest
- numbers = if ndc
- [ndc, rest].flatten
- else
- rest.flatten
- end
- ndc_length = ndc.length rescue 0 # if the ndc is false (example Denamrk), use 0
- lengths = [*@lengths[ndc_length]] # we can have one or more lengths, make them into an array
- return false unless lengths.include? numbers.join('').length
+ ndc ||= '' # handle 'false' ndc's (like Denmark)
+ lengths = [*@lengths[ndc.length]] # we can have one or more lengths, make them into an array
+
+ length = ndc.size + rest.inject(0) { |sum, part| part ? sum + part.size : sum }
+
+ return false unless lengths.include? length
true
end
Please sign in to comment.
Something went wrong with that request. Please try again.