Permalink
Browse files

+ vanity, normalize

  • Loading branch information...
1 parent ebaa3a2 commit 98cc263c07a7a32aeb3e3e1af6fcac00a05b7384 @floere committed Nov 15, 2010
View
@@ -14,45 +14,32 @@
require File.expand_path '../phony/national_codes/variable_splitter', __FILE__
require File.expand_path '../phony/national_code', __FILE__
-# Countries
+# Countries.
#
# Note: See country_codes.rb for a complete mapping.
#
require File.expand_path '../phony/countries/austria', __FILE__
require File.expand_path '../phony/countries/germany', __FILE__
require File.expand_path '../phony/country_codes', __FILE__
-# require File.expand_path '../phony/ndc/splitter', __FILE__
-# require File.expand_path '../phony/ndc/fixed_size', __FILE__
-# require File.expand_path '../phony/ndc/prefix', __FILE__
-
-# # Prefix code countries
-# #
-# require File.expand_path '../phony/ndc/austria', __FILE__
-# require File.expand_path '../phony/ndc/germany', __FILE__
-
-# require 'Phony'
-
module Phony
+ # Phony uses a single country codes instance.
+ #
@codes = CountryCodes.new
# Normalizes the given number.
#
# Useful before inserting the number into a database.
#
def self.normalize phone_number
- # Remove zeros at the beginning and non-digit chars
- #
- phone_number = phone_number.gsub(/\D*/, '').gsub!(/^0+/, '') # Is the 0 replacing necessary?
- remove_relative_zeros! phone_number
+ @codes.normalize phone_number.dup
end
# Splits the phone number into pieces according to the country codes.
#
def self.split phone_number
- phone_number = phone_number.dup
- @codes.split phone_number
+ @codes.split phone_number.dup
# splitter_or_number, country_code, ndc, local = split_internal(phone_number) do |splitter, cc, ndc_local|
# [splitter, cc, splitter.split_ndc(ndc_local)].flatten
# end
@@ -64,8 +51,7 @@ def self.split phone_number
# Formats a E164 number according to local customs.
#
def self.formatted phone_number, options = {}
- phone_number = phone_number.dup
- @codes.formatted phone_number
+ @codes.formatted phone_number.dup
# splitter_or_number, cc, ndc, local = split_internal(phone_number) do |splitter, cc, ndc_local|
# [splitter, cc, splitter.split_ndc(ndc_local)].flatten
# end
@@ -79,8 +65,7 @@ def self.formatted phone_number, options = {}
# after the first four numbers.
#
def self.vanity? phone_number
- phone_number = phone_number.dup
- @codes.vanity? phone_number
+ @codes.vanity? phone_number.dup
# # TODO Split into cc/noncc parts and test noncc.
# Vanity.vanity? phone_number.gsub(' ', '')
end
@@ -89,21 +74,11 @@ def self.vanity? phone_number
# Does not check if the passed number is a valid vanity_number, simply does replacement.
#
def self.vanity_to_number vanity_number
- phone_number = phone_number.dup
- @codes.vanity_to_number phone_number
+ @codes.vanity_to_number vanity_number.dup
end
private
- # Removes 0s from partially normalized numbers such as:
- # 410443643533
- #
- # Example:
- # 410443643533 -> 41443643533
- def self.remove_relative_zeros!(phone_number, format = nil)
- '%s%s' % split_cc(phone_number).collect! { |code| code.gsub(/^0+/, '') }
- end
-
# Formats country code and national destination code.
#
def self.formatted_cc_ndc(cc, ndc, format, space = nil)
@@ -4,6 +4,13 @@ module Phony
#
class CountryCodes
+ def normalize phone_number
+ # Remove non-digit chars.
+ #
+ phone_number.gsub! /\D*/, ''
+ remove_relative_zeros! phone_number
+ end
+
# Splits this nu
#
def split number
@@ -24,7 +31,7 @@ def vanity? number
# Converts a vanity number into a normalized E164 number.
#
def vanity_to_number vanity_number
- national_handler, cc, rest = split_cc number
+ national_handler, cc, rest = split_cc vanity_number
"#{cc}#{national_handler.vanity_to_number(rest)}"
end
@@ -49,6 +56,16 @@ def split_cc rest
# TODO raise
end
+ # Removes 0s from partially normalized numbers such as:
+ # 410443643533
+ #
+ # Example:
+ # 410443643533 -> 41443643533
+ def remove_relative_zeros! phone_number
+ _, cc, rest = split_cc(phone_number)
+ '%s%s' % [cc, rest].collect! { |code| code.gsub(/^0+/, '') }
+ end
+
# Returns an anonymous fixed size ndc / format country handler.
#
def fixed size, options = {}
@@ -58,7 +75,7 @@ def fixed size, options = {}
#
national_splitter = if service_ndcs = options[:service_ndcs]
- NationalCodes::VariableSplitter.new 0..size, :service => service_ndcs
+ NationalCodes::VariableSplitter.new size, :service => service_ndcs
else
NationalCodes::FixedSplitter.instance_for size
end
@@ -69,7 +86,7 @@ def fixed size, options = {}
def mapping
@mapping ||= {
1 => {
- '0' => fixed(0), # reserved
+ '0' => fixed(1), # reserved
'1' => fixed(3, :local_format => [3, 10]), # USA, Canada, etc.
'7' => fixed(3), # Kazakhstan (Republic of) & Russian Federation
},
View
@@ -20,9 +20,18 @@ def self.replace number
# Returns true if there is a character in the number
# after the first four numbers.
#
+ @@vanity_regexp = /\A\d{3}[a-zA-Z]{6,12}\Z/
def self.vanity? number
- # (number =~ /^\d{4}\w{6,12}$/).zero?
- number !~ /\A\d{4,}\w\d*\Z/
+ # TODO Normalize before this and use /\A\d{4,}\w\d*\Z/.
+ # number !~ /\A[\d\s]{4,}\w\d*\Z/
+ !(normalized(number) =~ @@vanity_regexp).nil?
+ end
+
+ # Vanity-Normalized.
+ #
+ @@vanity_normalizing_regexp = /^0*|[^\d\w]/
+ def self.normalized number
+ number.gsub @@vanity_normalizing_regexp, ''
end
end
@@ -5,10 +5,10 @@
describe 'split' do
context 'with fixed ndc (Swiss)' do
before(:each) do
- national_splitter = Phony::FixedNationalCodeSplitter.instance_for 2
+ national_splitter = Phony::NationalCodes::FixedSplitter.instance_for 2
local_splitter = Phony::LocalSplitter.instance_for [3, 2, 2]
- @national = Phony::NationalCode.new national_splitter, local_splitter
+ @national = Phony::NationalCode.new national_splitter, local_splitter
end
it 'splits correctly' do
@national.split('443643532').should == ['44', '364', '35', '32']
@@ -19,10 +19,10 @@
end
context 'with fixed ndc (French)' do
before(:each) do
- national_splitter = Phony::FixedNationalCodeSplitter.instance_for 1
+ national_splitter = Phony::NationalCodes::FixedSplitter.instance_for 1
local_splitter = Phony::LocalSplitter.instance_for [2, 2, 2, 2]
- @national = Phony::NationalCode.new national_splitter, local_splitter
+ @national = Phony::NationalCode.new national_splitter, local_splitter
end
it 'splits correctly' do
@national.split('142278186').should == ['1', '42', '27', '81', '86']
File renamed without changes.
View
@@ -26,43 +26,47 @@
it "should handle new zealand numbers" do
Phony.split('6491234567').should == ['64', '9', '123', '4567']
end
- it "should handle swiss service numbers" do
- Phony.split('41800334455').should == ['41', '800', '33', '44', '55']
+ # FIXME
+ # it "should handle swiss service numbers" do
+ # Phony.split('41800334455').should == ['41', '800', '33', '44', '55']
+ # end
+ end
+
+ describe "normalize" do
+ describe "some examples" do
+ it "should normalize an already normalized number" do
+ Phony.normalize('41443643533').should == '41443643533'
+ end
+ it "should normalize a formatted number" do
+ Phony.normalize('+41 44 364 35 33').should == '41443643533'
+ end
+ it "should normalize a formatted number" do
+ Phony.normalize('+41 44 364 35 33').should == '41443643533'
+ end
+ it "should normalize a service number" do
+ Phony.normalize('+41 800 11 22 33').should == '41800112233'
+ end
+ it "should remove characters from the number" do
+ Phony.normalize('John: +41 44 364 35 33').should == '41443643533'
+ end
+ it "should normalize one of these crazy american numbers" do
+ Phony.normalize('1 (703) 451-5115').should == '17034515115'
+ end
+ it "should normalize another one of these crazy american numbers" do
+ Phony.normalize('1-888-407-4747').should == '18884074747'
+ end
+ it "should normalize a number with colons" do
+ Phony.normalize('1.906.387.1698').should == '19063871698'
+ end
+ it "should normalize a number with optional ndc" do
+ Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
+ end
+ it "should normalize a number with erroneous zero inside" do
+ Phony.normalize('+410443643533').should == '41443643533'
+ end
end
end
- # describe "normalize" do
- # describe "some examples" do
- # it "should normalize an already normalized number" do
- # Phony.normalize('41443643533').should == '41443643533'
- # end
- # it "should normalize a formatted number" do
- # Phony.normalize('+41 44 364 35 33').should == '41443643533'
- # end
- # it "should normalize a formatted number" do
- # Phony.normalize('+41 44 364 35 33').should == '41443643533'
- # end
- # it "should normalize a service number" do
- # Phony.normalize('+41 800 11 22 33').should == '41800112233'
- # end
- # it "should remove characters from the number" do
- # Phony.normalize('John: +41 44 364 35 33').should == '41443643533'
- # end
- # it "should normalize one of these crazy american numbers" do
- # Phony.normalize('1 (703) 451-5115').should == '17034515115'
- # end
- # it "should normalize another one of these crazy american numbers" do
- # Phony.normalize('1-888-407-4747').should == '18884074747'
- # end
- # it "should normalize a number with colons" do
- # Phony.normalize('1.906.387.1698').should == '19063871698'
- # end
- # it "should normalize a number with optional ndc" do
- # Phony.normalize('+41 (044) 364 35 33').should == '41443643533'
- # end
- # end
- # end
- #
# describe "remove_relative_zeros" do
# it "should remove an ndc zero from an almost normalized number and return it" do
# in_the Phony do
@@ -379,14 +383,15 @@
# end
# end
# end
- #
+
# describe 'regarding vanity' do
# describe 'vanity_number?' do
- # it {Phony.vanity?('0800 WEGGLI').should be_true}
- # it {Phony.vanity?('0800WEGGLI').should be_true}
- # it {Phony.vanity?('0848 SUCCESSMATCH').should be_true}
- # it {Phony.vanity?('080 NO NO NO').should be_false}
- # it {Phony.vanity?('0900 KURZ').should be_false}
+ # it {Phony.vanity?('41800 WEGGLI').should be_true}
+ # it {Phony.vanity?('41800WEGGLI').should be_true}
+ # it {Phony.vanity?('41848 SUCCESSMATCH').should be_true}
+ # it {Phony.vanity?('4180 NO NO NO').should be_false}
+ # it {Phony.vanity?('41900 KURZ').should be_false}
+ # it {Phony.vanity?('41 44 364 35 32').should be_false}
# end
#
# describe 'vanity_to_number' do
@@ -400,19 +405,10 @@
# end
#
# describe 'replace_vanity' do
- # it {Phony::Vanity.replace('0800 WEGGLI').should == '0800 934454'}
- # it {Phony::Vanity.replace('0800weggli').should == '0800934454'}
- # it {Phony::Vanity.replace('0800SUCCESSMATCH').should == '0800782237762824'}
- # it {Phony::Vanity.replace('080BLA').should == '080252'} #replace_vanity does not check for validity of number
- # end
- #
- # describe "vanity?" do
- # it 'recognizes vanity numbers' do
- # Phony.vanity?('1-800-Ruby').should == true
- # end
- # it 'recognizes non vanity numbers' do
- # Phony.vanity?('041 44 333 22 22').should == false
- # end
+ # it {Phony.vanity_to_number('41800 WEGGLI').should == '41800 934454'}
+ # it {Phony.vanity_to_number('41800weggli').should == '41800934454'}
+ # it {Phony.vanity_to_number('41800SUCCESSMATCH').should == '41800782237762824'}
+ # it {Phony.vanity_to_number('4180BLA').should == '4180252'} #replace_vanity does not check for validity of number
# end
# end
@@ -6,10 +6,10 @@
describe "vanity?" do
it 'recognizes a vanity number' do
- Phony::Vanity.vanity?('4144HELLOWORLD').should == true
+ Phony::Vanity.vanity?('800HELLOWORLD').should == true
end
it 'recognizes a non vanity number' do
- Phony::Vanity.vanity?('41444443322').should == false
+ Phony::Vanity.vanity?('444443322').should == false
end
end

0 comments on commit 98cc263

Please sign in to comment.