phony 2.17.0 → 2.20.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/README.textile +34 -6
  3. data/lib/phony/config.rb +91 -0
  4. data/lib/phony/countries/argentina.rb +355 -0
  5. data/lib/phony/countries/austria.rb +4 -2
  6. data/lib/phony/countries/bangladesh.rb +2 -0
  7. data/lib/phony/countries/belarus.rb +2 -0
  8. data/lib/phony/countries/brazil.rb +4 -2
  9. data/lib/phony/countries/cambodia.rb +3 -5
  10. data/lib/phony/countries/china.rb +7 -2
  11. data/lib/phony/countries/croatia.rb +2 -0
  12. data/lib/phony/countries/georgia.rb +2 -0
  13. data/lib/phony/countries/germany.rb +5 -2
  14. data/lib/phony/countries/guinea.rb +8 -5
  15. data/lib/phony/countries/india.rb +2 -0
  16. data/lib/phony/countries/indonesia.rb +2 -0
  17. data/lib/phony/countries/ireland.rb +27 -23
  18. data/lib/phony/countries/italy.rb +41 -18
  19. data/lib/phony/countries/japan.rb +60 -8
  20. data/lib/phony/countries/kyrgyzstan.rb +2 -0
  21. data/lib/phony/countries/latvia.rb +2 -0
  22. data/lib/phony/countries/libya.rb +3 -1
  23. data/lib/phony/countries/malaysia.rb +2 -0
  24. data/lib/phony/countries/moldova.rb +2 -0
  25. data/lib/phony/countries/montenegro.rb +2 -0
  26. data/lib/phony/countries/myanmar.rb +2 -0
  27. data/lib/phony/countries/namibia.rb +2 -0
  28. data/lib/phony/countries/nepal.rb +2 -0
  29. data/lib/phony/countries/netherlands.rb +2 -0
  30. data/lib/phony/countries/pakistan.rb +2 -0
  31. data/lib/phony/countries/paraguay.rb +2 -0
  32. data/lib/phony/countries/russia_kazakhstan_abkhasia_south_ossetia.rb +2 -0
  33. data/lib/phony/countries/saudi_arabia.rb +2 -0
  34. data/lib/phony/countries/serbia.rb +7 -1
  35. data/lib/phony/countries/somalia.rb +2 -0
  36. data/lib/phony/countries/south_korea.rb +7 -2
  37. data/lib/phony/countries/sweden.rb +2 -0
  38. data/lib/phony/countries/taiwan.rb +3 -0
  39. data/lib/phony/countries/tajikistan.rb +2 -0
  40. data/lib/phony/countries/turkmenistan.rb +2 -0
  41. data/lib/phony/countries/ukraine.rb +2 -0
  42. data/lib/phony/countries/united_kingdom.rb +5 -2
  43. data/lib/phony/countries/uruguay.rb +2 -0
  44. data/lib/phony/countries/vietnam.rb +7 -1
  45. data/lib/phony/countries/zimbabwe.rb +2 -0
  46. data/lib/phony/countries.rb +108 -65
  47. data/lib/phony/country.rb +11 -3
  48. data/lib/phony/country_codes.rb +16 -3
  49. data/lib/phony/dsl.rb +5 -3
  50. data/lib/phony/local_splitters/fixed.rb +2 -0
  51. data/lib/phony/national_code.rb +1 -1
  52. data/lib/phony/national_splitters/none.rb +1 -3
  53. data/lib/phony/trunk_code.rb +5 -5
  54. data/lib/phony.rb +62 -57
  55. data/spec/functional/config_spec.rb +44 -0
  56. data/spec/functional/plausibility_spec.rb +104 -19
  57. data/spec/lib/phony/countries_spec.rb +107 -35
  58. data/spec/lib/phony/country_codes_spec.rb +82 -58
  59. data/spec/lib/phony/country_spec.rb +29 -9
  60. data/spec/lib/phony/dsl_spec.rb +7 -3
  61. data/spec/lib/phony/local_splitters/regex_spec.rb +19 -15
  62. data/spec/lib/phony/national_code_spec.rb +15 -45
  63. data/spec/lib/phony/national_splitters/fixed_spec.rb +12 -16
  64. data/spec/lib/phony/national_splitters/none_spec.rb +3 -3
  65. data/spec/lib/phony/national_splitters/variable_spec.rb +9 -13
  66. data/spec/lib/phony/trunk_code_spec.rb +85 -0
  67. data/spec/lib/phony/vanity_spec.rb +4 -4
  68. metadata +23 -18
