Skip to content
This repository
Browse code

added uk

  • Loading branch information...
commit c8173d5b1bcb3ba08513ebe34c0e5c1f90453b0d 2 parents 060a37b + 4712a10
Keith Bingman authored February 11, 2011
4  README.textile
Source Rendered
@@ -7,8 +7,8 @@ This gem can normalize, format and split E164 numbers.
7 7
 
8 8
 The (admittedly crazy) *goal* of this Gem is to be able to format/split all phone numbers in the world.
9 9
 
10  
-Currently handles Afghan, Austrian, Australian, Belgian, Brazilian, Chilean, Chinese, Czech, Danish, Dutch, Egyptian, French, German, Greek, Hungarian, Italian, Malaysian, (The) Netherlands, New Zealand, Norwegian, Peruvian, Polish, Russian, Romanian, South African, South Korean, Spanish, Swedish, Swiss, Turkish, Liechtenstein, US, Venezuelan numbers.
11  
-And to some extend, all others. Just try if it works for you.
  10
+Currently handles Afghan, Austrian, Australian, Belgian, Brazilian, Chilean, Chinese, Czech, Danish, Dutch, Egyptian, French, German, Greek, Hungarian, Italian, Malaysian, (The) Netherlands, New Zealand, Norwegian, Peruvian, Polish, Russian, Romanian, South African, South Korean, Spanish, Swedish, Swiss, Turkish, Liechtenstein, US, and Venezuelan numbers.
  11
+And to some extent, all others. Just try if it works for you.
12 12
 
13 13
 If it doesn't, please "enter an issue":http://github.com/floere/phony/issues.
14 14
 
20  history.textile
Source Rendered
... ...
@@ -1,3 +1,23 @@
  1
+h2. Version 1.2.9
  2
+
  3
+* hanke: Clashing mobile numbers with Liège in Belgium fixed. 
  4
+
  5
+h2. Version 1.2.8
  6
+
  7
+* hanke: Fix by glebm, thanks! Italian number handling did produce errors in Ruby 1.8. Closes "issue 13":https://github.com/floere/phony/issues#issue/13.
  8
+
  9
+h2. Version 1.2.7
  10
+
  11
+* hanke: Italian numbers are correctly normalized: Phony.normalize('+390909709511').should == '390909709511' (Zero in front of area code is not removed, closes "issue 12":https://github.com/floere/phony/issues#issue/12)
  12
+
  13
+h2. Version 1.2.6
  14
+
  15
+* hanke: Italy.
  16
+
  17
+h2. Version 1.2.5
  18
+
  19
+* hanke: Portugal. Polish landline (not mobile).
  20
+
1 21
 h2. Version 1.2.4
2 22
 
3 23
 * hanke: Malaysia.
2  lib/phony.rb
@@ -24,9 +24,11 @@
24 24
 require File.expand_path '../phony/countries/malaysia', __FILE__
25 25
 require File.expand_path '../phony/countries/netherlands', __FILE__
26 26
 require File.expand_path '../phony/countries/peru', __FILE__
  27
+require File.expand_path '../phony/countries/portugal', __FILE__
27 28
 require File.expand_path '../phony/countries/romania', __FILE__
28 29
 require File.expand_path '../phony/countries/south_korea', __FILE__
29 30
 require File.expand_path '../phony/countries/sweden', __FILE__
  31
+require File.expand_path '../phony/countries/united_kingdom', __FILE__
30 32
 
31 33
 require File.expand_path '../phony/country_codes', __FILE__
32 34
 
15  lib/phony/countries/all_other.rb
@@ -72,17 +72,18 @@ def self.mapping
72 72
                         # }
73 73
                   ),
74 74
           '43' => Countries::Austria,
75  
-          '44' => fixed(2), # TODO United Kingdom of Great Britain and Northern Ireland
  75
+          '44' => Countries::UnitedKingdom, # TODO United Kingdom of Great Britain and Northern Ireland
76 76
           '45' => fixed(2,  # Denmark
77  
-                        :local_format => [2, 2, 2]
78  
-                  ),
  77
+                        :local_format => [2, 2, 2],
  78
+                        :service_ndcs => %w{112 114}
  79
+                  ), # Denmark has no NDC, but 4 groups of 2 digits. I'm faking it here.
79 80
           '46' => Countries::Sweden,
80 81
           '47' => fixed(4,  # Norway
81 82
                     :local_format => [4]
82 83
                   ),
83  
-          '48' => fixed(2, # Poland (Republic of)
84  
-                        :local_format => [1, 3, 3] # Approximation. Correct would be 48-xxx-xxx-xxx
85  
-                  ),
  84
+          '48' => fixed(3, # Poland (Republic of)
  85
+                        :local_format => [3, 3] # Although the NDCs are 2 digits, the representation is 3 digits.
  86
+                  ), # Note: http://wapedia.mobi/en/Telephone_numbers_in_Poland, mobile not yet correct
86 87
           '49' => Countries::Germany,
87 88
           
88 89
           '51' => Countries::Peru,
@@ -220,7 +221,7 @@ def self.mapping
220 221
           '299' => fixed(2), # Greenland
221 222
 
222 223
           '350' => fixed(2), # Gibraltar
223  
-          '351' => fixed(2), # Portugal
  224
+          '351' => Countries::Portugal, # Portugal
224 225
           '352' => fixed(2), # Luxembourg
225 226
           '353' => fixed(2), # Ireland
226 227
           '354' => fixed(2), # Iceland
3  lib/phony/countries/belgium.rb
@@ -3,12 +3,13 @@
3 3
 # Taken from: http://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium
4 4
 #
5 5
 Phony::Countries::Belgium = Phony::Country.configured :local_format => [3, 5],
  6
