Skip to content

Commit

Permalink
Added support for Spanish mobile numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Narnach committed May 9, 2011
1 parent 13ad220 commit 50b0ec8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/number_recognizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class NumberRecognizer
add_format :country => "England", :mobile=>true, :format => /(44)(7\d{8,9})/
add_format :country => "Australia", :mobile=>true, :format => /(61)(4\d{8})/
add_format :country => "Portugal", :mobile=>true, :format => /(351)(9\d{8})/
add_format :country => "Spain", :mobile=>true, :format => /(34)([67]\d{8})/

add_format :country => "Netherlands", :mobile=>false, :format => /(31)([123457890]\d{8})/
add_format :country => "Suriname", :mobile=>false, :format => /(597)(\d{7,7})/
Expand Down Expand Up @@ -54,9 +55,11 @@ def correct(country_bias=nil)
when /^0?9([136]\d{7})$/ #this must come before NL !
self.number = "3519#{$1}"
when /^0?7(\d{8,9})$/
self.number = "447#{$1}"
prefix = pick_biased_country([44,34], country_bias)
self.number = "#{prefix}7#{$1}"
when /^0?6+(\d{8})$/
self.number = "316#{$1}"
prefix = pick_biased_country([31,34], country_bias)
self.number = "#{prefix}6#{$1}"
when /^0?4(\d{8})$/
prefix = pick_biased_country([32,61], country_bias)
self.number = "#{prefix}4#{$1}"
Expand Down
27 changes: 27 additions & 0 deletions spec/number_recognizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@
@nc.local_number.should == '927123456'
@nc.country_name.should == 'Portugal'
end

it 'should recognize 0034612345678' do
@nc = NumberRecognizer.new('0034612345678')
@nc.should be_valid
@nc.country.should == '34'
@nc.should be_mobile
@nc.local_number.should == '612345678'
@nc.country_name.should == 'Spain'
end
end

describe "correct" do
Expand Down Expand Up @@ -153,6 +162,24 @@
@nc.number.should == '61412345678'
@nc.old_number.should == '0412345678'
end

it 'should correct 0612345678 to 34612345678 given a country-bias for 34' do
@nc = NumberRecognizer.new('0612345678')
@nc.should_not be_valid

@nc.correct(34).should be_true
@nc.number.should == '34612345678'
@nc.old_number.should == '0612345678'
end

it 'should correct 0712345678 to 34712345678 given a country-bias for 34' do
@nc = NumberRecognizer.new('0712345678')
@nc.should_not be_valid

@nc.correct(34).should be_true
@nc.number.should == '34712345678'
@nc.old_number.should == '0712345678'
end
end

describe 'valid or correct mobile' do
Expand Down

0 comments on commit 50b0ec8

Please sign in to comment.