phony 2.16.4 → 2.20.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +5 -5
  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 +40 -18
  19. data/lib/phony/countries/japan.rb +61 -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 +22 -2
  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 +22 -13
  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 +3 -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 +93 -93
  45. data/lib/phony/countries/zimbabwe.rb +2 -0
  46. data/lib/phony/countries.rb +142 -64
  47. data/lib/phony/country.rb +15 -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 +285 -18
  57. data/spec/lib/phony/countries_spec.rb +140 -39
  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. data/spec/lib/phony_spec.rb +63 -67
  69. metadata +13 -8
@@ -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)
@@ -184,11 +188,13 @@ Phony.define do
184
188
  # country '49' # Germany, see special file.
185
189
 
186
190
  # Peru.
187
- #
191
+ # Note: https://en.wikipedia.org/wiki/Telephone_numbers_in_Peru
192
+ # Note: https://www.numberingplans.com/?page=plans&sub=phonenr&alpha_2_input=PE
188
193
  country '51',
189
194
  one_of('103', '105') >> split(3,3) | # Service.
190
- one_of('1', '9') >> split(4,4) | # Lima and mobile.
191
- fixed(2) >> split(4,4) # 2-digit NDCs.
195
+ one_of('9') >> split(4,4) | # Mobile.
196
+ one_of('1') >> split(3,4) | # Lima.
197
+ fixed(2) >> split(3,3) # 2-digit NDCs.
192
198
 
193
199
  # Mexico.
194
200
  #
@@ -209,14 +215,7 @@ Phony.define do
209
215
  fixed(3) >> split(7) # 3-digit NDCs
210
216
 
211
217
  # Argentine Republic.
212
- #
213
- country '54',
214
- one_of('11', '911') >> split(4,4) | # Fixed & Mobile
215
- match(/^(22[0137]|237|26[14]|29[179]|34[1235]|35[138]|38[1578])/) >> split(3,4) | # Fixed
216
- match(/^(922[0137]|9237|926[14]|929[179]|934[1235]|935[138]|938[1578])/) >> split(3,4) | # Mobile
217
- match(/^(9\d{4})/) >> split(2,4) | # Mobile
218
- match(/^([68]\d{2})/) >> split(3,4) | # Service
219
- fixed(4) >> split(2,4) # Fixed
218
+ # country '54' # argentina, see special file.
220
219
 
221
220
  # Brazil (Federative Republic of).
222
221
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Brazil
@@ -233,6 +232,7 @@ Phony.define do
233
232
  # http://www.itu.int/oth/T020200002C/en
234
233
  country '57',
235
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) |
236
236
  fixed(1) >> split(3,4)
237
237
 
238
238
  # Venezuela (Bolivarian Republic of)
@@ -260,9 +260,10 @@ Phony.define do
260
260
  trunk('0') |
261
261
  # 7/10 digits for area code '2'.
262
262
  match(/\A(2)\d{10}\z/) >> split(10) |
263
+ match(/\A(2)\d{8}\z/) >> split(8) |
263
264
  one_of('2') >> split(7) |
264
265
  # mobile
265
- match(/\A(9\d\d)\d{7}\z/) >> split(7) |
266
+ match(/\A([89]\d\d)\d{7}\z/) >> split(7) |
266
267
  match(/\A(9\d\d)\d+\z/) >> split(6) |
267
268
  #
268
269
  fixed(2) >> split(7)
@@ -354,20 +355,37 @@ Phony.define do
354
355
 
355
356
  # Côte d'Ivoire
356
357
  # http://www.wtng.info/wtng-225-ci.html
357
- # 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
358
359
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Ivory_Coast
359
360
  #
360
361
  # There is no trunk code for this country and several of the mobile prefixes start with 0
361
362
  country '225',
362
363
  trunk('', :normalize => false) |
363
- 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)
364
374
 
365
- country '226', none >> split(4,4) # Burkina Faso http://www.wtng.info/wtng-226-bf.html
366
375
  country '227', none >> split(4,4) # Niger http://www.wtng.info/wtng-227-ne.html
367
376
  country '228', none >> split(4,4) # Togolese Republic http://www.wtng.info/wtng-228-tg.html
368
377
  country '229', none >> split(4,4) # Benin http://www.itu.int/oth/T0202000017/en
369
378
 
370
- 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
+ )
371
389
 
372
390
  # Liberia
