From 9d21afe3b24cb81b6d189b5d652cd5754f19b56a Mon Sep 17 00:00:00 2001 From: Wes Oldenbeuving Date: Fri, 8 Jan 2010 12:07:15 +0100 Subject: [PATCH] Overhauled the matcher and corrector to be country-code aware --- lib/number_recognizer.rb | 30 +++++++++++------ spec/number_recognizer_spec.rb | 61 ++++++++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/lib/number_recognizer.rb b/lib/number_recognizer.rb index b2d6d8d..607c8ab 100644 --- a/lib/number_recognizer.rb +++ b/lib/number_recognizer.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/number_recognizer_spec.rb b/spec/number_recognizer_spec.rb index 0263f43..e8d9715 100644 --- a/spec/number_recognizer_spec.rb +++ b/spec/number_recognizer_spec.rb @@ -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 @@ -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 @@ -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 \ No newline at end of file