diff --git a/Readme.rdoc b/Readme.rdoc index 5de7a1e..0bba405 100644 --- a/Readme.rdoc +++ b/Readme.rdoc @@ -9,34 +9,40 @@ You can install the phone library as a gem Or as a Rails plugin script/plugin install git://github.com/carr/phone.git +== Updates in v 1.0 + + The biggest updating is the namespacing problem fixed that a lot of people were having. You now use phone by refering to + + Phoner::Phone + == Initializing You can initialize a new phone object with the number, area code, country code and extension number - Phone.new('5125486', '91', '385') + Phoner::Phone.new('5125486', '91', '385') or - Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143') + Phoner::Phone.new(:number => '5125486', :area_code => '91', :country_code => '385', :extension => '143') == Parsing -You can create a new phone object by parsing from a string. Phone does it's best to detect the country and area codes: - Phone.parse '+385915125486' - Phone.parse '00385915125486' +You can create a new phone object by parsing from a string. Phoner::Phone does it's best to detect the country and area codes: + Phoner::Phone.parse '+385915125486' + Phoner::Phone.parse '00385915125486' If the country or area code isn't given in the string, you must set it, otherwise it doesn't work: - Phone.parse '091/512-5486', :country_code => '385' - Phone.parse '(091) 512 5486', :country_code => '385' + Phoner::Phone.parse '091/512-5486', :country_code => '385' + Phoner::Phone.parse '(091) 512 5486', :country_code => '385' If you feel that it's tedious, set the default country code once (in your config/environment.rb): - Phone.default_country_code = '385' - Phone.parse '091/512-5486' - Phone.parse '(091) 512 5486' + Phoner::Phone.default_country_code = '385' + Phoner::Phone.parse '091/512-5486' + Phoner::Phone.parse '(091) 512 5486' Same goes for the area code: - Phone.parse '451-588', :country_code => '385', :area_code => '47' + Phoner::Phone.parse '451-588', :country_code => '385', :area_code => '47' or - Phone.default_country_code = '385' - Phone.default_area_code = '47' + Phoner::Phone.default_country_code = '385' + Phoner::Phone.default_area_code = '47' - Phone.parse '451-588' + Phoner::Phone.parse '451-588' === Automatic country and area code detection Like it's stated above, Phone does it's best to automatically detect the country and area code while parsing. Do do this, @@ -45,12 +51,12 @@ phone uses data stored in data/countries.yml. Each country code can have a regular expression named area_code that describes how the area code for that particular country looks like. -If an area_code regular expression isn't specified, the default, Phone::DEFAULT_AREA_CODE (correct for +If an area_code regular expression isn't specified, the default, Phoner::Phone::DEFAULT_AREA_CODE (correct for the US) is used. == Validating Validating is very relaxed, basically it strips out everything that's not a number or '+' character: - Phone.valid? 'blabla 091/512-5486 blabla' + Phoner::Phone.valid? 'blabla 091/512-5486 blabla' == Formatting Formating is done via the format method. The method accepts a Symbol or a String. @@ -61,22 +67,22 @@ When given a string, it interpolates the string with the following fields: * %a - area_code (91) * %A - area_code with leading zero (091) * %n - number (5125486) -* %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512) +* %f - first @@n1_length characters of number (configured through Phoner::Phone.n1_length), default is 3 (512) * %l - last characters of number (5486) * %x - the extension number - pn = Phone.parse('+385915125486') + pn = Phoner::Phone.parse('+385915125486') pn.to_s # => "+385915125486" pn.format("%A/%f-%l") # => "091/512-5486" pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486" -When given a symbol it is used as a lookup for the format in the Phone.named_formats hash. +When given a symbol it is used as a lookup for the format in the Phoner::Phone.named_formats hash. pn.format(:europe) # => "+385 (0) 91 512 5486" pn.format(:us) # => "(234) 123 4567" pn.format(:default_with_extension) # => "+3851234567x143" You can add your own custom named formats like so: - Phone.named_formats[:short] = '%A/%n1-%n2' + Phoner::Phone.named_formats[:short] = '%A/%n1-%n2' pn.format(:short) # => 091/512-5486 = TODO @@ -99,8 +105,11 @@ Currently tested on: [US] United States [ZA] South Africa += Known issues +There's an issue with Germany and area codes. + = Author -Copyright © 2010 Tomislav Car, Infinum +Copyright © 2010 Tomislav Car, {Infinum}[http://www.infinumdigital.com] = Contributors Don Morrison, Michael Squires, Todd Eichel (Fooala, Inc.), chipiga, Etienne Samson, Luke Randall diff --git a/lib/country.rb b/lib/country.rb new file mode 100644 index 0000000..64f63e2 --- /dev/null +++ b/lib/country.rb @@ -0,0 +1,30 @@ +module Phoner + class Country < Struct.new(:name, :country_code, :char_2_code, :area_code) + cattr_accessor :all + + def self.load + return @@all if @@all.present? + + data_file = File.join(File.dirname(__FILE__), '..', 'data', 'phone_countries.yml') + + @@all = {} + YAML.load(File.read(data_file)).each_pair do |key, c| + @@all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:area_code]) + end + @@all + end + + def to_s + name + end + + def self.find_by_country_code(code) + @@all[code] + end + + def country_code_regexp + Regexp.new("^[+]#{country_code}") + end + end + +end \ No newline at end of file diff --git a/lib/phone.rb b/lib/phone.rb index 2adf800..e22c439 100644 --- a/lib/phone.rb +++ b/lib/phone.rb @@ -10,240 +10,243 @@ # Phone.default_area_code # require File.join(File.dirname(__FILE__), 'support') unless defined? ActiveSupport -require File.join(File.dirname(__FILE__), 'phone_country') -class Phone - NUMBER = '([0-9]{1,8})$' - DEFAULT_AREA_CODE = '[2-9][0-8][0-9]' # USA - - attr_accessor :country_code, :area_code, :number, :extension - - cattr_accessor :default_country_code - cattr_accessor :default_area_code - cattr_accessor :named_formats - - # length of first number part (using multi number format) - cattr_accessor :n1_length - # default length of first number part - @@n1_length = 3 - - @@named_formats = { - :default => "+%c%a%n", - :default_with_extension => "+%c%a%nx%x", - :europe => '+%c (0) %a %f %l', - :us => "(%a) %f-%l" - } - - def initialize(*hash_or_args) - if hash_or_args.first.is_a?(Hash) - hash_or_args = hash_or_args.first - keys = {:number => :number, :area_code => :area_code, :country_code => :country_code, :extension => :extension} - else - keys = {:number => 0, :area_code => 1, :country_code => 2, :extension => 3} - end - - self.number = hash_or_args[ keys[:number] ] - self.area_code = hash_or_args[ keys[:area_code] ] || self.default_area_code - 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? - end - - # create a new phone number by parsing a string - # the format of the string is detect automatically (from FORMATS) - def self.parse(string, options={}) - if string.present? - PhoneCountry.load - extension = extract_extension(string) - string = normalize(string) - - options[:country_code] ||= self.default_country_code - options[:area_code] ||= self.default_area_code - - parts = split_to_parts(string, options) - - pn = Phone.new(parts) if parts - if pn.present? and extension.present? - pn.extension = extension +require File.join(File.dirname(__FILE__), 'country') + +module Phoner + class Phone + NUMBER = '([0-9]{1,8})$' + DEFAULT_AREA_CODE = '[2-9][0-8][0-9]' # USA + + attr_accessor :country_code, :area_code, :number, :extension + + cattr_accessor :default_country_code + cattr_accessor :default_area_code + cattr_accessor :named_formats + + # length of first number part (using multi number format) + cattr_accessor :n1_length + # default length of first number part + @@n1_length = 3 + + @@named_formats = { + :default => "+%c%a%n", + :default_with_extension => "+%c%a%nx%x", + :europe => '+%c (0) %a %f %l', + :us => "(%a) %f-%l" + } + + def initialize(*hash_or_args) + if hash_or_args.first.is_a?(Hash) + hash_or_args = hash_or_args.first + keys = {:number => :number, :area_code => :area_code, :country_code => :country_code, :extension => :extension} + else + keys = {:number => 0, :area_code => 1, :country_code => 2, :extension => 3} end - return pn - end - end - - # is this string a valid phone number? - def self.valid?(string) - begin - parse(string).present? - rescue RuntimeError # if we encountered exceptions (missing country code, missing area code etc) - return false - end - end - - # split string into hash with keys :country_code, :area_code and :number - def self.split_to_parts(string, options = {}) - country = detect_country(string) - - if country - options[:country_code] = country.country_code - string = string.gsub(country.country_code_regexp, '0') - else - if options[:country_code] - country = PhoneCountry.find_by_country_code options[:country_code] + + self.number = hash_or_args[ keys[:number] ] + self.area_code = hash_or_args[ keys[:area_code] ] || self.default_area_code + 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? + end + + # create a new phone number by parsing a string + # the format of the string is detect automatically (from FORMATS) + def self.parse(string, options={}) + if string.present? + Country.load + extension = extract_extension(string) + string = normalize(string) + + options[:country_code] ||= self.default_country_code + options[:area_code] ||= self.default_area_code + + parts = split_to_parts(string, options) + + pn = Phone.new(parts) if parts + if pn.present? and extension.present? + pn.extension = extension + end + return pn end end - - if country.nil? - if options[:country_code].nil? - raise "Must enter country code or set default country code" + + # is this string a valid phone number? + def self.valid?(string) + begin + parse(string).present? + rescue RuntimeError # if we encountered exceptions (missing country code, missing area code etc) + return false + end + end + + # split string into hash with keys :country_code, :area_code and :number + def self.split_to_parts(string, options = {}) + country = detect_country(string) + + if country + options[:country_code] = country.country_code + string = string.gsub(country.country_code_regexp, '0') else - raise "Could not find country with country code #{options[:country_code]}" + if options[:country_code] + country = Country.find_by_country_code options[:country_code] + end end + + if country.nil? + if options[:country_code].nil? + raise "Must enter country code or set default country code" + else + raise "Could not find country with country code #{options[:country_code]}" + end + end + + format = detect_format(string, country) + + return nil if format.nil? + + parts = string.match formats(country)[format] + + case format + when :short + {:number => parts[2], :area_code => parts[1], :country_code => options[:country_code]} + when :really_short + {:number => parts[1], :area_code => options[:area_code], :country_code => options[:country_code]} + end end - - format = detect_format(string, country) - - return nil if format.nil? - - parts = string.match formats(country)[format] - - case format - when :short - {:number => parts[2], :area_code => parts[1], :country_code => options[:country_code]} - when :really_short - {:number => parts[1], :area_code => options[:area_code], :country_code => options[:country_code]} - end - end - - # detect country from the string entered - def self.detect_country(string) - detected_country = nil - # find if the number has a country code - PhoneCountry.all.each_pair do |country_code, country| - if string =~ country.country_code_regexp - detected_country = country + + # detect country from the string entered + def self.detect_country(string) + detected_country = nil + # find if the number has a country code + Country.all.each_pair do |country_code, country| + if string =~ country.country_code_regexp + detected_country = country + end end + detected_country end - detected_country - end - - def self.formats(country) - area_code_regexp = country.area_code || DEFAULT_AREA_CODE - { - # 047451588, 013668734 - :short => Regexp.new('^0?(' + area_code_regexp + ')' + NUMBER), - # 451588 - :really_short => Regexp.new('^' + NUMBER) - } - end - - # detect format (from FORMATS) of input string - def self.detect_format(string_with_number, country) - arr = [] - formats(country).each_pair do |format, regexp| - arr << format if string_with_number =~ regexp - end - -# raise "Detected more than 1 format for #{string_with_number}" if arr.size > 1 - if arr.length > 1 -# puts %Q{detect_format: more than one format found - #{arr.inspect}} - return :really_short - end - arr.first - end - - # fix string so it's easier to parse, remove extra characters etc. - def self.normalize(string_with_number) - string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+') - end - - # pull off anything that look like an extension - #TODO: refactor things so this doesn't change string as a side effect - # - def self.extract_extension(string) - return nil if string.nil? - if string.sub! /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(*([-0-9]{1,})\)*#?$/i, '' - extension = $2 - return extension + + def self.formats(country) + area_code_regexp = country.area_code || DEFAULT_AREA_CODE + { + # 047451588, 013668734 + :short => Regexp.new('^0?(' + area_code_regexp + ')' + NUMBER), + # 451588 + :really_short => Regexp.new('^' + NUMBER) + } end + + # detect format (from FORMATS) of input string + def self.detect_format(string_with_number, country) + arr = [] + formats(country).each_pair do |format, regexp| + arr << format if string_with_number =~ regexp + end + + # raise "Detected more than 1 format for #{string_with_number}" if arr.size > 1 + if arr.length > 1 + # puts %Q{detect_format: more than one format found - #{arr.inspect}} + return :really_short + end + arr.first + end + + # fix string so it's easier to parse, remove extra characters etc. + def self.normalize(string_with_number) + string_with_number.gsub("(0)", "").gsub(/[^0-9+]/, '').gsub(/^00/, '+') + end + + # pull off anything that look like an extension + #TODO: refactor things so this doesn't change string as a side effect + # + def self.extract_extension(string) + return nil if string.nil? + if string.sub! /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(*([-0-9]{1,})\)*#?$/i, '' + extension = $2 + return extension + end + # + # We already returned any recognizable extension. + # However, we might still have extra junk to the right + # of the phone number proper, so just chop it off. + # + idx = string.rindex(/[0-9]/) + return nil if idx.nil? + return nil if idx == (string.length - 1) # at the end + string.slice!((idx+1)..-1) # chop it + return nil + end + + # format area_code with trailing zero (e.g. 91 as 091) + # format area_code with trailing zero (e.g. 91 as 091) + def area_code_long + "0" + area_code if area_code + end + + # first n characters of :number + def number1 + number[0...self.class.n1_length] + end + + # everything left from number after the first n characters (see number1) + def number2 + n2_length = number.size - self.class.n1_length + number[-n2_length, n2_length] + end + + # Formats the phone number. + # + # if the method argument is a String, it is used as a format string, with the following fields being interpolated: # - # We already returned any recognizable extension. - # However, we might still have extra junk to the right - # of the phone number proper, so just chop it off. + # * %c - country_code (385) + # * %a - area_code (91) + # * %A - area_code with leading zero (091) + # * %n - number (5125486) + # * %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512) + # * %l - last characters of number (5486) + # * %x - entire extension # - idx = string.rindex(/[0-9]/) - return nil if idx.nil? - return nil if idx == (string.length - 1) # at the end - string.slice!((idx+1)..-1) # chop it - return nil - end - - # format area_code with trailing zero (e.g. 91 as 091) - # format area_code with trailing zero (e.g. 91 as 091) - def area_code_long - "0" + area_code if area_code - end - - # first n characters of :number - def number1 - number[0...self.class.n1_length] - end - - # everything left from number after the first n characters (see number1) - def number2 - n2_length = number.size - self.class.n1_length - number[-n2_length, n2_length] - end - - # Formats the phone number. - # - # if the method argument is a String, it is used as a format string, with the following fields being interpolated: - # - # * %c - country_code (385) - # * %a - area_code (91) - # * %A - area_code with leading zero (091) - # * %n - number (5125486) - # * %f - first @@n1_length characters of number (configured through Phone.n1_length), default is 3 (512) - # * %l - last characters of number (5486) - # * %x - entire extension - # - # if the method argument is a Symbol, it is used as a lookup key for a format String in Phone.named_formats - # pn.format(:europe) - def format(fmt) - if fmt.is_a?(Symbol) - raise "The format #{fmt} doesn't exist'" unless named_formats.has_key?(fmt) - format_number named_formats[fmt] - else - format_number(fmt) - end - end - - # the default format is "+%c%a%n" - def to_s - format(:default) - end - - # does this number belong to the default country code? - def has_default_country_code? - country_code == self.class.default_country_code - end - - # does this number belong to the default area code? - def has_default_area_code? - area_code == self.class.default_area_code - end - - private - - def format_number(fmt) - result = fmt.gsub("%c", country_code || ""). - gsub("%a", area_code || ""). - gsub("%A", area_code_long || ""). - gsub("%n", number || ""). - gsub("%f", number1 || ""). - gsub("%l", number2 || ""). - gsub("%x", extension || "") - return result - end -end + # if the method argument is a Symbol, it is used as a lookup key for a format String in Phone.named_formats + # pn.format(:europe) + def format(fmt) + if fmt.is_a?(Symbol) + raise "The format #{fmt} doesn't exist'" unless named_formats.has_key?(fmt) + format_number named_formats[fmt] + else + format_number(fmt) + end + end + + # the default format is "+%c%a%n" + def to_s + format(:default) + end + + # does this number belong to the default country code? + def has_default_country_code? + country_code == self.class.default_country_code + end + + # does this number belong to the default area code? + def has_default_area_code? + area_code == self.class.default_area_code + end + + private + + def format_number(fmt) + result = fmt.gsub("%c", country_code || ""). + gsub("%a", area_code || ""). + gsub("%A", area_code_long || ""). + gsub("%n", number || ""). + gsub("%f", number1 || ""). + gsub("%l", number2 || ""). + gsub("%x", extension || "") + return result + end + end +end \ No newline at end of file diff --git a/lib/phone_country.rb b/lib/phone_country.rb deleted file mode 100644 index fe9371d..0000000 --- a/lib/phone_country.rb +++ /dev/null @@ -1,27 +0,0 @@ -class PhoneCountry < Struct.new(:name, :country_code, :char_2_code, :area_code) - cattr_accessor :all - - def self.load - return @@all if @@all.present? - - data_file = File.join(File.dirname(__FILE__), '..', 'data', 'phone_countries.yml') - - @@all = {} - YAML.load(File.read(data_file)).each_pair do |key, c| - @@all[key] = PhoneCountry.new(c[:name], c[:country_code], c[:char_2_code], c[:area_code]) - end - @@all - end - - def to_s - name - end - - def self.find_by_country_code(code) - @@all[code] - end - - def country_code_regexp - Regexp.new("^[+]#{country_code}") - end -end diff --git a/phone.gemspec b/phone.gemspec index c70e589..a21f5ca 100644 --- a/phone.gemspec +++ b/phone.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'phone' - s.version = '0.9.9.3' - s.date = '2010-07-30' + s.version = '1.0' + s.date = '2011-04-05' s.summary = "Phone number parsing, validation and formatting" s.description = "Phone number parsing, validation and formatting." @@ -22,7 +22,7 @@ Gem::Specification.new do |s| LICENSE data/phone_countries.yml lib/phone.rb - lib/phone_country.rb + lib/country.rb lib/support.rb test/extension_test.rb test/phone_test.rb diff --git a/test/countries/hr_test.rb b/test/countries/hr_test.rb index 4cdeeec..5866022 100644 --- a/test/countries/hr_test.rb +++ b/test/countries/hr_test.rb @@ -28,49 +28,49 @@ def test_zagreb_long_with_leading_zeros end def test_short_without_special_characters_with_country - Phone.default_country_code = '385' + Phoner::Phone.default_country_code = '385' parse_test('044885047', '385', '44', '885047') end def test_zagreb_short_without_special_characters_with_country - Phone.default_country_code = '385' + Phoner::Phone.default_country_code = '385' parse_test('013668734', '385', '1', '3668734') end def test_long_with_zero_in_brackets - Phone.default_country_code = nil + Phoner::Phone.default_country_code = nil parse_test('+385 (0)1 366 8111', '385', '1', '3668111') end def test_has_default_country_code - Phone.default_country_code = '385' + Phoner::Phone.default_country_code = '385' - assert_equal Phone.parse('+38547451588').has_default_country_code?, true - assert_equal Phone.parse('+38647451588').has_default_country_code?, false + assert_equal Phoner::Phone.parse('+38547451588').has_default_country_code?, true + assert_equal Phoner::Phone.parse('+38647451588').has_default_country_code?, false end def test_has_default_area_code - Phone.default_country_code = '385' - Phone.default_area_code = '47' + Phoner::Phone.default_country_code = '385' + Phoner::Phone.default_area_code = '47' - assert_equal Phone.parse('047/451-588').has_default_area_code?, true - assert_equal Phone.parse('032/336-1456').has_default_area_code?, false + assert_equal Phoner::Phone.parse('047/451-588').has_default_area_code?, true + assert_equal Phoner::Phone.parse('032/336-1456').has_default_area_code?, false end def test_validates - Phone.default_country_code = nil - assert_equal Phone.valid?('00385915125486'), true - assert_equal Phone.valid?('+385915125486'), true - assert_equal Phone.valid?('+385 (91) 512 5486'), true - assert_equal Phone.valid?('+38547451588'), true + Phoner::Phone.default_country_code = nil + assert_equal Phoner::Phone.valid?('00385915125486'), true + assert_equal Phoner::Phone.valid?('+385915125486'), true + assert_equal Phoner::Phone.valid?('+385 (91) 512 5486'), true + assert_equal Phoner::Phone.valid?('+38547451588'), true - Phone.default_country_code = '385' - assert_equal Phone.valid?('0915125486'), true - assert_equal Phone.valid?('091/512-5486'), true - assert_equal Phone.valid?('091/512-5486'), true - assert_equal Phone.valid?('091 512 54 86'), true - assert_equal Phone.valid?('091-512-54-86'), true - assert_equal Phone.valid?('047/451-588'), true + Phoner::Phone.default_country_code = '385' + assert_equal Phoner::Phone.valid?('0915125486'), true + assert_equal Phoner::Phone.valid?('091/512-5486'), true + assert_equal Phoner::Phone.valid?('091/512-5486'), true + assert_equal Phoner::Phone.valid?('091 512 54 86'), true + assert_equal Phoner::Phone.valid?('091-512-54-86'), true + assert_equal Phoner::Phone.valid?('047/451-588'), true end end diff --git a/test/countries/pt_test.rb b/test/countries/pt_test.rb index 97db4e8..ee90224 100644 --- a/test/countries/pt_test.rb +++ b/test/countries/pt_test.rb @@ -51,7 +51,7 @@ def test_pontadelgada # 707-708: Premium Numbers def test_707 - Phone.default_country_code = '351' + Phoner::Phone.default_country_code = '351' parse_test('707 123 456', '351', '707', '123456') end @@ -59,17 +59,17 @@ def test_707 # 800: Numero verde ("Green Number") def test_800 - Phone.default_country_code = '351' + Phoner::Phone.default_country_code = '351' parse_test('800 123 456', '351', '800', '123456') end # 808: Numero azul ("Blue Number") def test_808 - Phone.default_country_code = '351' + Phoner::Phone.default_country_code = '351' parse_test('808 123 456', '351', '808', '123456') end # 809: Custo partilhado ("Shared cost") def test_809 - Phone.default_country_code = '351' + Phoner::Phone.default_country_code = '351' parse_test('809 123 456', '351', '809', '123456') end @@ -108,22 +108,22 @@ def test_zonmobile end def test_validates - Phone.default_country_code = nil - assert_equal Phone.valid?('00351211234567'), true - assert_equal Phone.valid?('00351911234567'), true - assert_equal Phone.valid?('+351931234567'), true - assert_equal Phone.valid?('+351 (911) 123 456'), true - assert_equal Phone.valid?('+351921123456'), true + Phoner::Phone.default_country_code = nil + assert_equal Phoner::Phone.valid?('00351211234567'), true + assert_equal Phoner::Phone.valid?('00351911234567'), true + assert_equal Phoner::Phone.valid?('+351931234567'), true + assert_equal Phoner::Phone.valid?('+351 (911) 123 456'), true + assert_equal Phoner::Phone.valid?('+351921123456'), true - Phone.default_country_code = '351' - assert_equal Phone.valid?('(931) 234-567'), true - assert_equal Phone.valid?('(211) 234 567'), true - assert_equal Phone.valid?('232-123-456'), true - assert_equal Phone.valid?('232123456'), true - assert_equal Phone.valid?('92 212 34 56'), true - assert_equal Phone.valid?('221234567'), true - assert_equal Phone.valid?('708123456'), true - assert_equal Phone.valid?('800 123 456'), true + Phoner::Phone.default_country_code = '351' + assert_equal Phoner::Phone.valid?('(931) 234-567'), true + assert_equal Phoner::Phone.valid?('(211) 234 567'), true + assert_equal Phoner::Phone.valid?('232-123-456'), true + assert_equal Phoner::Phone.valid?('232123456'), true + assert_equal Phoner::Phone.valid?('92 212 34 56'), true + assert_equal Phoner::Phone.valid?('221234567'), true + assert_equal Phoner::Phone.valid?('708123456'), true + assert_equal Phoner::Phone.valid?('800 123 456'), true end end diff --git a/test/countries/us_test.rb b/test/countries/us_test.rb index 90c1819..e31a04d 100644 --- a/test/countries/us_test.rb +++ b/test/countries/us_test.rb @@ -12,13 +12,13 @@ def test_tollfree end def test_long_with_default_country_code - Phone.default_country_code = '1' + Phoner::Phone.default_country_code = '1' parse_test('2069735100', '1', '206', '9735100') end def test_short_with_default_country_code_and_area_code - Phone.default_country_code = '1' - Phone.default_area_code = '206' + Phoner::Phone.default_country_code = '1' + Phoner::Phone.default_area_code = '206' parse_test('9735100', '1', '206', '9735100') end end diff --git a/test/extension_test.rb b/test/extension_test.rb index 651a246..9a8931f 100644 --- a/test/extension_test.rb +++ b/test/extension_test.rb @@ -3,7 +3,7 @@ class ExtensionTest < Test::Unit::TestCase def test_parse_usa_long_with_simple_extension - pn = Phone.parse "+1 2069735100 x143" + pn = Phoner::Phone.parse "+1 2069735100 x143" assert_not_nil pn, %Q{parse should pass} assert_equal '9735100', pn.number @@ -13,12 +13,12 @@ def test_parse_usa_long_with_simple_extension end def test_to_s_with_extension - pn = Phone.new '5125486', '91', '385', '143' + pn = Phoner::Phone.new '5125486', '91', '385', '143' assert_equal '+385915125486x143', pn.format(:default_with_extension) end def test_format_with_extension - pn = Phone.new '5125486', '91', '385', '143' + pn = Phoner::Phone.new '5125486', '91', '385', '143' assert_equal '(091)/512-5486 x 143', pn.format('(%A)/%f-%l x %x') end diff --git a/test/phone_test.rb b/test/phone_test.rb index 97ded49..d38d178 100644 --- a/test/phone_test.rb +++ b/test/phone_test.rb @@ -3,104 +3,104 @@ class PhoneTest < Test::Unit::TestCase def test_number_without_country_code_initialize - Phone.default_country_code = nil + Phoner::Phone.default_country_code = nil assert_raise RuntimeError do - pn = Phone.new '5125486', '91' + pn = Phoner::Phone.new '5125486', '91' end end def test_number_without_country_and_area_code_initialize - Phone.default_country_code = nil - Phone.default_area_code = nil + Phoner::Phone.default_country_code = nil + Phoner::Phone.default_area_code = nil assert_raise RuntimeError do - pn = Phone.new '451588' + pn = Phoner::Phone.new '451588' end end def test_number_with_default_area_code_initialize - Phone.default_country_code = '385' - Phone.default_area_code = '47' + Phoner::Phone.default_country_code = '385' + Phoner::Phone.default_area_code = '47' - pn = Phone.new '451588' + pn = Phoner::Phone.new '451588' assert pn.number == '451588' assert pn.area_code == '47' assert pn.country_code == '385' end def test_number_with_default_country_code_initialize - Phone.default_country_code = '386' + Phoner::Phone.default_country_code = '386' - pn = Phone.new '5125486', '91' + pn = Phoner::Phone.new '5125486', '91' assert pn.number == '5125486' assert pn.area_code == '91' assert pn.country_code == '386' end def test_number_with_country_code_initialize - Phone.default_country_code = '387' + Phoner::Phone.default_country_code = '387' - pn = Phone.new '5125486', '91', '385' + pn = Phoner::Phone.new '5125486', '91', '385' assert pn.number == '5125486' assert pn.area_code == '91' assert pn.country_code == '385' end def test_parse_empty - assert_equal Phone.parse(''), nil - assert_equal Phone.parse(nil), nil + assert_equal Phoner::Phone.parse(''), nil + assert_equal Phoner::Phone.parse(nil), nil end def test_parse_short_without_special_characters_without_country - Phone.default_country_code = nil + Phoner::Phone.default_country_code = nil assert_raise RuntimeError do - pn = Phone.parse "0915125486" + pn = Phoner::Phone.parse "0915125486" end end def test_parse_short_with_special_characters_without_country - Phone.default_country_code = nil + Phoner::Phone.default_country_code = nil assert_raise RuntimeError do - pn = Phone.parse "091/512-5486" + pn = Phoner::Phone.parse "091/512-5486" end end def test_to_s - Phone.default_country_code = nil - pn = Phone.new '5125486', '91', '385' + Phoner::Phone.default_country_code = nil + pn = Phoner::Phone.new '5125486', '91', '385' assert pn.to_s == '+385915125486' end def test_to_s_without_country_code - Phone.default_country_code = '385' - pn = Phone.new '5125486', '91' + Phoner::Phone.default_country_code = '385' + pn = Phoner::Phone.new '5125486', '91' assert pn.format("0%a%n") == '0915125486' end def test_format_special_with_country_code - Phone.default_country_code = nil - pn = Phone.new '5125486', '91', '385' + Phoner::Phone.default_country_code = nil + pn = Phoner::Phone.new '5125486', '91', '385' assert pn.format("+ %c (%a) %n") == '+ 385 (91) 5125486' end def test_format_special_without_country_code - Phone.default_country_code = '385' - pn = Phone.new '5125486', '91' + Phoner::Phone.default_country_code = '385' + pn = Phoner::Phone.new '5125486', '91' assert_equal pn.format("%A/%f-%l"), '091/512-5486' end def test_format_with_symbol_specifier - Phone.default_country_code = nil - pn = Phone.new '5125486', '91', '385' + Phoner::Phone.default_country_code = nil + pn = Phoner::Phone.new '5125486', '91', '385' assert_equal pn.format(:europe), '+385 (0) 91 512 5486' end def test_doesnt_validate - assert_equal Phone.valid?('asdas'), false - assert_equal Phone.valid?('385915125486'), false + assert_equal Phoner::Phone.valid?('asdas'), false + assert_equal Phoner::Phone.valid?('385915125486'), false end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 769ae43..540e5be 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,7 +6,7 @@ require 'phone' def parse_test(raw, country_code, area_code, number) - pn = Phone.parse(raw) + pn = Phoner::Phone.parse(raw) assert_not_nil pn, %Q{parse should pass} assert_equal pn.country_code, country_code