phony 1.2.9 → 1.2.10

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -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, UK, 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
 
@@ -72,7 +72,7 @@ module Phony
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
77
  :local_format => [2, 2, 2],
78
78
  :service_ndcs => %w{112 114}
@@ -0,0 +1,105 @@
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 6.
4
+ #
5
+ # The UK is special in formatting in that it uses a 4-4, or 3-4 formatting rule with area
6
+ # codes that are 2 or 3 digits long, and a 6 formatting rule with area codes that are 4-6 digits long.
7
+ #
8
+ # To reflect this different formatting, we need to install all handlers in a row.
9
+ # First, the area codes of length 2, without a fallback length (since this captures all), but with a nil fallback length.
10
+ #
11
+ handlers = []
12
+
13
+ area_code_2_national = Phony::NationalSplitters::Variable.new nil, :landline => [
14
+ '20', # London
15
+ '23', # Southampton, Portsmith
16
+ '24', # Coventry
17
+ '28', # Northern Ireland
18
+ '29', # Cardiff
19
+ ]
20
+ area_code_2_local = Phony::LocalSplitter.instance_for [4, 4]
21
+ handlers << Phony::NationalCode.new(area_code_2_national, area_code_2_local)
22
+
23
+ area_code_3_national = Phony::NationalSplitters::Variable.new nil, :landline => [
24
+ # Geographical.
25
+ #
26
+ '113', # Leeds
27
+ '114', # Sheffield
28
+ '115', # Nottingham
29
+ '116', # Leicester
30
+ '117', # Bristol
31
+ '118', # Reading
32
+ '121', # Birmingham
33
+ '131', # Edinburgh
34
+ '141', # Glasgow
35
+ '151', # Liverpool
36
+ '161', # Manchester
37
+ '171', # Used for inner London until 2000
38
+ '181', # Used for outer London until 2000
39
+ '191', # Tyne and Wear/County Durham
40
+ # Services.
41
+ #
42
+ '800', # Freephone
43
+ '808', # Freephone
44
+ '842',
45
+ '843',
46
+ '844',
47
+ '845',
48
+ '870',
49
+ '871',
50
+ '872',
51
+ '873',
52
+ '900', # Premium rate content services
53
+ '908', # Sexual entertainment services
54
+ '909', # Sexual entertainment services
55
+ '982', # Sexual entertainment services
56
+ ]
57
+ area_code_3_local = Phony::LocalSplitter.instance_for [3, 4]
58
+ handlers << Phony::NationalCode.new(area_code_3_national, area_code_3_local)
59
+
60
+ # 6 is the fallback length.
61
+ #
62
+ area_code_45_national = Phony::NationalSplitters::Variable.new 6, :landline => [
63
+ # Geographical.
64
+ #
65
+ '1204', # Bolton
66
+ '1224', # Aberdeen
67
+ '1244', # Chester
68
+ '1382', # Dundee
69
+ '1429', # Hartlepool
70
+ '1482', # Hull
71
+ '1527', # Redditch
72
+ '1582', # Luton
73
+ '1670', # Morpeth
74
+ '1730', # Petersfield
75
+ '1736', # Penzance
76
+ '1750', # Selkirk
77
+ '1772', # Preston
78
+ '1853', # Ullapool
79
+ '1900', # Workington
80
+ '1946', # Whitehaven
81
+ '1947', # Whitby
82
+ # Mobile.
83
+ #
84
+ ('7400'..'7599').to_a,
85
+ '7624',
86
+ ('7700'..'7999').to_a,
87
+ # Geographical.
88
+ #
89
+ '13873', # Langholm
90
+ '15242', # Hornby
91
+ '15394', # Hawkshead
92
+ '15395', # Grange-over-Sands
93
+ '15396', # Sedbergh
94
+ '16973', # Wigton
95
+ '16974', # Raughton Head
96
+ '16977', # Brampton[3][4]
97
+ '17683', # Appleby
98
+ '17684', # Pooley Bridge
99
+ '17687', # Keswick
100
+ '19467', # Gosforth
101
+ ]
102
+ area_code_45_local = Phony::LocalSplitter.instance_for [6]
103
+ handlers << Phony::NationalCode.new(area_code_45_national, area_code_45_local)
104
+
105
+ Phony::Countries::UnitedKingdom = Phony::Country.new *handlers
data/lib/phony/country.rb CHANGED
@@ -4,21 +4,19 @@ module Phony
4
4
  #
5
5
  class Country
6
6
 
