From d3d3b1df2b80cfabe71f10eeb96872102d33c6f5 Mon Sep 17 00:00:00 2001 From: Wes Oldenbeuving Date: Tue, 22 Feb 2011 09:43:33 +0100 Subject: [PATCH] Added NumberRecognizer#type back as a composite of country name and mobile/landline status --- lib/number_recognizer.rb | 7 ++++++- spec/number_recognizer_spec.rb | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/number_recognizer.rb b/lib/number_recognizer.rb index 9f27435..99c2d8a 100644 --- a/lib/number_recognizer.rb +++ b/lib/number_recognizer.rb @@ -71,12 +71,17 @@ def correct(country_bias=nil) valid? end + def type + return nil unless valid? + "#{country_name} #{mobile? ? "mobile" : "landline"}" + end + private def parse @parsed = true @valid = false - number = self.number.to_s + number = self.number.to_s.sub(/^0+/,'') format = self.class.formats.find {|format| number.match(format[:format])} unless format return diff --git a/spec/number_recognizer_spec.rb b/spec/number_recognizer_spec.rb index 1b34d70..a87e6b4 100644 --- a/spec/number_recognizer_spec.rb +++ b/spec/number_recognizer_spec.rb @@ -211,6 +211,10 @@ @nc.local_number.should == '913773785' end + it "should return false for a landline number" do + @nc = NumberRecognizer.new('31201234567') + @nc.should_not be_valid_or_correct_mobile + end end describe 'mobile?' do @@ -219,9 +223,18 @@ @nc.should be_mobile end - it 'should recognize 31202345678 as not mobile number' do - @nc = NumberRecognizer.new('31202345678') + it 'should recognize 31201234567 as not mobile number' do + @nc = NumberRecognizer.new('31201234567') @nc.should_not be_mobile end end + + describe "type" do + it "should compose country and mobile/landline status" do + @nc = NumberRecognizer.new('31612345678') + @nc.type.should == "Netherlands mobile" + @nc = NumberRecognizer.new('31201234567') + @nc.type.should == "Netherlands landline" + end + end end