373
391
  # https://www.numberingplans.com/?page=plans&sub=phonenr&alpha_2_input=LR
@@ -385,18 +403,25 @@ Phony.define do
385
403
  # Ghana
386
404
  #
387
405
  # From http://www.itu.int/oth/T0202000052/en
406
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Ghana
388
407
  #
389
- country '233', fixed(2) >> split(3,4)
408
+ country '233', trunk('0') | fixed(2) >> split(3,4)
390
409
 
391
410
  # Nigeria
392
- # 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
393
413
  #
394
- # 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
395
417
  country '234',
396
- match(/^([7-9]0\d)\d+$/) >> split(3,4) | # Mobile
397
- match(/^(81\d)\d+$/) >> split(3,4) | # Mobile
398
- one_of('1', '2', '9') >> split(3,4) | # Lagos, Ibadan and Abuja
399
- 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
400
425
 
401
426
  country '235', none >> split(4,4) # Chad http://www.wtng.info/wtng-235-td.html
402
427
  country '236', none >> split(4,4) # Central African Republic http://www.wtng.info/wtng-236-cf.html
@@ -405,7 +430,9 @@ Phony.define do
405
430
  country '239', fixed(1) >> split(3,3) # Sao Tome and Principe, http://www.wtng.info/wtng-239-st.html
406
431
 
407
432
  country '240', none >> split(3,3,3) # Equatorial Guinea
408
- 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
409
436
  country '242', # Congo http://www.wtng.info/wtng-242-cg.html
410
437
  trunk('', :normalize => false) |
411
438
  none >> split(4,5)
@@ -425,9 +452,9 @@ Phony.define do
425
452
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Rwanda
426
453
  country '250',
427
454
  trunk('0') |
428
- one_of('25') >> split(7) | # Geographic, fixed
429
- match(/^(7[238])/) >> split(7) | # Non-geographic, mobile
430
- 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
431
458
 
432
459
  country '251', fixed(2) >> split(3, 4) # Ethiopia http://www.wtng.info/wtng-251-et.html
433
460
 
@@ -478,27 +505,32 @@ Phony.define do
478
505
  # Zambia
479
506
  # http://www.wtng.info/wtng-260-zm.html
480
507
  # https://github.com/googlei18n/libphonenumber/
508
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Zambia
509
+
481
510
  country '260',
482
- match(/^(9(5[034589]|[67]\d))/) >> split(6) | # Mobile
483
- match(/^(800)/) >> split(3,3) | # Toll free
484
- 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
485
515
 
486
- # Madagascar http://www.wtng.info/wtng-261-mg.html
516
+ # Madagascar
517
+ # https://en.wikipedia.org/wiki/Telephone_numbers_in_Madagascar
487
518
  # http://www.itu.int/oth/T020200007F/en
488
519
  country '261',
489
- none >> matched_split(
490
- /\A200\d+\z/ => [2,3,3,3], # Telecom Malagasy (Telma)
491
- /\A20\d+\z/ => [2,3,4], # Telecom Malagasy (Telma)
492
- /\A23\d+\z/ => [2,3,4], # Digitel
493
- /\A30\d+\z/ => [2,3,4], # mobile Madamobil (CDMA2000)
494
- /\A31\d+\z/ => [2,3,4], # mobile Airtel Madagascar
495
- /\A32\d+\z/ => [2,3,4], # mobile Orange Madagascar
496
- /\A33\d+\z/ => [2,3,4], # mobile Airtel Madagascar
497
- /\A34\d+\z/ => [2,3,4], # mobile Telecom Malagasy (Telma)
498
- /\A5\d+\z/ => [3,3,3], # pager
499
- /\A22\d+\z/ => [3,3,3], # satellite GULFSAT Téléphonie
500
- /\A6\d+\z/ => [3,3,3] # satellite
501
- )
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
502
534
 
503
535
  country '262', # Reunion / Mayotte (new) http://www.wtng.info/wtng-262-fr.html
504
536
  trunk('0') |
@@ -679,9 +711,28 @@ Phony.define do
679
711
 
680
712
  # Bulgaria
681
713
  #
714
+ # Note: https://www.numberingplans.com/?page=plans&sub=phonenr&alpha_2_input=BG
715
+ # Note: https://en.wikipedia.org/wiki/Telephone_numbers_in_Bulgaria
682
716
  country '359',
683
717
  trunk('0') |
