Permalink
Browse files

+ 1.6.1, ! 00 normalization

  • Loading branch information...
1 parent 4270bcd commit b46b9c927a458891dd7cdd2300f5f450cebba6c2 @floere committed Nov 25, 2011
Showing with 29 additions and 27 deletions.
  1. +9 −9 Gemfile.lock
  2. +2 −1 lib/phony/country_codes.rb
  3. +6 −6 lib/phony/national_code.rb
  4. +1 −1 phony.gemspec
  5. +1 −0 spec/lib/phony/countries_spec.rb
  6. +10 −10 spec/lib/phony_spec.rb
View
@@ -2,16 +2,16 @@ GEM
remote: http://rubygems.org/
remote: http://rubygems.org/
specs:
- diff-lcs (1.1.2)
- rake (0.8.7)
- rspec (2.3.0)
- rspec-core (~> 2.3.0)
- rspec-expectations (~> 2.3.0)
- rspec-mocks (~> 2.3.0)
- rspec-core (2.3.1)
- rspec-expectations (2.3.0)
+ diff-lcs (1.1.3)
+ rake (0.9.2.2)
+ rspec (2.7.0)
+ rspec-core (~> 2.7.0)
+ rspec-expectations (~> 2.7.0)
+ rspec-mocks (~> 2.7.0)
+ rspec-core (2.7.1)
+ rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
- rspec-mocks (2.3.0)
+ rspec-mocks (2.7.0)
PLATFORMS
ruby
@@ -22,10 +22,11 @@ def self.instance
@instance ||= new
end
+ @@normalizing_pattern = /^0+|\D/
def normalize number
# Remove non-digit chars.
#
- number.gsub! /\D*/, EMPTY_STRING
+ number.gsub! @@normalizing_pattern, EMPTY_STRING
national_handler, cc, rest = split_cc number
@normalize_format % [cc, national_handler.normalize(rest)]
end
View
@@ -1,17 +1,17 @@
module Phony
-
+
# NationalCodes have a special numbers splitter, a national code splitter and a local code splitter.
#
class NationalCode
-
+
#
#
def initialize national_splitter, local_splitter, normalize = nil
@national_splitter = national_splitter
@local_splitter = local_splitter
@normalize = normalize != false
end
-
+
# Split gets a number without country code and splits it into
# its parts.
#
@@ -20,7 +20,7 @@ def split national_number
return ndc unless rest
[ndc, *@local_splitter.split(rest)]
end
-
+
# Split gets a number without country code and removes a relative zero.
#
# Note: Some cases, like Italy, don't remove the relative zero.
@@ -29,7 +29,7 @@ def normalize national_number
return national_number unless @normalize
national_number.gsub(/^0+/, '')
end
-
+
end
-
+
end
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'phony'
- s.version = '1.6.0'
+ s.version = '1.6.1'
s.authors = ['Florian Hanke']
s.email = 'florian.hanke+phony@gmail.com'
s.homepage = 'http://github.com/floere/phony'
@@ -86,6 +86,7 @@
end
it 'handles german numbers' do
Phony.split('493038625454').should == ['49', '30', '386', '25454'] # Berlin
+ Phony.split('4932221764542').should == ['49', '32', '221', '764542'] # Non-Geographical
Phony.split('4922137683323').should == ['49', '221', '376', '83323'] # Cologne
Phony.split('497614767676').should == ['49', '761', '476', '7676'] # Freiburg im Breisgau
Phony.split('4921535100').should == ['49', '2153', '510', '0'] # Nettetal-Lobberich
View
@@ -3,7 +3,7 @@
require 'spec_helper'
describe Phony do
-
+
describe 'nil cases' do
it "should raise on normalize nil" do
expect {
@@ -21,7 +21,7 @@
}.to raise_error(ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.split(number).")
end
end
-
+
describe "normalize" do
describe "some examples" do
it "should normalize an already normalized number" do
@@ -30,8 +30,8 @@
it "should normalize a format number" do
Phony.normalize('+41 44 364 35 33').should == '41443643533'
end
- it "should normalize a format number" do
- Phony.normalize('+41 44 364 35 33').should == '41443643533'
+ it "should normalize a 00 number" do
+ Phony.normalize('0041 44 364 35 33').should == '41443643533'
end
it "should normalize a service number" do
Phony.normalize('+41 800 11 22 33').should == '41800112233'
@@ -59,13 +59,13 @@
end
end
end
-
+
describe 'formatted' do
it 'is an alias of format' do
Phony.formatted('41443643532').should == '+41 44 364 35 32'
end
end
-
+
describe "format" do
describe "default" do
it "should format swiss numbers" do
@@ -182,7 +182,7 @@
end
end
end
-
+
context 'minimal cases' do
context 'normalizing' do
it 'handles completely crazy "numbers"' do
@@ -217,7 +217,7 @@
end
end
end
-
+
context "speed" do
before(:each) do
@phone_numbers = [
@@ -256,7 +256,7 @@
end
end
end
-
+
describe 'vanity' do
describe 'vanity_number?' do
it {Phony.vanity?('41800 WEGGLI').should be_true}
@@ -266,7 +266,7 @@
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
it {Phony.vanity_to_number('41800WEGGLI').should == '41800934454'}
it {Phony.vanity_to_number('41800weggli').should == '41800934454'}

0 comments on commit b46b9c9

Please sign in to comment.