@@ -1,4 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
+ # frozen_string_literal: true
3
+
2
4
  # All countries, ordered by country code.
3
5
  #
4
6
  # Definitions are in the format:
@@ -67,7 +69,7 @@ Phony.define do
67
69
  country '20', one_of('800') >> split(7..7) | # Egypt toll free
68
70
  one_of('3') >> split(7..7) | # Alexandria
69
71
  one_of('2') >> split(8..8) | # Cairo/Giza
70
- one_of('10', '11', '12') >> split(8..8) | # the 3 mobile operators
72
+ one_of('10', '11', '12', '15') >> split(8..8) | # the 4 mobile operators
71
73
  fixed(2) >> split(7..7) # all the other 24 provinces
72
74
 
73
75
  # South Africa.
@@ -94,15 +96,16 @@ Phony.define do
94
96
  # Belgium.
95
97
  #
96
98
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium
99
+ # https://www.bipt.be/operators/publication/national-numbering-plan
97
100
  #
98
101
  country '32', trunk('0') |
99
- match(/^(7[08])\d+$/) >> split(3,3) | # Premium and national rate Services
100
- match(/^(800|90\d)\d+$/) >> split(2,3) | # Toll free service and premium numbers
101
- match(/^(46[05678])\d{6}$/) >> split(2,2,2) | # Mobile (Lycamobile, Telenet, Join Experience, Proximus 0460)
102
- match(/^(4[789]\d)\d{6}$/) >> split(2,2,2) | # Mobile
103
- match(/^(456)\d{6}$/) >> split(2,2,2) | # Mobile Vikings
104
- one_of('2','3','4','9') >> split(3,2,2) | # Short NDCs
105
- fixed(2) >> split(2,2,2) # 2-digit NDCs
102
+ match(/^(7[08])\d+$/) >> split(3,3) | # Premium and national rate Services
103
+ match(/^(800|90\d)\d+$/) >> split(2,3) | # Toll free service and premium numbers
104
+ match(/^(46[056789])\d{6}$/) >> split(2,2,2) | # Mobile (Lycamobile, Telenet, Join Experience, Proximus 0460)
105
+ match(/^(4[789]\d)\d{6}$/) >> split(2,2,2) | # Mobile
106
+ match(/^(45[56])\d{6}$/) >> split(2,2,2) | # Mobile Vikings and Voo
107
+ one_of('2','3','4','9') >> split(3,2,2) | # Short NDCs
108
+ fixed(2) >> split(2,2,2) # 2-digit NDCs
106
109
 
107
110
  # France.
108
111
  #
@@ -152,6 +155,7 @@ Phony.define do
152
155
  #
153
156
  country '41',
154
157
  trunk('0', normalize: true) |
158
+ match(/^(860)\d+$/) >> split(2, 3, 2, 2) | # Voice Mail access
155
159
  match(/^(8(?:00|4[0248]))\d+$/) >> split(3,3) | # Freecall/Shared Cost
156
160
  match(/^(90[016])\d+$/) >> split(3,3) | # Business
157
161
  fixed(2) >> split(3,2,2)
@@ -211,14 +215,7 @@ Phony.define do
211
215
  fixed(3) >> split(7) # 3-digit NDCs
212
216
 
213
217
  # Argentine Republic.
214
- #
215
- country '54',
216
- one_of('11', '911') >> split(4,4) | # Fixed & Mobile
217
- match(/^(22[0137]|237|26[14]|29[179]|34[1235]|35[138]|38[1578])/) >> split(3,4) | # Fixed
218
- match(/^(922[0137]|9237|926[14]|929[179]|934[1235]|935[138]|938[1578])/) >> split(3,4) | # Mobile
219
- match(/^(9\d{4})/) >> split(2,4) | # Mobile
220
- match(/^([68]\d{2})/) >> split(3,4) | # Service
221
- fixed(4) >> split(2,4) # Fixed
218
+ # country '54' # argentina, see special file.
222
219
 
223
220
  # Brazil (Federative Republic of).
224
221
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Brazil
@@ -235,6 +232,7 @@ Phony.define do
235
232
  # http://www.itu.int/oth/T020200002C/en
236
233
  country '57',
