Permalink
Browse files

+ cleanup

  • Loading branch information...
1 parent 155aaf8 commit b742c4fa6f7a3284d5360e54fd0349fafc6d7597 @floere committed Nov 15, 2010
Showing with 142 additions and 372 deletions.
  1. +5 −59 lib/phony.rb
  2. +25 −4 lib/phony/country_codes.rb
  3. +14 −1 spec/lib/country_codes_spec.rb
  4. +98 −308 spec/lib/phony_spec.rb
View
@@ -40,34 +40,22 @@ def self.normalize phone_number
#
def self.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
- # return splitter_or_number if local.nil?
- #
- # [country_code, ndc, splitter_or_number.split_local(local)].flatten
end
# Formats a E164 number according to local customs.
#
def self.formatted phone_number, options = {}
- @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
- # return splitter_or_number if local.nil?
- #
- # space = options[:spaces] || ' '
- # formatted_cc_ndc(cc, ndc, options[:format], space) + splitter_or_number.locally_formatted(local, space)
+ formatted! phone_number.dup, options
end
-
+ def self.formatted! phone_number, options = {}
+ @codes.formatted phone_number
+ end
+
# Returns true if there is a character in the number
# after the first four numbers.
#
def self.vanity? phone_number
@codes.vanity? phone_number.dup
- # # TODO Split into cc/noncc parts and test noncc.
- # Vanity.vanity? phone_number.gsub(' ', '')
end
# Converts any character in the vanity_number to its numeric representation.
@@ -77,46 +65,4 @@ def self.vanity_to_number vanity_number
@codes.vanity_to_number vanity_number.dup
end
- private
-
- # Formats country code and national destination code.
- #
- def self.formatted_cc_ndc(cc, ndc, format, space = nil)
- space ||= ' '
- format, split_phone_number = case format
- when nil, :international_absolute, :international, :+
- [ndc.blank? ? '+%s%s' : '+%s%s%s%s', [cc, space, ndc, space]]
- when :international_relative
- [ndc.blank? ? '00%s%s' : '00%s%s%s%s', [cc, space, ndc, space]]
- when :national
- [ndc.blank? ? '' : '0%s%s', [ndc, space]]
- when :local
- ['', []]
- end
- format % split_phone_number
- end
-
- # def self.split_cc(phone_number)
- # split_internal(phone_number) do |splitter, cc, ndc_local|
- # [cc, ndc_local]
- # end
- # end
- #
- # def self.split_cc_ndc(phone_number)
- # split_internal(phone_number) do |splitter, cc, ndc_local|
- # splitter ? [cc, splitter.split_ndc(ndc_local)].flatten : [cc, ndc_local, '']
- # end
- # end
- #
- # def self.split_internal(phone_number)
- # number = phone_number.dup
- # presumed_code = ''
- # 1.upto(3) do |i|
- # presumed_code << number.slice!(0..0)
- # splitter = @@country_codes[i][presumed_code]
- # return yield(splitter, presumed_code, number) if splitter
- # end
- # return yield(nil, '', '')
- # end
-
end
@@ -4,11 +4,11 @@ module Phony
#
class CountryCodes
- def normalize phone_number
+ def normalize number
# Remove non-digit chars.
#
- phone_number.gsub! /\D*/, ''
- remove_relative_zeros! phone_number
+ number.gsub! /\D*/, ''
+ remove_relative_zeros! number
end
# Splits this nu
@@ -19,7 +19,28 @@ def split number
end
def formatted number, options = {}
-
+ format_cc_ndc_local options[:format], options[:spaces] || ' ', *split(number)
+ end
+ # Formats country code and national destination code.
+ #
+ def format_cc_ndc_local format, space, cc, ndc, *parts
+ "#{format_cc_ndc(format, space, cc, ndc)}#{format_local(space, parts)}"
+ end
+ def format_cc_ndc format, space, cc, ndc
+ format, split_phone_number = case format
+ when nil, :international_absolute, :international, :+
+ [ndc.blank? ? '+%s%s' : '+%s%s%s%s', [cc, space, ndc, space]]
+ when :international_relative
+ [ndc.blank? ? '00%s%s' : '00%s%s%s%s', [cc, space, ndc, space]]
+ when :national
+ [ndc.blank? ? '' : '0%s%s', [ndc, space]]
+ when :local
+ ['', []]
+ end
+ format % split_phone_number
+ end
+ def format_local space, parts_ary
+ parts_ary.join space.to_s
end
# Is the given number a vanity number?
@@ -12,10 +12,23 @@
end
end
- describe "remove_relative_zeros" do
+ describe 'remove_relative_zeros' do
it "should remove an ndc zero from an almost normalized number and return it" do
@countries.remove_relative_zeros!('410443643533').should == '41443643533'
end
end
+ describe 'formatted' do
+ it 'formats correctly' do
+ @countries.formatted('41443643532', :format => :international, :spaces => :-).should == '+41-44-364-35-32'
+ end
+ it 'formats correctly' do
+ @countries.formatted('41443643532', :format => :international_relative, :spaces => :-).should == '0041-44-364-35-32'
+ end
+ it 'formats correctly' do
+ @countries.formatted('41443643532', :format => :national, :spaces => :-).should == '044-364-35-32'
+ end
+
+ end
+
end
Oops, something went wrong.

0 comments on commit b742c4f

Please sign in to comment.