Skip to content

Commit

Permalink
Merge pull request #27 from miloshadzic/exceptions
Browse files Browse the repository at this point in the history
Add specific errors.
  • Loading branch information
carr committed Aug 16, 2012
2 parents 231f248 + 7b5c395 commit 388c50f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
6 changes: 6 additions & 0 deletions lib/errors.rb
@@ -0,0 +1,6 @@
module Phoner
class PhoneError < RuntimeError;end
class BlankNumberError < PhoneError;end
class AreaCodeError < PhoneError;end
class CountryCodeError < PhoneError;end
end
14 changes: 8 additions & 6 deletions lib/phone.rb
Expand Up @@ -11,6 +11,7 @@
#
require File.join(File.dirname(__FILE__), 'support') unless defined? ActiveSupport
require File.join(File.dirname(__FILE__), 'country')
require File.join(File.dirname(__FILE__), 'errors')

module Phoner
class Phone
Expand Down Expand Up @@ -48,9 +49,9 @@ def initialize(*hash_or_args)
self.country_code = hash_or_args[ keys[:country_code] ] || self.default_country_code
self.extension = hash_or_args[ keys[:extension] ]

raise "Must enter number" if self.number.blank?
raise "Must enter area code or set default area code" if self.area_code.blank?
raise "Must enter country code or set default country code" if self.country_code.blank?
raise NumberError, "Must enter number" if self.number.blank?
raise AreaCodeError, "Must enter area code or set default area code" if self.area_code.blank?
raise CountryCodeError, "Must enter country code or set default country code" if self.country_code.blank?
end

# create a new phone number by parsing a string
Expand Down Expand Up @@ -78,7 +79,8 @@ def self.parse(string, options={})
def self.valid?(string)
begin
parse(string).present?
rescue RuntimeError # if we encountered exceptions (missing country code, missing area code etc)
# if we encountered exceptions (missing country code, missing area code etc)
rescue PhoneError
return false
end
end
Expand All @@ -98,9 +100,9 @@ def self.split_to_parts(string, options = {})

if country.nil?
if options[:country_code].nil?
raise "Must enter country code or set default country code"
raise CountryCodeError, "Must enter country code or set default country code"
else
raise "Could not find country with country code #{options[:country_code]}"
raise CountryCodeError, "Could not find country with country code #{options[:country_code]}"
end
end

Expand Down
18 changes: 9 additions & 9 deletions test/phone_test.rb
Expand Up @@ -4,17 +4,17 @@ class PhoneTest < Test::Unit::TestCase

def test_number_without_country_code_initialize
Phoner::Phone.default_country_code = nil
assert_raise RuntimeError do

assert_raise Phoner::CountryCodeError do
pn = Phoner::Phone.new '5125486', '91'
end
end

def test_number_without_country_and_area_code_initialize
Phoner::Phone.default_country_code = nil
Phoner::Phone.default_area_code = nil
assert_raise RuntimeError do
Phoner::Phone.default_area_code = nil

assert_raise Phoner::AreaCodeError do
pn = Phoner::Phone.new '451588'
end
end
Expand Down Expand Up @@ -54,16 +54,16 @@ def test_parse_empty

def test_parse_short_without_special_characters_without_country
Phoner::Phone.default_country_code = nil
assert_raise RuntimeError do

assert_raise Phoner::CountryCodeError do
pn = Phoner::Phone.parse "0915125486"
end
end

def test_parse_short_with_special_characters_without_country
Phoner::Phone.default_country_code = nil
assert_raise RuntimeError do

assert_raise Phoner::CountryCodeError do
pn = Phoner::Phone.parse "091/512-5486"
end
end
Expand Down

0 comments on commit 388c50f

Please sign in to comment.