diff --git a/lib/number_recognizer.rb b/lib/number_recognizer.rb index d1c7442..25fd517 100644 --- a/lib/number_recognizer.rb +++ b/lib/number_recognizer.rb @@ -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})/ @@ -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}" diff --git a/spec/number_recognizer_spec.rb b/spec/number_recognizer_spec.rb index f97e274..fbc7eb6 100644 --- a/spec/number_recognizer_spec.rb +++ b/spec/number_recognizer_spec.rb @@ -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 @@ -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