+ :mobile_local_format => [6],
6 7
  :ndc_fallback_length => 2,
7 8
  :ndc_mapping => {
8 9
    :landline => [
9 10
                  '2', # Brussels (Bruxelles/Brussel)
10 11
                  '3', # Antwerpen (Antwerp), Sint-Niklaas
11  
-                 # '4', # Liège (Luik), Voeren (Fourons)
  12
+                 '4', # Liège (Luik), Voeren (Fourons)
12 13
                  '9', # Gent (Ghent/Gand)
13 14
                 ],
14 15
    :mobile => [
2  lib/phony/countries/egypt.rb
@@ -2,6 +2,8 @@
2 2
 #
3 3
 # http://en.wikipedia.org/wiki/Telephone_numbers_in_Egypt
4 4
 #
  5
+# TODO If the fallback length is 2, why do I list length 2 landlines?
  6
+#
5 7
 Phony::Countries::Egypt = Phony::Country.configured :local_format => [8],
6 8
   :local_special_format => [7],
7 9
   :ndc_fallback_length => 2,
2  lib/phony/countries/hungary.rb
@@ -13,7 +13,7 @@
13 13
     :landline => [
14 14
                    '1'
15 15
                   ],
16  
-    :mobile   => [], # TODO
  16
+    # TODO :mobile   => [],
17 17
     :service  => [
18 18
                    '104',
19 19
                    '105',
241  lib/phony/countries/italy.rb
@@ -4,102 +4,153 @@
4 4
 #
5 5
 Phony::Countries::Italy = Phony::Country.configured :local_format => [3, 4],
6 6
   :local_special_format => [3, 3],
  7
+  :normalize => false,
7 8
   :ndc_fallback_length => 3,
8 9
   :ndc_mapping => {
9 10
     :landline => [
10  
-                   '10', # Genoa
11  
-                   '11', # Turin
12  
-                  '122', # Sestrieres, Bardonecchia and other Susa Valley mountain resorts
13  
-                  '125', # Ivrea
14  
-                  '131', # Alessandria
15  
-                  '141', # Asti
16  
-                   '15', # Biella
17  
-                  '161', # Vercelli
18  
-                  '165', # Aosta, Courmayeur
19  
-                  '166', # Cervinia
20  
-                  '171', # Cuneo 
21  
-                  '185', # Tigullio
22  
-                  '187', # La Spezia and Cinque Terre
23  
-                   '19', # Savona
24  
-                    '2', # Milan
25  
-                   '30', # Brescia
26  
-                   '31', # Como
27  
-                  '321', # Novara
28  
-                  '331', # Varese
29  
-                  '341', # Lecco
30  
-                  '342', # Sondrio
31  
-                   '35', # Bergamo
32  
-                  '369', # Mediaset Cologno Monzese and Segrate TV studios, geographical rate (used for polls, television programs, etc.)
33  
-                  '376', # Mantua
34  
-                  '382', # Pavia
35  
-                   '39', # Monza
36  
-                   '40', # Trieste
37  
-                   '41', # Venice
38  
-                  '425', # Rovigo
39  
-                  '432', # Udine
40  
-                  '444', # Vicenza
41  
-                   '45', # Verona
42  
-                  '461', # Trento
43  
-                  '471', # Bolzano
44  
-                  '481', # Gorizia
45  
-                   '49', # Padua
46  
-                   '50', # Pisa
47  
-                   '51', # Bologna
48  
-                  '521', # Parma
49  
-                  '522', # Reggio nell'Emilia
50  
-                  '523', # Piacenza
51  
-                  '532', # Ferrara
52  
-                  '536', # Sassuolo
53  
-                  '541', # Rimini
54  
-                  '549', # San Marino (Most Serene Republic of San Marino)
55  
-                   '55', # Florence
56  
-                  '573', # Pistoia
57  
-                  '577', # Siena
58  
-                  '586', # Livorno
59  
-                   '59', # Modena
60  
-                    '6', # Rome (including State of Vatican City) and Aprilia
61  
-                   '70', # Cagliari
62  
-                   '71', # Ancona
63  
-                   '75', # Perugia
64  
-                  '769', # RAI Saxa Rubra Studios, geographical rate (used for polls, television programs, etc.)
65  
-                  '771', # Fondi
66  
-                  '773', # Latina
67  
-                  '775', # Frosinone
68  
-                  '789', # Olbia and Costa Smeralda 
69  
-                   '79', # Sassari
70  
-                   '80', # Bari
71  
-                   '81', # Naples
72  
-                  '823', # Caserta
73  
-                  '824', # Benevento
74  
-                  '825', # Avellino 
75  
-                   '85', # Pescara
76  
-                  '865', # Isernia 
77  
-                  '871', # Chieti
78  
-                  '872', # Lanciano
79  
-                  '873', # Vasto
80  
-                  '874', # Campobasso 
81  
-                  '875', # Termoli
82  
-                  '881', # Foggia
83  
-                   '89', # Salerno 
84  
-                   '90', # Messina
85  
-                   '91', # Palermo
86  
-                   '92', # Agrigento
87  
-                   '95', # Catania
88  
-                  '961', # Catanzaro 
89  
-                  '965', # Reggio di Calabria
90  
-                  '971', # Potenza
91  
-                   '99', # Taranto
92  
-                  ],
93  
-    :mobile => [], # TODO
  11
+      '010', # Genoa
  12
+      '011', # Turin
  13
+     '0122', # Sestrieres, Bardonecchia and other Susa Valley mountain resorts
  14
+     '0125', # Ivrea
  15
+     '0131', # Alessandria
  16
+     '0141', # Asti
  17
+      '015', # Biella
  18
+     '0161', # Vercelli
  19
+     '0165', # Aosta, Courmayeur
  20
+     '0166', # Cervinia
  21
+     '0171', # Cuneo 
  22
+     '0185', # Tigullio
  23
+     '0187', # La Spezia and Cinque Terre
  24
+      '019', # Savona
  25
+       '02', # Milan
  26
+      '030', # Brescia
  27
+      '031', # Como
  28
+     '0321', # Novara
  29
+     '0331', # Varese
  30
+     '0341', # Lecco
  31
+     '0342', # Sondrio
  32
+      '035', # Bergamo
  33
+     '0369', # Mediaset Cologno Monzese and Segrate TV studios, geographical rate (used for polls, television programs, etc.)
  34
+     '0376', # Mantua
  35
+     '0382', # Pavia
  36
+      '039', # Monza
  37
+      '040', # Trieste
  38
+      '041', # Venice
  39
+     '0425', # Rovigo
  40
+     '0432', # Udine
  41
+     '0444', # Vicenza
  42
+      '045', # Verona
  43
+     '0461', # Trento
  44
+     '0471', # Bolzano
  45
+     '0481', # Gorizia
  46
+      '049', # Padua
  47
+      '050', # Pisa
  48
+      '051', # Bologna
  49
+     '0521', # Parma
  50
+     '0522', # Reggio nell'Emilia
  51
+     '0523', # Piacenza
  52
+     '0532', # Ferrara
  53
+     '0536', # Sassuolo
  54
+     '0541', # Rimini
  55
+     '0549', # San Marino (Most Serene Republic of San Marino)
  56
+      '055', # Florence
  57
+     '0573', # Pistoia
  58
+     '0577', # Siena
  59
+     '0586', # Livorno
  60
+      '059', # Modena
  61
+       '06', # Rome (including State of Vatican City) and Aprilia
  62
+      '070', # Cagliari
  63
+      '071', # Ancona
  64
+      '075', # Perugia
  65
+     '0769', # RAI Saxa Rubra Studios, geographical rate (used for polls, television programs, etc.)
  66
+     '0771', # Fondi
  67
+     '0773', # Latina
  68
+     '0775', # Frosinone
  69
+     '0789', # Olbia and Costa Smeralda 
  70
+      '079', # Sassari
  71
+      '080', # Bari
  72
+      '081', # Naples
  73
+     '0823', # Caserta
  74
+     '0824', # Benevento
  75
+     '0825', # Avellino 
  76
+      '085', # Pescara
  77
+     '0865', # Isernia 
  78
+     '0871', # Chieti
  79
+     '0872', # Lanciano
  80
+     '0873', # Vasto
  81
+     '0874', # Campobasso 
  82
+     '0875', # Termoli
  83
+     '0881', # Foggia
  84
+      '089', # Salerno 
  85
+      '090', # Messina
  86
+      '091', # Palermo
  87
+      '092', # Agrigento
  88
+      '095', # Catania
  89
+     '0961', # Catanzaro 
  90
+     '0965', # Reggio di Calabria
  91
+     '0971', # Potenza
  92
+      '099', # Taranto
  93
+     ],
  94
+    :mobile => [
  95
+     '310',
  96
+     '31100',
  97
+     '31101',
  98
+     '31102',
  99
+     '31103',
  100
+     '31104',
  101
+     '31105',
  102
+     '313',
  103
+     '319',
  104
+     ('3200'..'3209').to_a,
  105
+     '322',
  106
+     ('3230'..'3239').to_a,
  107
+     ('3270'..'3279').to_a,
  108
+     ('3280'..'3289').to_a,
  109
+     ('3290'..'3299').to_a,
  110
+     ('3300'..'3309').to_a,
  111
+     '331',
  112
+     ('3330'..'3339').to_a,
  113
+     ('3340'..'3349').to_a,
  114
+     ('3350'..'3359').to_a,
  115
+     ('3360'..'3369').to_a,
  116
+     ('3370'..'3379').to_a,
  117
+     ('3380'..'3389').to_a,
  118
+     ('3390'..'3399').to_a,
  119
+     ('3400'..'3409').to_a,
  120
+     '341',
  121
+     ('3430'..'3439').to_a,
  122
+     '345',
  123
+     ('3460'..'3469').to_a,
  124
+     ('3470'..'3479').to_a,
  125
+     ('3480'..'3489').to_a,
  126
+     ('3490'..'3499').to_a,
  127
+     '350',
  128
+     ('3600'..'3609').to_a,
  129
+     '361',
  130
+     '362',
  131
+     ('3630'..'3639').to_a,
  132
+     ('3660'..'3669').to_a,
  133
+     ('3680'..'3689').to_a,
  134
+     '370',
  135
+     '373',
  136
+     '377',
  137
+     ('3800'..'3809').to_a,
  138
+     ('3880'..'3889').to_a,
  139
+     ('3890'..'3899').to_a,
  140
+     ('3900'..'3909').to_a,
  141
+     ('3910'..'3919').to_a,
  142
+     ('3920'..'3929').to_a,
  143
+     ('3930'..'3939').to_a,
  144
+     '397'
  145
+     ].flatten,
94 146
     :service => [ # Not exhaustive.
95  
-                  '112',
96  
-                  '113',
97  
-                  '115',
98  
-                  '117',
99  
-                  '118',
100  
-                  '878',
101  
-                  '1515',
102  
-                  '1518',
103  
-                  '1530'
104  
-                ]
105  
-  }
  147
+      '112',
  148
+      '113',
  149
+      '115',
  150
+      '117',
  151
+      '118',
  152
+      '1515',
  153
+      '1518',
  154
+      '1530'
  155
+    ]
  156
+  }
2  lib/phony/countries/peru.rb
@@ -3,7 +3,7 @@
3 3
 # http://en.wikipedia.org/wiki/Telephone_numbers_in_Peru