237
234
  match(/\A(3\d\d)\d+\z/) >> split(3,4) | # mobile (300 310 311 312 313 315 316)
235
+ match(/\A(60\d)\d+\z/) >> split(3,4) |
238
236
  fixed(1) >> split(3,4)
239
237
 
240
238
  # Venezuela (Bolivarian Republic of)
@@ -262,9 +260,10 @@ Phony.define do
262
260
  trunk('0') |
263
261
  # 7/10 digits for area code '2'.
264
262
  match(/\A(2)\d{10}\z/) >> split(10) |
263
+ match(/\A(2)\d{8}\z/) >> split(8) |
265
264
  one_of('2') >> split(7) |
266
265
  # mobile
267
- match(/\A(9\d\d)\d{7}\z/) >> split(7) |
266
+ match(/\A([89]\d\d)\d{7}\z/) >> split(7) |
268
267
  match(/\A(9\d\d)\d+\z/) >> split(6) |
269
268
  #
270
269
  fixed(2) >> split(7)
@@ -356,20 +355,37 @@ Phony.define do
356
355
 
357
356
  # Côte d'Ivoire
358
357
  # http://www.wtng.info/wtng-225-ci.html
359
- # http://www.itu.int/dms_pub/itu-t/oth/02/02/T02020000310001PDFE.pdf
358
+ # https://www.itu.int/dms_pub/itu-t/oth/02/02/T02020000310006PDFE.pdf
360
359
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Ivory_Coast
361
360
  #
362
361
  # There is no trunk code for this country and several of the mobile prefixes start with 0
363
362
  country '225',
364
363
  trunk('', :normalize => false) |
365
- fixed(2) >> split(2,2,2)
364
+ fixed(2) >> split(2,2,2,2)
365
+
366
+ # Burkina Faso
367
+ # http://www.wtng.info/wtng-226-bf.html
368
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Burkina_Faso
369
+ #
370
+ # There is no trunk code for this country and several of the mobile prefixes start with 0
371
+ country '226',
372
+ trunk('', normalize: false) |
373
+ none >> split(2,2,2,2)
366
374
 
367
- country '226', none >> split(4,4) # Burkina Faso http://www.wtng.info/wtng-226-bf.html
368
375
  country '227', none >> split(4,4) # Niger http://www.wtng.info/wtng-227-ne.html
369
376
  country '228', none >> split(4,4) # Togolese Republic http://www.wtng.info/wtng-228-tg.html
370
377
  country '229', none >> split(4,4) # Benin http://www.itu.int/oth/T0202000017/en
371
378
 
372
- country '230', none >> split(4,4) # Mauritius http://www.wtng.info/wtng-230-mu.html
379
+ # Mauritius
380
+ # http://www.wtng.info/wtng-230-mu.html
381
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Mauritius
382
+ #
383
+ # There is no trunk code for this country.
384
+ country '230',
385
+ none >> matched_split(
386
+ /^5\d{7}$/ => [1,3,4], # Mobile
387
+ /^[246]\d{6}$/ => [3,4], # Landline
388
+ )
373
389
 
374
390
  # Liberia
375
391
  # https://www.numberingplans.com/?page=plans&sub=phonenr&alpha_2_input=LR
@@ -387,18 +403,25 @@ Phony.define do
387
403
  # Ghana
388
404
  #
389
405
  # From http://www.itu.int/oth/T0202000052/en
406
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Ghana
390
407
  #
391
- country '233', fixed(2) >> split(3,4)
408
+ country '233', trunk('0') | fixed(2) >> split(3,4)
392
409
 
393
410
  # Nigeria
394
- # Wikipedia says 3 4 split, many local number with no splitting
411
+ # 3 4 split for mobile and 1 digit NDC, 3 2 or 3 3 otherwise
412
+ # Many local numbers with no splitting
395
413
  #
396
- # mobile telephony number allocation taken from: http://www.ncc.gov.ng/index.php?option=com_content&view=article&id=113&Itemid=102
414
+ # mobile telephony number allocation taken from:
415
+ # https://www.ncc.gov.ng/technical-regulation/standards/numbering and
416
+ # https://www.itu.int/oth/T020200009C/en
397
417
  country '234',
