Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
Recognize australian and mobile numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Narnach committed Mar 30, 2010
1 parent 30ce2cb commit 1c5d16a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/number_recognizer.rb
Expand Up @@ -9,16 +9,23 @@ class NumberRecognizer
'Belgian mobile' => /(32)(4\d{8,8})/,
'Suriname' => /(597)(\d{7,7})/,
'Dutch Antilles' => /(599)(\d{7,7})/,
'England' => /(44)(\d{9,10})/
'England' => /(44)(\d{9,10})/,
'Australia mobile' => /(61)(4\d{8})/,
'Austraia' => /(61)([1-35-9]\d{8})/,
}

def initialize(number)
@number = number
end

def mobile?
self.valid? if self.type.nil?
self.type.to_s =~ /mobile/
end

def valid_or_correct_mobile?
return false unless valid? or correct
['Dutch mobile', 'Belgian mobile'].include? type
mobile?
end

# Set type, country and local_number
Expand Down
21 changes: 19 additions & 2 deletions spec/number_recognizer_spec.rb
Expand Up @@ -72,6 +72,14 @@
@nc.local_number.should == '123456789'
@nc.type.should == 'England'
end

it 'should recognize 0061451124205 as an Australian number' do
@nc = NumberRecognizer.new('0061451124205')
@nc.should be_valid
@nc.country.should == '61'
@nc.local_number.should == '451124205'
@nc.type.should == 'Australia mobile'
end
end

describe "correction" do
Expand Down Expand Up @@ -127,8 +135,6 @@
@nc.local_number.should == '412345678'
end



describe 'valid or correct mobile' do
it 'should correct 0612345678 to 0031612345678' do
@nc = NumberRecognizer.new('0612345678')
Expand All @@ -149,4 +155,15 @@
end
end
end

describe 'mobile?' do
it 'should recognize 31612345678 as a mobile number' do
@nc = NumberRecognizer.new('0031612345678')
@nc.should be_mobile
end
it 'should recognize 31202345678 as not mobile number' do
@nc = NumberRecognizer.new('31202345678')
@nc.should_not be_mobile
end
end
end

0 comments on commit 1c5d16a

Please sign in to comment.