684
- fixed(2) >> split(3,2,2) # Bulgaria
718
+ match(/^(2)\d{6}$/) >> split(3,3) | # Landline Sofia (optional last digit)
719
+ match(/^(2)\d{7}$/) >> split(3,4) | # Landline Sofia (optional last digit)
720
+ match(/^(30|31|33|36|37|39|41|43\d|45|47|51|53|55|57|59)\d{5}$/) >> split(5) |
721
+ match(/^(30|31|32|33|34|35|36|37|38|39|41|42|44|45|46|47|51|52|53|54|55|56|57|58|59)\d{6}$/) >> split(2,4) |
722
+
723
+ match(/^(60|61|63|65|66|69)\d{5}$/) >> split(5) |
724
+ match(/^(60|61|62|63|64|65|66|68|69)\d{6}$/) >> split(6) |
725
+
726
+ match(/^(700|701|703|704|705|706|707|708|709)\d{5}$/) >> split(5) |
727
+ match(/^(701|702|703|704|705|706|707|708|709)\d{4}$/) >> split(4) |
728
+
729
+ match(/^(71|72|74|75|77)\d{5}$/) >> split(5) |
730
+ match(/^(71|72|73|74|75|76|77|78|79)\d{6}$/) >> split(6) |
731
+
732
+ match(/^(80\d|81|91|93|95|97)\d{5}$/) >> split(5) |
733
+ match(/^(81|82|84|86)\d{6}$/) >> split(6) |
734
+ match(/^(87|88|89)\d{7}$/) >> split(4,3) |
735
+ match(/^(90|91|92|93|94|95|96|97|98\d|99\d)\d{6}$/) >> split(6)
685
736
 
686
737
  # Lithuania
687
738
  #
@@ -715,11 +766,12 @@ Phony.define do
715
766
  # Monaco
716
767
  #
717
768
  country '377',
718
- one_of('6') >> split(2,2,2,2) | # mobile
769
+ one_of('6') >> split(2,2,2,2) | # mobile
719
770
  fixed(2) >> split(2,2,2)
720
771
 
721
772
  # San Marino
722
773
  country '378',
774
+ trunk('', :normalize => false) |
723
775
  none >> matched_split(
724
776
  /\A\d{6}\z/ => [3,3],
725
777
  /\A\d+\z/ => [3,3,4]
@@ -733,7 +785,13 @@ Phony.define do
733
785
 
734
786
  # country '382' # Montenegro, see special file
735
787
 
736
- country '383', todo # -
788
+ # Kosovo
789
+ #
790
+ # Note: https://en.wikipedia.org/wiki/Telephone_numbers_in_Kosovo
791
+ country '383',
792
+ trunk('0') |
793
+ fixed(2) >> split(3,3)
794
+
737
795
  country '384', todo # -
738
796
 
739
797
  # country '385' # Croatia, see special file.
@@ -871,7 +929,14 @@ Phony.define do
871
929
  )
872
930
 
873
931
  country '671', todo # Spare code
874
- 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
+
875
940
  country '673', fixed(1) >> split(3, 3) # Brunei Darussalam http://www.wtng.info/wtng-673-bn.html
876
941
  country '674', none >> split(3, 4) # Nauru (Republic of) http://www.wtng.info/wtng-674-nr.html
877
942
 
@@ -888,7 +953,14 @@ Phony.define do
888
953
  one_of('115') >> split(4,4) | # voicemail (mobile)
889
954
  one_of(%w(28 29)) >> split(3,2) | # pager
890
955
  one_of(%w(16 18)) >> split(3,2) | # ISP
891
- one_of(%w(71 72 73 76)) >> split(3,3) | # mobile
956
+ one_of(%w(70 71 72 74 75 76 78 79)) >> split(3,3) | # mobile
957
+ one_of(%w(731 732 733 734 735 736 738 739)) >> split(2,3) | # mobile
958
+ one_of(%w(770)) >> split(2,3) | # wireless geographic; Port Moresby, National Capital District
959
+ one_of(%w(771)) >> split(2,3) | # wireless geographic; Momase region
960
+ one_of(%w(772)) >> split(2,3) | # wireless geographic; Highlands region
961
+ one_of(%w(7730)) >> split(1,3) | # wireless geographic; Southern region
962
+ one_of(%w(774)) >> split(2,3) | # wireless geographic; Islands region
963
+ one_of(%w(775)) >> split(2,3) | # mobile
892
964
  one_of(%w(80 81 82 83)) >> split(3,3) | # freephone
