Skip to content

Commit

Permalink
Overhauled the matcher and corrector to be country-code aware
Browse files Browse the repository at this point in the history
  • Loading branch information
Narnach committed Jan 8, 2010
1 parent 69c1d20 commit 9d21afe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
30 changes: 20 additions & 10 deletions lib/number_recognizer.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
class NumberRecognizer
attr_accessor :type, :number, :old_number
attr_accessor :number, :old_number, :type
attr_accessor :country, :local_number

# Name -> pattern
KNOWN_FORMATS = {
'Dutch mobile' => /(316|6)\d{8,8}/,
'Dutch landline' => /31[12345789]\d{8,8}/,
'Belgian mobile' => /324\d{8,8}/,
'Suriname' => /597\d{7,7}/,
'Dutch Antilles' => /599\d{7,7}/,
'England' => /44\d{9,10}/
'Dutch mobile' => /(31)(6\d{8,8})/,
'Dutch landline' => /(31)([12345789]\d{8,8})/,
'Belgian mobile' => /(32)(4\d{8,8})/,
'Suriname' => /(597)(\d{7,7})/,
'Dutch Antilles' => /(599)(\d{7,7})/,
'England' => /(44)(\d{9,10})/
}

def initialize(number)
@number = number
end

# Set type, country and local_number
def valid?
self.type = nil
return false unless match = KNOWN_FORMATS.find {|name, pattern| number =~ /^0{0,2}#{pattern}$/}
self.type = match.first
number = self.number.to_s
self.type, pattern = KNOWN_FORMATS.find {|name, pattern| number.match /^0{0,2}#{pattern}$/}
return false unless pattern
match = number.match(pattern)
self.country = match[1]
self.local_number = match[2]
true
end

Expand All @@ -31,6 +36,11 @@ def correct
return false
end
self.old_number = old_number
valid?
true
end

def normalized_number
"#{country}#{local_number}"
end
end
61 changes: 47 additions & 14 deletions spec/number_recognizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,79 @@
require 'number_recognizer'

describe NumberRecognizer do
it 'should normalize numbers' do
@nc = NumberRecognizer.new('0031612345678')
@nc.valid?
@nc.country.should == '31'
@nc.local_number.should == '612345678'
@nc.normalized_number.should == '31612345678'
end

describe 'recognition' do
it 'should recognize 0031612345678 as dutch mobile' do
@nc = NumberRecognizer.new('0031612345678')
@nc.should be_valid
@nc.type.should == 'Dutch mobile'
it 'should be false when a nil number is provided' do
@nc = NumberRecognizer.new(nil)
@nc.should_not be_valid
@nc.type.should be_nil
end

it 'should recognize 0612345678 as dutch mobile' do
@nc = NumberRecognizer.new('0612345678')
it 'should recognize 0031612345678 as dutch mobile' do
@nc = NumberRecognizer.new('0031612345678')
@nc.should be_valid
@nc.country.should == '31'
@nc.local_number.should == '612345678'
@nc.type.should == 'Dutch mobile'
end

it 'should recognize 0031201234567 as dutch landline' do
@nc = NumberRecognizer.new('0031201234567')
@nc.should be_valid
@nc.country.should == '31'
@nc.local_number.should == '201234567'
@nc.type.should == 'Dutch landline'
end

it 'should recognize 0032412345678 as belgian mobile' do
@nc = NumberRecognizer.new('0032412345678')
@nc.should be_valid
@nc.country.should == '32'
@nc.local_number.should == '412345678'
@nc.type.should == 'Belgian mobile'
end

it 'should recognize 005971234567 as a Suriname number' do
@nc = NumberRecognizer.new('005971234567')
@nc.should be_valid
@nc.country.should == '597'
@nc.local_number.should == '1234567'
@nc.type.should == 'Suriname'
end

it 'should recognize 005991234567 as a Dutch Antilles number' do
@nc = NumberRecognizer.new('005991234567')
@nc.should be_valid
@nc.country.should == '599'
@nc.local_number.should == '1234567'
@nc.type.should == 'Dutch Antilles'
end

it 'should be false when a nil number is provided' do
@nc = NumberRecognizer.new(nil)
@nc.should_not be_valid
@nc.type.should be_nil
end

it 'should recognize 00441234567890 as an English number' do
@nc = NumberRecognizer.new('00441234567890')
@nc.should be_valid
@nc.country.should == '44'
@nc.local_number.should == '1234567890'
@nc.type.should == 'England'
end

it 'should recognize 0044123456789 as an English number' do
@nc = NumberRecognizer.new('0044123456789')
@nc.should be_valid
@nc.country.should == '44'
@nc.local_number.should == '123456789'
@nc.type.should == 'England'
end
end

describe "correction" do
it 'should correct 06612345678 to 0031612345678' do
@nc = NumberRecognizer.new('06612345678')
@nc.should_not be_valid
Expand All @@ -65,8 +83,9 @@
@nc.number.should == '0031612345678'
@nc.old_number.should == '06612345678'

@nc.should be_valid
@nc.type.should == 'Dutch mobile'
@nc.country.should == '31'
@nc.local_number.should == '612345678'
end

it 'should correct 09612345678 to 0031612345678' do
Expand All @@ -77,8 +96,22 @@
@nc.number.should == '0031612345678'
@nc.old_number.should == '09612345678'

@nc.should be_valid
@nc.type.should == 'Dutch mobile'
@nc.country.should == '31'
@nc.local_number.should == '612345678'
end

it 'should correct 0612345678 to 0031612345678' do
@nc = NumberRecognizer.new('0612345678')
@nc.should_not be_valid

@nc.correct.should be_true
@nc.number.should == '0031612345678'
@nc.old_number.should == '0612345678'

@nc.type.should == 'Dutch mobile'
@nc.country.should == '31'
@nc.local_number.should == '612345678'
end
end
end

0 comments on commit 9d21afe

Please sign in to comment.