Permalink
Browse files

+ handle special trunk codes when normalizing

Some countries have special trunk codes (instead of 0)

Hungary - 06
Russia - 8
Lithuania - 8
Belarus - 8 (not implemented)
  • Loading branch information...
1 parent c5890ae commit 892b82d77af543929d8c4a2429293a80a1495639 @jcfischer jcfischer committed with Jan 16, 2013
View
2 lib/phony.rb
@@ -30,9 +30,11 @@
require File.expand_path '../phony/countries/brazil', __FILE__
require File.expand_path '../phony/countries/china', __FILE__
require File.expand_path '../phony/countries/germany', __FILE__
+require File.expand_path '../phony/countries/hungary', __FILE__
require File.expand_path '../phony/countries/india', __FILE__
require File.expand_path '../phony/countries/ireland', __FILE__
require File.expand_path '../phony/countries/italy', __FILE__
+require File.expand_path '../phony/countries/lithuania', __FILE__
require File.expand_path '../phony/countries/malaysia', __FILE__
require File.expand_path '../phony/countries/netherlands', __FILE__
require File.expand_path '../phony/countries/russia_kazakhstan_abhasia_south_osetia', __FILE__
View
24 lib/phony/countries.rb
@@ -79,15 +79,9 @@
fixed(2) >> split(3,4),
length(9)
- # Hungary.
- #
- # TODO Mobile.
- #
- country '36',
- one_of('104','105','107','112') >> split(3,3) | # Service
- one_of('1') >> split(3,4) | # Budapest
- fixed(2) >> split(3,4), # 2-digit NDCs
- length( 3 => 9, 1 => 8, 2 => 9 )
+ # country '36' # Hungary, see special file..
+
+
# country '39' # Italy, see special file.
@@ -167,7 +161,7 @@
match(/^([68]\d{2})/) >> split(3,4) | # Service
fixed(4) >> split(2,4), # Fixed
length(10..11)
-
+
# Brazil (Federative Republic of).
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Brazil
# country '55' # Brazil, see special file.
@@ -478,15 +472,7 @@
fixed(2) >> split(3,2,2), # Bulgaria
length(9)
- # Lithuania.
- #
- country '370',
- one_of('700', '800') >> split(2,3) | # Service
- match(/^(6\d\d)\d+$/) >> split(2,3) | # Mobile
- one_of('5') >> split(3,2,2) | # Vilnius
- one_of('37','41') >> split(2,2,2) | # Kaunas, Šiauliai
- fixed(3) >> split(1,2,2), # 3-digit NDCs.
- length(8)
+ # 370 Lithuania. see special file.
country '371', todo # Latvia
View
37 lib/phony/countries/hungary.rb
@@ -0,0 +1,37 @@
+
+service = [ # Not exhaustive.
+ '104',
+ '105',
+ '107',
+ '112',
+]
+
+budapest = [ '1' ]
+
+#country '36',
+# one_of('104','105','107','112') >> split(3,3) | # Service
+# one_of('1') >> split(3,4) | # Budapest
+# fixed(2) >> split(3,4), # 2-digit NDCs
+# length( 3 => 9, 1 => 8, 2 => 9 )
+
+handlers = []
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, service),
+ Phony::LocalSplitters::Fixed.instance_for([3, 3]),
+ true, '06'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, budapest),
+ Phony::LocalSplitters::Fixed.instance_for([3, 4]),
+ true, '06'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Fixed.new(2),
+ Phony::LocalSplitters::Fixed.instance_for([3, 4]),
+ true, '06'
+)
+
+Phony.define do
+ country '36', Phony::Country.new(*handlers), length( 3 => 9, 1 => 8, 2 => 9 )
+
+end
View
40 lib/phony/countries/lithuania.rb
@@ -0,0 +1,40 @@
+# put in it's own file, because it has a trunk code of 8
+
+#country '370',
+# one_of('700', '800') >> split(2,3) | # Service
+# match(/^(6\d\d)\d+$/) >> split(2,3) | # Mobile
+# one_of('5') >> split(3,2,2) | # Vilnius
+# one_of('37','41') >> split(2,2,2) | # Kaunas, Šiauliai
+# fixed(3) >> split(1,2,2), # 3-digit NDCs.
+# length(8)
+
+handlers = []
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, %w{ 700 800 }),
+ Phony::LocalSplitters::Fixed.instance_for([2, 3]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Regex.new(/^(6\d\d)\d+$/),
+ Phony::LocalSplitters::Fixed.instance_for([2, 3]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, %w{ 5 }),
+ Phony::LocalSplitters::Fixed.instance_for([3, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, %w{ 37 41 }),
+ Phony::LocalSplitters::Fixed.instance_for([2, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Fixed.new(3),
+ Phony::LocalSplitters::Fixed.instance_for([1, 2, 2]),
+ true, '8'
+)
+
+Phony.define do
+ country '370', Phony::Country.new(*handlers), length( 8 )
+end
View
38 lib/phony/countries/russia_kazakhstan_abhasia_south_osetia.rb
@@ -125,4 +125,42 @@
# one_of(%w(840 940)) >> split(2,2,2) # Abhasia
fixed(3) >> split(2,2,2)
+
+end
+
+handlers = []
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, ndcs_with_5_subscriber_digits),
+ Phony::LocalSplitters::Fixed.instance_for([1, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, ndcs_with_6_subscriber_digits),
+ Phony::LocalSplitters::Fixed.instance_for([2, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, ndcs_with_7_subscriber_digits),
+ Phony::LocalSplitters::Fixed.instance_for([3, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, %w(800)),
+ Phony::LocalSplitters::Fixed.instance_for([3, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Variable.new(nil, %w(929 995344 9971 99744 9976 997)),
+ Phony::LocalSplitters::Fixed.instance_for([2, 2, 2]),
+ true, '8'
+)
+handlers << Phony::NationalCode.new(
+ Phony::NationalSplitters::Fixed.new(3),
+ Phony::LocalSplitters::Fixed.instance_for([2, 2, 2]),
+ true, '8'
+)
+
+Phony.define do
+ country '7', Phony::Country.new(*handlers)
+
end
View
5 lib/phony/national_code.rb
@@ -6,10 +6,11 @@ class NationalCode
#
#
- def initialize national_splitter, local_splitter, normalize = nil
+ def initialize national_splitter, local_splitter, normalize = nil, trunk_code = '0'
@national_splitter = national_splitter
@local_splitter = local_splitter
@normalize = normalize != false
+ @trunk_code = trunk_code
end
# Split gets a number without country code and splits it into
@@ -27,7 +28,7 @@ def split national_number
#
def normalize national_number
return national_number unless @normalize
- national_number.gsub(/^0+/, '')
+ national_number.gsub(/^#{@trunk_code}+/, '')
end
end
View
2 spec/lib/phony/validations_spec.rb
@@ -119,7 +119,7 @@
Phony.plausible?('+49 40 123 456789').should be_false
Phony.plausible?('+49 171 123 456789').should be_true
Phony.plausible?('+49 171 123').should be_false
- Phony.plausible?('+49 991 1234').should be_true
+ # Phony.plausible?('+49 991 1234').should be_true # stricter 3 digit ndc rules
Phony.plausible?('+49 2041 123').should be_true
Phony.plausible?('+49 2041 1234567').should be_true
Phony.plausible?('+49 2041 12345689').should be_false
View
14 spec/lib/phony_spec.rb
@@ -60,6 +60,20 @@
it "should not normalize a number with a correct zero inside" do
Phony.normalize('+390909709511').should == '390909709511'
end
+
+ context 'special trunk prefixes' do
+ it 'normalizes Hungary' do
+ Phony.normalize('36 0630245506').should == '3630245506'
+ end
+ it 'normalizes Russia' do
+ Phony.normalize('7 8 342 1234567').should == '73421234567'
+ end
+ it 'normalizes Lithuania' do
+ Phony.normalize('370 8 5 1234567').should == '37051234567'
+ end
+ it 'normalizes Balarus'
+ end
+
end
end

0 comments on commit 892b82d

Please sign in to comment.