893
965
  one_of(%w(90 91 92 93)) >> split(3,3) | # geographic, ISDN
894
966
  one_of(%w(3 4 5 6 7 8 9)) >> split(3,3) # geographic
@@ -931,12 +1003,12 @@ Phony.define do
931
1003
  /\A800\d+\z/ => [3,3], # freephone
932
1004
  /\A830\d+\z/ => [3,3], # shared cost
933
1005
  /\A60\d+\z/ => [3,3], # wireless geographic
934
- /\A(72|75|76|77)\d+\z/ => [3,4], # mobile
1006
+ /\A(71|72|75|76|77)\d+\z/ => [3,4], # mobile
935
1007
  /\A84\d+\z/ => [3,4], # wireless geographic
936
1008
  /\A\d+\z/ => [2,3] # geographic
937
1009
  )
938
1010
 
939
- 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
940
1012
  country '687', none >> split(3,3) # New Caledonia (Territoire français d'outre-mer) http://www.wtng.info/wtng-687-nc.html
941
1013
  country '688', none >> split(5) # Tuvalu http://www.wtng.info/wtng-688-tv.html
942
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
@@ -952,7 +1024,10 @@ Phony.define do
952
1024
  country '698', todo # -
953
1025
  country '699', todo # -
954
1026
 
955
- 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
+
956
1031
  country '801', todo # -
957
1032
  country '802', todo # -
958
1033
  country '803', todo # -
@@ -960,7 +1035,7 @@ Phony.define do
960
1035
  country '805', todo # -
961
1036
  country '806', todo # -
962
1037
  country '807', todo # -
963
- country '808', todo # International Shared Cost Service (ISCS)
1038
+ country '808', none >> split(12) # International Shared Cost Service (ISCS)
964
1039
  country '809', todo # -
965
1040
 
966
1041
  country '830', todo # -
@@ -991,6 +1066,7 @@ Phony.define do
991
1066
  # Hong Kong, China
992
1067
 
993
1068
  country '852',
1069
+ match(/^(800)\d+$/) >> split(2, 4) |
994
1070
  none >> split(4,4)
995
1071
 
996
1072
  # Macao, China
@@ -1003,6 +1079,7 @@ Phony.define do
1003
1079
 
1004
1080
  # Lao People's Democratic Republic http://www.wtng.info/wtng-856-la.html, https://www.numberingplans.com
1005
1081
  country '856',
1082
+ trunk('0') |
1006
1083
  one_of('30') >> split(3,4) | # geographic
1007
1084
  one_of('20') >> split(4,4) | # mobile
1008
1085
  fixed(2) >> split(3,3) # geographic
@@ -1139,6 +1216,7 @@ Phony.define do
1139
1216
  one_of(%w(500 700 900)) >> split(3,3) | # premium rate
1140
1217
  one_of('800') >> matched_split(
1141
1218
  /\A\d{2}\z/ => [2],
1219
+ /\A\d{7}\z/ => [2,3,2],
1142
1220
  /\A\d+\z/ => [2,3,4]) | # freephone
1143
1221
  one_of(%w(50 52 54 55 56 58)) >> split(3,4) | # mobile
1144
1222
  one_of(%w(2 3 4 6 7 9)) >> split(3,4) |
@@ -1147,9 +1225,9 @@ Phony.define do
1147
1225
  # Israel (State of) (also works for numbers in Palestinian territories)
1148
1226
  country '972',
1149
1227
  trunk('0') |
1150
- one_of('1') >> split(3,3,3) | # special numbers
1151
- one_of('2', '3', '4', '8', '9') >> split(3,4) | # 1 digit ndc
1152
- 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
1153
1231
 
1154
1232
  country '973', none >> split(4,4..4) # Bahrain (Kingdom of) http://www.itu.int/oth/T0202000011/en
1155
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.
@@ -185,6 +193,10 @@ module Phony
185
193
  #
186
194
  ndc_needed = hints[:ndc]
187
195
  return false if ndc_needed && !(ndc_needed === ndc)
196
+
197
+ # If there is no local part, we can assume it's not a plausible number.
198
+ # (Or, not defined correctly in Phony yet)
199
+ return false unless local
188
200
 
189
201
  # Local code specific checks.
190
202
  #
@@ -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..15) === 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