7
- def initialize national_code, special_code = nil, mobile_code = nil
8
- @national_code = national_code
9
- @special_code = special_code
10
- @mobile_code = mobile_code
7
+ #
8
+ #
9
+ def initialize *codes
10
+ @codes = codes
11
11
  end
12
12
 
13
- #
13
+ # A number is split with the code handlers as given in the initializer.
14
14
  #
15
15
  def split national_number
16
- ndc, *rest = @special_code.split national_number if @special_code
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
21
- [ndc, *rest]
16
+ @codes.each do |code|
17
+ ndc, *rest = code.split national_number
18
+ return [ndc, *rest] if rest && !rest.empty?
19
+ end
22
20
  end
23
21
 
24
22
  # Removes 0s from partially normalized numbers
@@ -30,11 +28,10 @@ module Phony
30
28
  # In some cases it doesn't, like Italy.
31
29
  #
32
30
  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
31
+ @codes.each do |code|
32
+ normalized = code.normalize national_number
33
+ return normalized if normalized && !normalized.empty?
34
+ end
38
35
  end
39
36
 
40
37
  # Is this national number a vanity number?
@@ -74,23 +71,25 @@ module Phony
74
71
  ndc_fallback_length = options[:ndc_fallback_length]
75
72
  ndc_mapping = options[:ndc_mapping] || raise("No ndc_mapping given!")
76
73
 
77
- national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
78
- local_splitter = Phony::LocalSplitter.instance_for options[:local_format] || [3, 2, 2]
79
- national_code = Phony::NationalCode.new national_splitter, local_splitter, normalize
74
+ codes = []
80
75
 
81
76
  if ndc_mapping[:service]
82
77
  service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => ndc_mapping[:service]
83
78
  service_local_splitter = Phony::LocalSplitter.instance_for options[:service_local_format] || [3, 6]
84
- service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
79
+ codes << Phony::NationalCode.new(service_national_splitter, service_local_splitter, normalize)
85
80
  end
86
81
  if ndc_mapping[:mobile]
87
82
  mobile_local_format = options[:mobile_local_format] || options[:local_format] || [9]
88
83
  mobile_national_splitter = Phony::NationalSplitters::Variable.new nil, :mobile => ndc_mapping[:mobile]
89
84
  mobile_local_splitter = Phony::LocalSplitter.instance_for mobile_local_format
90
- mobile_code = Phony::NationalCode.new mobile_national_splitter, mobile_local_splitter, normalize
85
+ codes << Phony::NationalCode.new(mobile_national_splitter, mobile_local_splitter, normalize)
91
86
  end
92
87
 
93
- new national_code, service_code, mobile_code
88
+ national_splitter = Phony::NationalSplitters::Variable.new ndc_fallback_length, ndc_mapping
89
+ local_splitter = Phony::LocalSplitter.instance_for options[:local_format] || [3, 2, 2]
90
+ codes << Phony::NationalCode.new(national_splitter, local_splitter, normalize)
91
+
92
+ new *codes
94
93
  end
95
94
 
96
95
  # Gets a configured country instance with fixed length ndc code.
@@ -110,19 +109,20 @@ module Phony
110
109
  service_ndcs = options[:service_ndcs]
111
110
  local_format = options[:local_format]
112
111
 
113
- national_splitter = Phony::NationalSplitters::Fixed.new ndc_length
114
- local_splitter = Phony::LocalSplitter.instance_for local_format || [3, 2, 2]
115
- national_code = Phony::NationalCode.new national_splitter, local_splitter, normalize
112
+ codes = []
116
113
 
117
- service_code = nil
118
114
  if service_ndcs
119
115
  service_local_format = options[:service_local_format] || local_format || [3, 3]
120
116
  service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => service_ndcs
121
117
  service_local_splitter = Phony::LocalSplitter.instance_for service_local_format
122
- service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
118
+ codes << Phony::NationalCode.new(service_national_splitter, service_local_splitter, normalize)
123
119
  end
124
120
 
125
- new national_code, service_code
121
+ national_splitter = Phony::NationalSplitters::Fixed.new ndc_length
122
+ local_splitter = Phony::LocalSplitter.instance_for local_format || [3, 2, 2]
123
+ codes << Phony::NationalCode.new(national_splitter, local_splitter, normalize)
124
+
125
+ new *codes
126
126
  end
127
127
 
128
128
  end
data/lib/phony.rb CHANGED
@@ -28,6 +28,7 @@ require File.expand_path '../phony/countries/portugal', __FILE__
28
28
  require File.expand_path '../phony/countries/romania', __FILE__