398
- match(/^([7-9]0\d)\d+$/) >> split(3,4) | # Mobile
399
- match(/^(81\d)\d+$/) >> split(3,4) | # Mobile
400
- one_of('1', '2', '9') >> split(3,4) | # Lagos, Ibadan and Abuja
401
- fixed(2) >> split(3,4) # 2-digit NDC
418
+ match(/^([7-9]0\d)\d+$/) >> split(3,4) | # Mobile
419
+ match(/^(81\d)\d+$/) >> split(3,4) | # Mobile
420
+ match(/^(91\d)\d+$/) >> split(3,4) | # Mobile
421
+ one_of('1', '2') >> split(3,3..4) | # Lagos, Ibadan
422
+ one_of('9') >> split(3,4) | # Abuja
423
+ one_of((30..79).map(&:to_s)) >> split(3,2..3) | # 2-digit NDC
424
+ one_of(%w(82 83 84 85 86 87 88 89)) >> split(3,3) # 2-digit NDC
402
425
 
403
426
  country '235', none >> split(4,4) # Chad http://www.wtng.info/wtng-235-td.html
404
427
  country '236', none >> split(4,4) # Central African Republic http://www.wtng.info/wtng-236-cf.html
@@ -407,7 +430,9 @@ Phony.define do
407
430
  country '239', fixed(1) >> split(3,3) # Sao Tome and Principe, http://www.wtng.info/wtng-239-st.html
408
431
 
409
432
  country '240', none >> split(3,3,3) # Equatorial Guinea
410
- country '241', fixed(1) >> split(3,3) # Gabonese Republic http://www.wtng.info/wtng-241-ga.html
433
+ country '241',
434
+ match(/^(\d)\d{6}$/) >> split(3,3) | # Gabonese Republic http://www.wtng.info/wtng-241-ga.html
435
+ match(/^(\d\d)\d{6}$/) >> split(2,2,2) # 2019 update https://en.wikipedia.org/wiki/Telephone_numbers_in_Gabon
411
436
  country '242', # Congo http://www.wtng.info/wtng-242-cg.html
412
437
  trunk('', :normalize => false) |
413
438
  none >> split(4,5)
@@ -427,9 +452,9 @@ Phony.define do
427
452
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Rwanda
428
453
  country '250',
429
454
  trunk('0') |
430
- one_of('25') >> split(7) | # Geographic, fixed
431
- match(/^(7[238])/) >> split(7) | # Non-geographic, mobile
432
- one_of('06') >> split(6) # Satellite
455
+ one_of('25') >> split(7) | # Geographic, fixed
456
+ match(/^(7[2389])/) >> split(7) | # Non-geographic, mobile
457
+ one_of('06') >> split(6) # Satellite
433
458
 
434
459
  country '251', fixed(2) >> split(3, 4) # Ethiopia http://www.wtng.info/wtng-251-et.html
435
460
 
@@ -469,7 +494,7 @@ Phony.define do
469
494
  one_of('802') >> split(3,3) | # national rate
470
495
  one_of(%w(251 252 271 272 281 282 293)) >> split(3,2) |
471
496
  one_of(%w(21 23 24 26)) >> split(3,3) |
472
- one_of(%w(82 84 86)) >> split(4,3) | # mobile
497
+ one_of(%w(82 83 84 85 86 87)) >> split(4,3) | # mobile
473
498
  one_of('89') >> split(4,3) | # satellite GMPCS
474
499
  one_of('7') >> split(4,4) | # universal access
475
500
  one_of('9') >> split(4,4) | # premium rate
@@ -480,27 +505,32 @@ Phony.define do
480
505
  # Zambia
481
506
  # http://www.wtng.info/wtng-260-zm.html
482
507
  # https://github.com/googlei18n/libphonenumber/
508
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Zambia
509
+
483
510
  country '260',
484
- match(/^(9(5[034589]|[67]\d))/) >> split(6) | # Mobile
485
- match(/^(800)/) >> split(3,3) | # Toll free
486
- match(/^(21[1-8])/) >> split(6) # Fixed
511
+ trunk('0') |
512
+ match(/^(75|76|77|94|95|96|97)/) >> split(3, 4) | # Mobile
513
+ match(/^(800)/) >> split(3,3) | # Toll free
514
+ match(/^(21[1-8])/) >> split(6) # Fixed
487
515
 
488
- # Madagascar http://www.wtng.info/wtng-261-mg.html
516
+ # Madagascar
517
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Madagascar
489
518
  # http://www.itu.int/oth/T020200007F/en