4 4
 #
5 5
 Phony::Countries::Peru = Phony::Country.configured :local_format => [4, 4],
6  
-  :local_special_format => [3, 3],
  6
+  :service_local_format => [3, 3],
7 7
   :ndc_fallback_length => 2,
8 8
   :ndc_mapping => {
9 9
     :landline => [
16  lib/phony/countries/portugal.rb
... ...
@@ -0,0 +1,16 @@
  1
+# Portuguese phone numbers.
  2
+#
  3
+# http://en.wikipedia.org/wiki/Telephone_numbers_in_Portugal
  4
+#
  5
+Phony::Countries::Portugal = Phony::Country.configured :local_format => [3, 4],
  6
+  :local_special_format => [3, 3],
  7
+  :local_mobile_format => [3, 3],
  8
+  :ndc_fallback_length => 3,
  9
+  :ndc_mapping => {
  10
+    :landline => [
  11
+      '21', # Lisboa
  12
+      '22', # Porto
  13
+    ],
  14
+    :mobile => ('90'..'99').to_a,
  15
+    :service => %w{700 800} # Not exhaustive.
  16
+  }
1  lib/phony/countries/romania.rb
@@ -4,6 +4,7 @@
4 4
 #
5 5
 Phony::Countries::Romania = Phony::Country.configured :local_format => [3, 4], # Also captures 3, 3 on a fallback.
6 6
   :local_special_format => [3, 3],
  7
+  :local_mobile_format => [3, 4],
7 8
   :ndc_fallback_length => 3,
8 9
   :ndc_mapping => {
9 10
     :landline => [
88  lib/phony/countries/united_kingdom.rb
... ...
@@ -0,0 +1,88 @@
  1
+# The United Kingdom uses a variable-length ndc code, thus we use a separate file to not let all_other.rb explode.
  2
+#
  3
+# Note: The United Kingdom uses a variable ndc format from length 2 to 4.
  4
+#
  5
+Phony::Countries::UnitedKingdom = Phony::Country.configured :local_format => [4, 6],
  6
+                                                            :local_special_format => [8],
  7
+                                                            :mobile_local_format => [6],
  8
+                                                            :ndc_fallback_length => 4,
  9
+                                                            :ndc_mapping => {
  10
+                                                              :landline => [
  11
+'20',   # London
  12
+'23',   # Southampton, Portsmith
  13
+'24',   # Coventry
  14
+'28',   # Northern Ireland
  15
+'29',   # Caridff
  16
+'113',  # Leeds
  17
+'114',  # Sheffield	
  18
+'115',  # Nottingham	
  19
+'116',  # Leicester	
  20
+'117',  # Bristol	
  21
+'118',  # Reading
  22
+'121',  # Birmingham
  23
+'131',  # Edinburgh
  24
+'141',  # Glasgow
  25
+'151',  # Liverpool
  26
+'161',  # Manchester
  27
+'171',  # Used for inner London until 2000
  28
+'181',  # Used for outer London until 2000
  29
+'191',  # Tyne and Wear/County Durham
  30
+'1204', # Bolton
  31
+'1224', # Aberdeen
  32
+'1244', # Chester
  33
+'1382', # Dundee
  34
+'1429', # Hartlepool
  35
+'1482', # Hull
  36
+'1527', # Redditch	
  37
+'1582', # Luton
  38
+'1670', # Morpeth
  39
+'1730', # Petersfield
  40
+'1736', # Penzance
  41
+'1750', # Selkirk
  42
+'1772', # Preston
  43
+'1853', # Ullapool
  44
+'1900', # Workington
  45
+'1946', # Whitehaven
  46
+'1947', # Whitby
  47
+# 5 Digit prefixes
  48
+'13873', # Langholm
  49
+'15242', # Hornby
  50
+'15394', # Hawkshead
  51
+'15395', # Grange-over-Sands
  52
+'15396', # Sedbergh
  53
+'16973', # Wigton
  54
+'16974', # Raughton Head
  55
+'16977', # Brampton[3][4]
  56
+'17683', # Appleby
  57
+'17684', # Pooley Bridge
  58
+'17687', # Keswick
  59
+'19467', # Gosforth
  60
+
  61
+],
  62
+:mobile => [
  63
+'7400',
  64
+'7500',
  65
+'7600',
  66
+'7700',
  67
+'7780',
  68
+'7800',
  69
+'7900',
  70
+],
  71
+:service => [
  72
+'800', # Freephone
  73
+'808', # Freephone
  74
+'842',
  75
+'843',
  76
+'844',
  77
+'845', 
  78
+'870',
  79
+'871',
  80
+'872',
  81
+'873',
  82
+'900', # Premium rate content services
  83
+'908', # Sexual entertainment services
  84
+'909', # Sexual entertainment services
  85
+'982', # Sexual entertainment services
  86
+]
  87
+}
  88
+
46  lib/phony/country.rb
@@ -4,19 +4,39 @@ module Phony
4 4
   #
5 5
   class Country
6 6
     
7  
-    def initialize national_code, special_code = nil
  7
+    def initialize national_code, special_code = nil, mobile_code = nil
8 8
       @national_code = national_code
9 9
       @special_code  = special_code
  10
+      @mobile_code   = mobile_code
10 11
     end
11 12
     
12 13
     #
13 14
     #
14 15
     def split national_number
15 16
       ndc, *rest = @special_code.split national_number if @special_code
16  
-      ndc, *rest = @national_code.split national_number unless rest && !rest.empty?
  17
+      return [ndc, *rest] if rest && !rest.empty?
  18
+      ndc, *rest = @mobile_code.split national_number if @mobile_code
  19
+      return [ndc, *rest] if rest && !rest.empty?
  20
+      ndc, *rest = @national_code.split national_number
17 21
       [ndc, *rest]
18 22
     end
19 23
     
  24
+    # Removes 0s from partially normalized numbers
  25
+    # such as 410443643533.
  26
+    # 
  27
+    # Example:
  28
+    #   410443643533 -> 41443643533
  29
+    #
  30
+    # In some cases it doesn't, like Italy.
  31
+    #
  32
+    def normalize national_number
  33
+      normalized = @special_code.normalize national_number if @special_code
  34
+      return normalized if normalized && !normalized.empty?
  35
+      normalized = @mobile_code.normalize national_number if @mobile_code
  36
+      return normalized if normalized && !normalized.empty?
  37
+      @national_code.normalize national_number
  38
+    end
  39
+    
20 40
     # Is this national number a vanity number?
21 41
     #
22 42
     def vanity? national_number
@@ -50,20 +70,27 @@ def vanity_to_number vanity_number
50 70
     # If that is not the case, I will expand the framework.
51 71
     #
52 72
     def self.configured options = {}
  73
+      normalize            = options[:normalize]
53 74
       ndc_fallback_length  = options[:ndc_fallback_length]
54 75
       ndc_mapping          = options[:ndc_mapping] || raise("No ndc_mapping given!")
55 76
       
56 77
       national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
57 78
       local_splitter    = Phony::LocalSplitter.instance_for options[:local_format] || [3, 2, 2]
58  
-      national_code     = Phony::NationalCode.new national_splitter, local_splitter
  79
+      national_code     = Phony::NationalCode.new national_splitter, local_splitter, normalize
59 80
       
60 81
       if ndc_mapping[:service]
61 82
         service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => ndc_mapping[:service]
62 83
         service_local_splitter    = Phony::LocalSplitter.instance_for options[:service_local_format] || [3, 6]
63  
-        service_code              = Phony::NationalCode.new service_national_splitter, service_local_splitter
  84
+        service_code              = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
  85
+      end
  86
+      if ndc_mapping[:mobile]
  87
+        mobile_local_format      = options[:mobile_local_format] || options[:local_format] || [9]
  88
+        mobile_national_splitter = Phony::NationalSplitters::Variable.new nil, :mobile => ndc_mapping[:mobile]
  89
+        mobile_local_splitter    = Phony::LocalSplitter.instance_for mobile_local_format
  90
+        mobile_code              = Phony::NationalCode.new mobile_national_splitter, mobile_local_splitter, normalize
64 91
       end
65 92
       
66  
-      new national_code, service_code
  93
+      new national_code, service_code, mobile_code
67 94
     end
68 95
     
69 96
     # Gets a configured country instance with fixed length ndc code.
@@ -78,20 +105,21 @@ def self.configured options = {}
78 105
     #                       :service_ndcs         => ['800']
79 106
     #
80 107
     def self.fixed options = {}
  108
+      normalize    = options[:normalize]
81 109
       ndc_length   = options[:ndc_length]
82 110
       service_ndcs = options[:service_ndcs]
83 111
       local_format = options[:local_format]
84 112
       
85 113
       national_splitter = Phony::NationalSplitters::Fixed.new ndc_length
86 114
       local_splitter    = Phony::LocalSplitter.instance_for local_format || [3, 2, 2]
87  
-      national_code     = Phony::NationalCode.new national_splitter, local_splitter
  115
+      national_code     = Phony::NationalCode.new national_splitter, local_splitter, normalize
88 116
       
89 117
       service_code = nil
90 118
       if service_ndcs
91  
-        service_local_format      = options[:service_local_format] || local_format
  119
+        service_local_format      = options[:service_local_format] || local_format || [3, 3]
92 120
         service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => service_ndcs
93  
-        service_local_splitter    = Phony::LocalSplitter.instance_for service_local_format || [3, 3]
94  
-        service_code              = Phony::NationalCode.new service_national_splitter, service_local_splitter
  121
+        service_local_splitter    = Phony::LocalSplitter.instance_for service_local_format
  122
+        service_code              = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
95 123
       end
96 124
       
97 125
       new national_code, service_code
14  lib/phony/country_codes.rb
@@ -8,7 +8,8 @@ def normalize number
8 8
       # Remove non-digit chars.
9 9
       #
10 10
       number.gsub! /\D*/, ''
11  
-      remove_relative_zeros! number
  11
+      national_handler, cc, rest = split_cc number
  12
+      '%s%s' % [cc, national_handler.normalize(rest)]
12 13
     end
13 14
     
14 15
     # Splits this number into cc, ndc and locally split number parts.
@@ -79,17 +80,6 @@ def split_cc rest
79 80
       # This line is never reached as CCs are in prefix code.
80 81
     end
81 82
     
82  
-    # Removes 0s from partially normalized numbers
83  
-    # such as 410443643533.
84  
-    # 
85  
-    # Example:
86  
-    #   410443643533 -> 41443643533
87  
-    #
88  
-    def remove_relative_zeros! phone_number
89  
-      _, cc, rest = split_cc phone_number
90  
-      '%s%s' % [cc, rest].collect! { |code| code.gsub(/^0+/, '') }
91  
-    end
92  
-    
93 83
     # Cached mapping of all countries.
94 84
     #
95 85
     def mapping
12  lib/phony/national_code.rb
@@ -8,9 +8,10 @@ class NationalCode
8 8
     
9 9
     #
10 10
     #
11  
-    def initialize national_splitter, local_splitter
  11
+    def initialize national_splitter, local_splitter, normalize = nil
12 12
       @national_splitter = national_splitter
13 13
       @local_splitter    = local_splitter
  14
+      @normalize         = !(normalize == false) # if nil, true (default), if false, false, if true, true.
14 15
     end
15 16
     
16 17
     # Split gets a number without country code and splits it into
@@ -22,6 +23,15 @@ def split national_number
22 23
       [ndc, *@local_splitter.split(rest)]
23 24
     end
24 25
     
  26
+    # Split gets a number without country code and removes a relative zero.
  27
+    #
  28
+    # Note: Some cases, like Italy, don't remove the relative zero.
  29
+    #
  30
+    def normalize national_number
  31
+      return national_number unless @normalize
  32
+      national_number.gsub(/^0+/, '')
  33
+    end
  34
+    
25 35
   end
26 36
   
27 37
 end
2  phony.gemspec
... ...
@@ -1,6 +1,6 @@
1 1
 Gem::Specification.new do |s|
2 2
   s.name = 'phony'
3  
-  s.version = '1.2.4'
  3
+  s.version = '1.2.9'
4 4
   s.authors = ['Florian Hanke']
5 5
   s.email = 'florian.hanke+phony@gmail.com'
6 6
   s.homepage = 'http://github.com/floere/phony'
6  spec/lib/phony/countries/belgium_spec.rb
... ...
@@ -1,3 +1,6 @@
  1
+# encoding: utf-8
  2
+#
  3
+
1 4
 require 'spec_helper'
2 5
 
3 6
 describe Phony::Countries::Belgium do
@@ -20,11 +23,10 @@
20 23
       @belgium.split('95551914').should == ['9', '555', '1914']
21 24
     end
22 25
     it "should handle Liège" do
23  
-      pending 'left out for now, as it messes up the mobile numbers'
24 26
       @belgium.split('45551414').should == ['4', '555', '1414']
25 27
     end
26 28
     it "should handle some mobile services" do
27  
-      @belgium.split('475279584').should == ['475', '279', '584']
  29
+      @belgium.split('475279584').should == ['475', '279584']
28 30
     end
29 31
 
30 32
   end
21  spec/lib/phony/countries/portugal_spec.rb
... ...
@@ -0,0 +1,21 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Phony::Countries::Portugal do
  4
+  
  5
+  before(:each) do
  6
+    @portugal = Phony::Countries::Portugal
  7
+  end
  8
+  
  9
+  describe "split" do
  10
+    it "works with Lisboa" do
  11
+      @portugal.split('211231234').should == ['21', '123', '1234']
  12
+    end
  13
+    it "works with Abrantes" do
  14
+      @portugal.split('241123123').should == ['241', '123', '123']
  15
+    end
  16
+    it 'works with a mobile' do
  17
+      @portugal.split('931231234').should == ['93', '123', '1234']
  18
+    end
  19
+  end
  20
+  
  21
+end
47  spec/lib/phony/countries/united_kingdom_spec.rb
... ...
@@ -0,0 +1,47 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Phony::Countries::UnitedKingdom do
  4
+  
  5
+  before(:each) do
  6
+    @uk = Phony::Countries::UnitedKingdom
  7
+  end
  8
+  
  9
+  # Note: Many Specs
  10
+  #
  11
+  describe "split" do
  12
+    it "should handle London" do
  13
+      @uk.split('2045671113').should == ['20', '4567', '1113']
  14
+    end
  15
+    it "should handle Cardiff" do
  16
+      @uk.split('2076229901').should == ['20', '7622', '9901']
  17
+    end
  18
+    it "should handle Leeds" do
  19
+      @uk.split('1136770011').should == ['113', '677', '0011']
  20
+    end
  21
+    it "should handle Dundee" do
  22
+      @uk.split('1382229845').should == ['1382', '229845']
  23
+    end
  24
+    it "should handle Bolten" do
  25
+      @uk.split('120499532').should == ['1204', '99532']
  26
+    end
  27
+    it "should handle Sedbergh" do
  28
+      @uk.split('1539618756 ').should ==['15396', '18756']
  29
+    end
  30
+    it "should handle Mobile Numbers" do
  31
+      @uk.split('7780605207').should == ['7780', '605207']
  32
+    end
  33
+    it "should handle service numbers with 800 [regression]" do
  34
+      @uk.split('8005878323').should == ['800', '587', '8323']
  35
+    end
  36
+    #
  37
+    # 44 116 xxx xxxx Leicester
  38
+    # 44 131 xxx xxxx Edinburgh
  39
+    # 44 151 xxx xxxx Liverpool
  40
+    # 44 1382  xxxxxx Dundee
  41
+    # 44 1386  xxxxxx Evesham
  42
+    # 44 1865  xxxxxx Oxford
  43
+    # 44 153 96 xxxxx Sedbergh
  44
+    # 44 169 77  xxxx Brampton
  45
+  end
  46
+  
  47
+end
6  spec/lib/phony/country_codes_spec.rb
@@ -12,12 +12,6 @@
12 12
     end
13 13
   end
14 14
   
15  
-  describe 'remove_relative_zeros' do
16  
-    it "should remove an ndc zero from an almost normalized number and return it" do
17  
-      @countries.remove_relative_zeros!('410443643533').should == '41443643533'
18  
-    end
19  
-  end
20  
-  
21 15
   describe 'formatted' do
22 16
     it 'formats correctly' do
23 17
       @countries.formatted('41443643532', :format => :international, :spaces => :-).should == '+41-44-364-35-32'
8  spec/lib/phony/country_spec.rb
@@ -7,6 +7,7 @@
7 7
       before(:each) do
8 8
         @country = Phony::Country.configured :local_format         => [3, 2, 2],
9 9
                                              :service_local_format => [3, 3],
  10
+                                             :mobile_local_format  => [1, 2, 4],
10 11
                                              :ndc_fallback_length  => 4,
11 12
                                              :ndc_mapping => {
12 13
                                                :normal  => ['44'],
@@ -21,7 +22,7 @@
21 22
         @country.split('800333666').should == ['800', '333', '666']
22 23
       end
23 24
       it 'works with mobile numbers' do
24  
-        @country.split('764333532').should == ['76', '433', '35', '32']
  25
+        @country.split('764333532').should == ['76', '4', '33', '3532']
25 26
       end
26 27
       it 'uses the fallback if it is not in the mapping' do
27 28
         @country.split('123456789').should == ['1234', '567', '89']
@@ -63,6 +64,11 @@
63 64
         @switzerland.split('443643532').should == ['44', '364', '35', '32']
64 65
       end
65 66
     end
  67
+    describe 'normalize' do
  68
+      it "should handle ZH" do
  69
+        @switzerland.normalize('0443643532').should == '443643532'
  70
+      end
  71
+    end
66 72
   end
67 73
   
68 74
   context "without special cases" do
42  spec/lib/phony/national_code_spec.rb
@@ -16,6 +16,12 @@
16 16
       it 'splits correctly' do
17 17
         @national.split('44364353').should == ['44', '364', '35', '3']
18 18
       end
  19
+      it 'normalizes correctly' do
  20
+        @national.normalize('044364353').should == '44364353'
  21
+      end
  22
+      it 'normalizes correctly' do
  23
+        @national.normalize('44364353').should == '44364353'
  24
+      end
19 25
     end
20 26
     context 'with fixed ndc (French)' do
21 27
       before(:each) do
@@ -30,6 +36,42 @@
30 36
       it 'splits correctly' do
31 37
         @national.split('14227818').should == ['1', '42', '27', '81', '8']
32 38
       end
  39
+      it 'normalizes correctly' do
  40
+        @national.normalize('0142278186').should == '142278186'
  41
+      end
  42
+      it 'normalizes correctly' do
  43
+        @national.normalize('142278186').should == '142278186'
  44
+      end
  45
+    end
  46
+    context 'normalizing' do
  47
+      context 'false' do
  48
+        before(:each) do
  49
+          @national = Phony::NationalCode.new nil, nil, false
  50
+        end
  51
+        it 'normalizes an italian case correctly' do
  52
+          @national.normalize('0909709511').should == '0909709511'
  53
+        end
  54
+      end
  55
+      context 'true' do
  56
+        before(:each) do
  57
+          national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
  58
+          local_splitter    = Phony::LocalSplitter.instance_for [3, 2, 2]
  59
+          @national = Phony::NationalCode.new national_splitter, local_splitter, true
  60
+        end
  61
+        it 'normalizes a swiss case correctly' do
  62
+          @national.normalize('044364353').should == '44364353'
  63
+        end
  64
+      end
  65
+      context 'nil (true)' do
  66
+        before(:each) do
  67
+          national_splitter = Phony::NationalSplitters::Fixed.instance_for 2
  68
+          local_splitter    = Phony::LocalSplitter.instance_for [3, 2, 2]
  69
+          @national = Phony::NationalCode.new national_splitter, local_splitter, nil
  70
+        end
  71
+        it 'normalizes a swiss case correctly' do
  72
+          @national.normalize('044364353').should == '44364353'
  73
+        end
  74
+      end
33 75
     end
34 76
   end
35 77
   
109  spec/lib/phony_spec.rb
@@ -8,9 +8,17 @@
8 8
     it 'handles afghan numbers' do
9 9
       Phony.split('93201234567').should == ['93', '20', '1234567'] # Kabul
10 10
     end
11  
-    it "should handle austrian numbers" do
  11
+    it "handles austrian numbers" do
12 12
       Phony.split('43198110').should == ['43', '1', '98110']
13 13
     end
  14
+    it 'handles belgian numbers' do
  15
+      Phony.split('3235551212').should == ['32', '3', '555', '1212']   # Antwerpen
  16
+      Phony.split('32505551212').should == ['32', '50', '555', '1212'] # Brugge
  17
+      Phony.split('3225551212').should == ['32', '2', '555', '1212']   # Brussels
  18
+      Phony.split('3295551914').should == ['32', '9', '555', '1914']   # Gent
  19
+      Phony.split('3245551414').should == ['32', '4', '555', '1414']   # Liège
  20
+      Phony.split('32475279584').should == ['32', '475', '279584']     # mobile
  21
+    end
14 22
     it 'handles brazilian numbers' do
15 23
       Phony.split('551112341234').should == ['55', '11', '1234', '1234']
16 24
     end
@@ -31,14 +39,14 @@
31 39
       Phony.split('31201234567').should == ['31', '20', '1234567']
32 40
       Phony.split('31222123456').should == ['31', '222', '123456']
33 41
     end
34  
-    it "should handle egyptian numbers" do
  42
+    it "handles egyptian numbers" do
35 43
       Phony.split('20212345678').should == ['20', '2', '12345678']
36 44
       Phony.split('20951234567').should == ['20', '95', '1234567']
37 45
     end
38  
-    it "should handle french numbers" do
  46
+    it "handles french numbers" do
39 47
       Phony.split('33112345678').should == ['33', '1', '12','34','56','78']
40 48
     end
41  
-    it "should handle german numbers" do
  49
+    it "handles german numbers" do
42 50
       Phony.split('4930123456').should ==   ['49', '30', '123', '456']
43 51
       Phony.split('4976112345').should ==   ['49', '761', '123', '45']
44 52
       Phony.split('492041123456').should == ['49', '2041', '123', '456']
@@ -51,7 +59,9 @@
51 59
       Phony.split('3612345678').should == ['36', '1', '234', '5678']
52 60
     end
53 61
     it "should handle italian numbers" do
54  
-      Phony.split('3928061371').should == ['39', '2', '806', '1371']
  62
+      Phony.split('3934869528').should == ['39', '3486', '952', '8']   # Mobile
  63
+      Phony.split('39068546705').should == ['39', '06', '854', '6705']   # Roma
  64
+      Phony.split('390909709511').should == ['39', '090', '970', '9511'] # Barcellona
55 65
     end
56 66
     it 'handles malay numbers' do
57 67
       Phony.split('6082123456').should == ['60', '82', '123456'] # Kuching
@@ -63,9 +73,13 @@
63 73
       Phony.split('51912341234').should == ['51', '9', '1234', '1234'] # mobile
64 74
       Phony.split('51841234123').should == ['51', '84', '1234', '123'] # Cuzco, best effort
65 75
     end
66  
-    it "should handle polish numbers" do
67  
-      Phony.split('48501224455').should == ['48', '50', '1', '224', '455'] # Mobile
68  
-      Phony.split('48121123123').should == ['48', '12', '1', '123', '123']
  76
+    it "handles polish numbers" do
  77
+      Phony.split('48123123123').should == ['48', '123', '123', '123']
  78
+    end
  79
+    it 'handles portuguese numbers' do
  80
+      Phony.split('351211231234').should == ['351', '21', '123', '1234'] # Lisboa
  81
+      Phony.split('351241123123').should == ['351', '241', '123', '123'] # Abrantes
  82
+      Phony.split('351931231234').should == ['351', '93', '123', '1234'] # mobile
69 83
     end
70 84
     it 'handles romanian numbers' do
71 85
       Phony.split('40211231234').should == ['40', '21', '123', '1234'] # Bucureşti
@@ -141,6 +155,9 @@
141 155
       it "should normalize a number with erroneous zero inside" do
142 156
         Phony.normalize('+410443643533').should == '41443643533'
143 157
       end
  158
+      it "should not normalize a number with a correct zero inside" do
  159
+        Phony.normalize('+390909709511').should == '390909709511'
  160
+      end
144 161
     end
145 162
   end
146 163
   
@@ -277,44 +294,44 @@
277 294
     end
278 295
   end
279 296
   
280  
-  context "speed" do
281  
-    before(:each) do
282  
-      @phone_numbers = [
283  
-        '41443643532',
284  
-        '18091231234',
285  
-        '43198110',
286  
-        '33142278186',
287  
-        '4233841148'
288  
-      ]
289  
-    end
290  
-    describe 'split' do
291  
-      it 'is fast' do
292  
-        number = @phone_numbers.first
293  
-        performance_of { Phony.split(number) }.should < 0.00005
294  
-      end
295  
-      it 'is fast' do
296  
-        performance_of { @phone_numbers.each { |number| Phony.split(number) } }.should < 0.00015
297  
-      end
298  
-    end
299  
-    describe 'normalize' do
300  
-      it 'is fast' do
301  
-        number = @phone_numbers.first
302  
-        performance_of { Phony.normalize(number) }.should < 0.0001
303  
-      end
304  
-      it 'is fast' do
305  
-        performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00016
306  
-      end
307  
-    end
308  
-    describe 'formatted' do
309  
-      it 'is fast' do
310  
-        number = @phone_numbers.first
311  
-        performance_of { Phony.formatted(number) }.should < 0.000075
312  
-      end
313  
-      it 'is fast' do
314  
-        performance_of { @phone_numbers.each { |number| Phony.formatted(number) } }.should < 0.00016
315  
-      end
316  
-    end
317  
-  end
  297
+  # context "speed" do
  298
+  #   before(:each) do
  299
+  #     @phone_numbers = [
  300
+  #       '41443643532',
  301
+  #       '18091231234',
  302
+  #       '43198110',
  303
+  #       '33142278186',
  304
+  #       '4233841148'
  305
+  #     ]
  306
+  #   end
  307
+  #   describe 'split' do
  308
+  #     it 'is fast' do
  309
+  #       number = @phone_numbers.first
  310
+  #       performance_of { Phony.split(number) }.should < 0.00005
  311
+  #     end
  312
+  #     it 'is fast' do
  313
+  #       performance_of { @phone_numbers.each { |number| Phony.split(number) } }.should < 0.00015
  314
+  #     end
  315
+  #   end
  316
+  #   describe 'normalize' do
  317
+  #     it 'is fast' do
  318
+  #       number = @phone_numbers.first
  319
+  #       performance_of { Phony.normalize(number) }.should < 0.0001
  320
+  #     end
  321
+  #     it 'is fast' do
  322
+  #       performance_of { @phone_numbers.each { |number| Phony.normalize(number) } }.should < 0.00016
  323
+  #     end
  324
+  #   end
  325
+  #   describe 'formatted' do
  326
+  #     it 'is fast' do
  327
+  #       number = @phone_numbers.first
  328
+  #       performance_of { Phony.formatted(number) }.should < 0.000075
  329
+  #     end
  330
+  #     it 'is fast' do
  331
+  #       performance_of { @phone_numbers.each { |number| Phony.formatted(number) } }.should < 0.00016
  332
+  #     end
  333
+  #   end
  334
+  # end
318 335
   
319 336
   describe 'vanity' do
320 337
     describe 'vanity_number?' do

0 notes on commit c8173d5

Please sign in to comment.
Something went wrong with that request. Please try again.