29
29
  require File.expand_path '../phony/countries/south_korea', __FILE__
30
30
  require File.expand_path '../phony/countries/sweden', __FILE__
31
+ require File.expand_path '../phony/countries/united_kingdom', __FILE__
31
32
 
32
33
  require File.expand_path '../phony/country_codes', __FILE__
33
34
 
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  #
3
+
3
4
  require 'spec_helper'
4
5
 
5
6
  describe Phony::Countries::Belgium do
@@ -0,0 +1,50 @@
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 Mobile Numbers" do
34
+ @uk.split('7480605207').should == ['7480', '605207']
35
+ end
36
+ it "should handle service numbers with 800 [regression]" do
37
+ @uk.split('8005878323').should == ['800', '587', '8323']
38
+ end
39
+ #
40
+ # 44 116 xxx xxxx Leicester
41
+ # 44 131 xxx xxxx Edinburgh
42
+ # 44 151 xxx xxxx Liverpool
43
+ # 44 1382 xxxxxx Dundee
44
+ # 44 1386 xxxxxx Evesham
45
+ # 44 1865 xxxxxx Oxford
46
+ # 44 153 96 xxxxx Sedbergh
47
+ # 44 169 77 xxxx Brampton
48
+ end
49
+
50
+ end
@@ -73,15 +73,15 @@ describe Phony::Country do
73
73
 
74
74
  context "without special cases" do
75
75
  before(:each) do
76
- national_splitter = Phony::NationalSplitters::Variable.new 4, { :normal => ['44'] }
77
- local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
78
- national_code = Phony::NationalCode.new national_splitter, local_splitter
79
-
80
76
  special_national_splitter = Phony::NationalSplitters::Variable.new nil, { :service => ['800'] }
81
77
  special_local_splitter = Phony::LocalSplitter.instance_for [3, 3]
82
78
  special_code = Phony::NationalCode.new special_national_splitter, special_local_splitter
83
79
 
84
- @switzerland = Phony::Country.new national_code, special_code
80
+ national_splitter = Phony::NationalSplitters::Variable.new 4, { :normal => ['44'] }
81
+ local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
82
+ national_code = Phony::NationalCode.new national_splitter, local_splitter
83
+
84
+ @switzerland = Phony::Country.new special_code, national_code
85
85
  end
86
86
 
87
87
  describe "split" do
@@ -73,7 +73,7 @@ describe Phony do
73
73
  Phony.split('51912341234').should == ['51', '9', '1234', '1234'] # mobile
74
74
  Phony.split('51841234123').should == ['51', '84', '1234', '123'] # Cuzco, best effort
75
75
  end
76
- it "should handle polish numbers" do
76
+ it "handles polish numbers" do
77
77
  Phony.split('48123123123').should == ['48', '123', '123', '123']
78
78
  end
79
79
  it 'handles portuguese numbers' do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: phony
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.2.9
5
+ version: 1.2.10
6
6
  platform: ruby
7
7
  authors:
8
8
  - Florian Hanke
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-03 00:00:00 +01:00
13
+ date: 2011-02-13 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -40,6 +40,7 @@ files:
40
40
  - lib/phony/countries/romania.rb
41
41
  - lib/phony/countries/south_korea.rb
42
42
  - lib/phony/countries/sweden.rb
43
+ - lib/phony/countries/united_kingdom.rb
43
44
  - lib/phony/country.rb
44
45
  - lib/phony/country_codes.rb
45
46
  - lib/phony/local_splitter.rb
@@ -57,6 +58,7 @@ files:
57
58
  - spec/lib/phony/countries/hungary_spec.rb
58
59
  - spec/lib/phony/countries/portugal_spec.rb
59
60
  - spec/lib/phony/countries/switzerland_spec.rb
61
+ - spec/lib/phony/countries/united_kingdom_spec.rb
60
62
  - spec/lib/phony/country_codes_spec.rb
61
63
  - spec/lib/phony/country_spec.rb
62
64
  - spec/lib/phony/local_splitter_spec.rb
@@ -102,6 +104,7 @@ test_files:
102
104
  - spec/lib/phony/countries/hungary_spec.rb
103
105
  - spec/lib/phony/countries/portugal_spec.rb
104
106
  - spec/lib/phony/countries/switzerland_spec.rb
107
+ - spec/lib/phony/countries/united_kingdom_spec.rb
105
108
  - spec/lib/phony/country_codes_spec.rb
106
109
  - spec/lib/phony/country_spec.rb
107
110
  - spec/lib/phony/local_splitter_spec.rb