490
519
  country '261',
491
- none >> matched_split(
492
- /\A200\d+\z/ => [2,3,3,3], # Telecom Malagasy (Telma)
493
- /\A20\d+\z/ => [2,3,4], # Telecom Malagasy (Telma)
494
- /\A23\d+\z/ => [2,3,4], # Digitel
495
- /\A30\d+\z/ => [2,3,4], # mobile Madamobil (CDMA2000)
496
- /\A31\d+\z/ => [2,3,4], # mobile Airtel Madagascar
497
- /\A32\d+\z/ => [2,3,4], # mobile Orange Madagascar
498
- /\A33\d+\z/ => [2,3,4], # mobile Airtel Madagascar
499
- /\A34\d+\z/ => [2,3,4], # mobile Telecom Malagasy (Telma)
500
- /\A5\d+\z/ => [3,3,3], # pager
501
- /\A22\d+\z/ => [3,3,3], # satellite GULFSAT Téléphonie
502
- /\A6\d+\z/ => [3,3,3] # satellite
503
- )
520
+ # none >> matched_split(
521
+ # /\A20\d+\z/ => [2,2,3,2], # Telecom Malagasy (Telma)
522
+ # /\A32\d+\z/ => [2,2,3,2], # mobile Orange Madagascar
523
+ # /\A33\d+\z/ => [2,2,3,2], # mobile Airtel Madagascar
524
+ # /\A34\d+\z/ => [2,2,3,2], # mobile Telecom Malagasy (Telma)
525
+ # /\A38\d+\z/ => [2,2,3,2], # mobile Telecom Malagasy (Telma)
526
+ # /\A39\d+\z => [2,2,3,2] # mobile Blueline
527
+ # ),
528
+ match(/\A(20)\d+\z/) >> split(2,3,2) | # fix Telecom Malagasy (Telma)
529
+ match(/\A(32)\d+\z/) >> split(2,3,2) | # mobile Orange Madagascar
530
+ match(/\A(33)\d+\z/) >> split(2,3,2) | # mobile Airtel Madagascar
531
+ match(/\A(34)\d+\z/) >> split(2,3,2) | # mobile Telecom Malagasy (Telma)
532
+ match(/\A(38)\d+\z/) >> split(2,3,2) | # mobile Telecom Malagasy (Telma)
533
+ match(/\A(39)\d+\z/) >> split(2,3,2) # mobile Blueline Madagascar
504
534
 
505
535
  country '262', # Reunion / Mayotte (new) http://www.wtng.info/wtng-262-fr.html
506
536
  trunk('0') |
@@ -624,12 +654,12 @@ Phony.define do
624
654
  match(/^(2[467]\d{2})\d{4}$/) >> split(2,2) | # 4-digit NDC
625
655
  match(/^(6\d[18])\d+$/) >> split(3,3) | # mobile
626
656
  match(/^(60\d{2})\d{8}$/) >> split(2,2,2,2) | # mobile machine to machine
627
- match(/^((2[^467]|[3-9]\d))\d{4}$/) >> split(2,2) | # 2-digit NDC Regular 6 digits number
628
- match(/^((2[^467]|[3-9]\d))\d{4}$/) >> split(2,2) | # 2-digit NDC Regular 6 digits number
629
- match(/^((2[^467]|[3-9]\d))\d{5}$/) >> split(2,2,1) | # 2-digit NDC Regular 6 digits number w/ 1 digit extension
630
- match(/^((2[^467]|[3-9]\d))\d{6}$/) >> split(2,2,2) | # 2-digit NDC Regular 8 digits number or 6 digits with 2 digits extension
631
- match(/^((2[^467]|[3-9]\d))\d{7}$/) >> split(2,2,3) | # 2-digit NDC Regular 6 digits with 4 digits extension
632
- match(/^((2[^467]|[3-9]\d))\d{8}$/) >> split(2,2,4) | # 2-digit NDC Regular 6 digits number with 4 digits extension
657
+ match(/^((2[^467]|[2-9]\d))\d{4}$/) >> split(2,2) | # 2-digit NDC Regular 6 digits number
658
+ match(/^((2[^467]|[2-9]\d))\d{4}$/) >> split(2,2) | # 2-digit NDC Regular 6 digits number
659
+ match(/^((2[^467]|[2-9]\d))\d{5}$/) >> split(2,2,1) | # 2-digit NDC Regular 6 digits number w/ 1 digit extension
660
+ match(/^((2[^467]|[2-9]\d))\d{6}$/) >> split(2,2,2) | # 2-digit NDC Regular 8 digits number or 6 digits with 2 digits extension
661
+ match(/^((2[^467]|[2-9]\d))\d{7}$/) >> split(2,2,3) | # 2-digit NDC Regular 6 digits with 4 digits extension
662
+ match(/^((2[^467]|[2-9]\d))\d{8}$/) >> split(2,2,4) | # 2-digit NDC Regular 6 digits number with 4 digits extension
633
663
  match(/^(\d{2})\d{3}$/) >> split(2,2,1) # fallback for 5 digit number
