Skip to content

Commit

Permalink
Match short English numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Narnach committed Dec 18, 2009
1 parent 5019c74 commit 69d4691
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/number_recognizer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class NumberRecognizer
attr_accessor :type

# Name -> [country code prefix match, digits after prefix]
# Name -> pattern
KNOWN_FORMATS = {
'Dutch mobile' => [/(316|6)/, 8],
'Dutch landline' => [/31[12345789]/, 8],
'Belgian mobile' => [324, 8],
'Suriname' => [597,7],
'Dutch Antilles' => [599,7],
'England' => [44,10]
'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}/

}
attr_accessor :number
Expand All @@ -19,7 +19,7 @@ def initialize(number)

def valid?
self.type = nil
return false unless match = KNOWN_FORMATS.find {|name, pattern| number =~ /^0{0,2}#{pattern.first}\d{#{pattern.last},#{pattern.last}}$/}
return false unless match = KNOWN_FORMATS.find {|name, pattern| number =~ /^0{0,2}#{pattern}$/}
self.type = match.first
true
end
Expand Down
6 changes: 6 additions & 0 deletions spec/number_recognizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@
@nc.should be_valid
@nc.type.should == 'England'
end

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

0 comments on commit 69d4691

Please sign in to comment.