634
664
 
635
665
  # country '353' # Republic of Ireland, see special file.
@@ -736,11 +766,12 @@ Phony.define do
736
766
  # Monaco
737
767
  #
738
768
  country '377',
739
- one_of('6') >> split(2,2,2,2) | # mobile
769
+ one_of('6') >> split(2,2,2,2) | # mobile
740
770
  fixed(2) >> split(2,2,2)
741
771
 
742
772
  # San Marino
743
773
  country '378',
774
+ trunk('', :normalize => false) |
744
775
  none >> matched_split(
745
776
  /\A\d{6}\z/ => [3,3],
746
777
  /\A\d+\z/ => [3,3,4]
@@ -898,7 +929,14 @@ Phony.define do
898
929
  )
899
930
 
900
931
  country '671', todo # Spare code
901
- country '672', todo # Australian External Territories
932
+
933
+ # Australian External Territories https://en.wikipedia.org/wiki/Telephone_numbers_in_Norfolk_Island
934
+ # Norfolk Island
935
+ country '672',
936
+ fixed(1) >> split(2,3) |
937
+ match(/^(2\d+)$/) >> split(3) | # Fixed
938
+ match(/^(5\d+)$/) >> split(3) # Mobile
939
+
902
940
  country '673', fixed(1) >> split(3, 3) # Brunei Darussalam http://www.wtng.info/wtng-673-bn.html
903
941
  country '674', none >> split(3, 4) # Nauru (Republic of) http://www.wtng.info/wtng-674-nr.html
904
942
 
@@ -965,12 +1003,12 @@ Phony.define do
965
1003
  /\A800\d+\z/ => [3,3], # freephone
966
1004
  /\A830\d+\z/ => [3,3], # shared cost
967
1005
  /\A60\d+\z/ => [3,3], # wireless geographic
968
- /\A(72|75|76|77)\d+\z/ => [3,4], # mobile
1006
+ /\A(71|72|75|76|77)\d+\z/ => [3,4], # mobile
969
1007
  /\A84\d+\z/ => [3,4], # wireless geographic
970
1008
  /\A\d+\z/ => [2,3] # geographic
971
1009
  )
972
1010
 
973
- country '686', none >> split(2,3) # Kiribati (Republic of) http://www.wtng.info/wtng-686-ki.html
1011
+ country '686', none >> split(8) # Kiribati (Republic of) https://www.numberingplans.com/?page=plans&sub=phonenr&alpha_2_input=KI
974
1012
  country '687', none >> split(3,3) # New Caledonia (Territoire français d'outre-mer) http://www.wtng.info/wtng-687-nc.html
975
1013
  country '688', none >> split(5) # Tuvalu http://www.wtng.info/wtng-688-tv.html
976
1014
  country '689', none >> split(2,2,2,2) # French Polynesia (Territoire français d'outre-mer) http://www.wtng.info/wtng-689-pf.html
@@ -986,7 +1024,10 @@ Phony.define do
986
1024
  country '698', todo # -
987
1025
  country '699', todo # -
988
1026
 
989
- country '800', todo # International Freephone Service
1027
+ # International Freephone Service
1028
+ # https://www.itu.int/en/ITU-T/inr/unum/Pages/uifn.aspx
1029
+ country '800', none >> split(8)
1030
+
990
1031
  country '801', todo # -
991
1032
  country '802', todo # -
992
1033
  country '803', todo # -
@@ -994,7 +1035,7 @@ Phony.define do
994
1035
  country '805', todo # -
995
1036
  country '806', todo # -
996
1037
  country '807', todo # -
997
- country '808', todo # International Shared Cost Service (ISCS)
1038
+ country '808', none >> split(12) # International Shared Cost Service (ISCS)
998
1039
  country '809', todo # -
999
1040
 
1000
1041
  country '830', todo # -
@@ -1025,6 +1066,7 @@ Phony.define do
1025
1066
  # Hong Kong, China
1026
1067
 
1027
1068
  country '852',
1069
+ match(/^(800)\d+$/) >> split(2, 4) |
1028
1070
  none >> split(4,4)
1029
1071
 
1030
1072
  # Macao, China
@@ -1037,6 +1079,7 @@ Phony.define do
1037
1079
 
1038
1080
  # Lao People's Democratic Republic http://www.wtng.info/wtng-856-la.html, https://www.numberingplans.com
1039
1081
  country '856',
1082
+ trunk('0') |
1040
1083
  one_of('30') >> split(3,4) | # geographic
1041
1084
  one_of('20') >> split(4,4) | # mobile
1042
1085
  fixed(2) >> split(3,3) # geographic
@@ -1182,9 +1225,9 @@ Phony.define do
1182
1225
  # Israel (State of) (also works for numbers in Palestinian territories)
1183
1226
  country '972',
1184
1227
  trunk('0') |
1185
- one_of('1') >> split(3,3,3) | # special numbers
1186
- one_of('2', '3', '4', '8', '9') >> split(3,4) | # 1 digit ndc
1187
- match(/^(5[023456789]|7[23467])\d+$/) >> split(3,4) # 2 digit ndc
1228
+ one_of('1') >> split(3,3,3) | # special numbers
1229
+ one_of('2', '3', '4', '8', '9') >> split(3,4) | # 1 digit ndc
1230
+ match(/^(5[012345689]|7[234679])\d+$/) >> split(3,4) # 2 digit ndc
1188
1231
 
1189
1232
  country '973', none >> split(4,4..4) # Bahrain (Kingdom of) http://www.itu.int/oth/T0202000011/en
1190
1233
 
data/lib/phony/country.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Phony
2
4
 
3
5
  #
@@ -95,6 +97,9 @@ module Phony
95
97
  end
96
98
  end
97
99
  def format_cc_ndc trunk, ndc, local, type, space, parentheses, use_trunk
100
+ # Note: We mark NDCs that are of type "none" with false (nil trips plausible?). This would result in false being printed.
101
+ # Therefore we set NDC to nil when formatting.
102
+ ndc = nil if ndc == false
98
103
  case type
99
104
  when String
100
105
  trunk &&= trunk.format(space, use_trunk)
@@ -123,6 +128,7 @@ module Phony
123
128
  end
124
129
  end
125
130
  def format_ndc ndc, parentheses
131
+ ndc = nil if ndc == false # TODO
126
132
  parentheses ? "(#{ndc})" : ndc
127
133
  end
128
134
  def format_with_ndc format, cc, ndc, local, space
@@ -154,10 +160,12 @@ module Phony
154
160
  #
155
161
  # In some cases it doesn't, like Italy.
156
162
  #
157
- def normalize national_number
163
+ # Note: Options such as CC
164
+ #
165
+ def normalize national_number, options = {}
158
166
  clean! national_number
159
167
  normalized = @codes.reduce national_number do |number, code|
160
- result = code.normalize number
168
+ result = code.normalize number, options
161
169
  break result if result
162
170
  number
163
171
  end
@@ -168,7 +176,7 @@ module Phony
168
176
  #
169
177
  def plausible? rest, hints = {}
170
178
  local, _, ndc, *rest = internal_split rest
171
-
179
+
172
180
  # Element based checking.
173
181
  #
174
182
  # Note: ndc == false means the country has none.
@@ -1,6 +1,6 @@
1
1
  module Phony
2
2
 
3
- EMPTY_STRING = ''
3
+ EMPTY_STRING = '' unless defined?(EMPTY_STRING)
4
4
 
5
5
  # Handles determining the correct national code handler.
6
6
  #
@@ -66,7 +66,7 @@ module Phony
66
66
  country, cc, number = partial_split number
67
67
  country
68
68
  end
69
- number = country.normalize number
69
+ number = country.normalize number, cc: cc
70
70
  countrify! number, cc
71
71
  end
72
72
 
@@ -97,9 +97,20 @@ module Phony
97
97
 
98
98
  # False if it fails the basic check.
99
99
  #
100
- return false unless (4..16) === normalized.size
100
+ return false unless (4..16) === normalized.size # unless hints[:check_length] == false
101
101
 
102
102
  country, cc, rest = partial_split normalized
103
+
104
+ # Was a country calling code given?
105
+ #
106
+ if ccc = hints[:ccc]
107
+ cc, ndc, *local = split ccc
108
+
109
+ raise ArgumentError.new("The provided ccc option is too long and includes more than a cc ('#{cc}') and ndc ('#{ndc}'). It also includes '#{local.join}'.") unless local.size == 1 && local[0].empty?
110
+
111
+ hints[:cc] = cc
112
+ hints[:ndc] = ndc
113
+ end
103
114
 
104
115
  # Country code plausible?
105
116
  #
@@ -109,6 +120,8 @@ module Phony
109
120
  # Country specific tests.
110
121
  #
111
122
  country.plausible? rest, hints
123
+ rescue ArgumentError
124
+ raise
112
125
  rescue StandardError
113
126
  return false
114
127
  end
data/lib/phony/dsl.rb CHANGED
@@ -12,9 +12,9 @@ module Phony
12
12
  #
13
13
  # Phony.define.country ...
14
14
  #
15
- def self.define
15
+ def self.define(&block)
16
16
  dsl = DSL.new
17
- dsl.instance_eval(&Proc.new) if block_given?
17
+ dsl.instance_eval(&block) if block_given?
18
18
  dsl
19
19
  end
20
20
 
@@ -41,6 +41,8 @@ module Phony
41
41
  # country '27', # CC, followed by rules, for example fixed(2) >> ...
42
42
  #
43
43
  def country country_code, definition, options = {}
44
+ return unless Phony.config.load?(country_code)
45
+
44
46
  definition.with country_code, options
45
47
  Phony::CountryCodes.instance.add country_code, definition
46
48
  end
@@ -139,7 +141,7 @@ module Phony
139
141
  #
140
142
  ndcs = ndcs.first if Array === ndcs.first
141
143
 
142
- NationalSplitters::Variable.new options[:max_length], ndcs
144
+ NationalSplitters::Variable.new options[:max_length], ndcs.map(&:freeze)
143
145
  end
144
146
 
145
147
  # If you have a number of (possibly) variable length NDCs
@@ -62,6 +62,8 @@ module Phony
62
62
  #
63
63
  #
64
64
  def plausible? rest, hints = {}
65
+ return true if hints[:check_length] == false
66
+
65
67
  @length === rest.inject(0) { |total, part| total + part.size }
66
68
  end
67
69
 
@@ -26,7 +26,7 @@ module Phony
26
26
  #
27
27
  # Note: Some cases, like Italy, don't remove the relative zero.
28
28
  #
29
- def normalize national_number
29
+ def normalize national_number, options = {}
30
30
  national_number.gsub(/\A0+/, EMPTY_STRING)
31
31
  end
32
32
 
@@ -31,9 +31,7 @@ module Phony
31
31
  # since using nil is dangerous and breaks
32
32
  # abstraction)
33
33
  #
34
- # Note: Actually, it might stay in.
35
- #
36
- # TODO Flip nil/false?
34
+ # Note: Decided it stays in. When formatting, it's turned into nil.
37
35
  #
38
36
  def split national_number
39
37
  [nil, false, national_number]
@@ -6,9 +6,9 @@ module Phony
6
6
  # * code: The trunk code, e.g. 0.
7
7
  #
8
8
  # Options:
9
- # * normalize: Remove the trunk code when normalizing (only use if number scheme is defined unambiguously).
10
- # * split: Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
11
- # * format: Add the trunk code when formatting (passing `false` will not add it).
9
+ # * normalize: [true (default), false] Remove the trunk code when normalizing (only use if number scheme is defined unambiguously).
10
+ # * split: [true, false (default)] Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
11
+ # * format: [true (default), false] Add the trunk code when formatting (passing `false` will not add it).
12
12
  #
13
13
  def initialize code, options = {}
14
14
  @code = code
@@ -35,8 +35,8 @@ module Phony
35
35
 
36
36
  # Normalize normalizes the given national number.
37
37
  #
38
- def normalize national_number
39
- national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @normalize
38
+ def normalize national_number, options = {}
39
+ national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @normalize && options[:cc]
40
40
  return national_number
41
41
  end
42
42