Skip to content

Commit 7edccc3

Browse files
author
Joost Hietbrink
committed
Fix github issue joost#22. joost#22
1 parent 3160339 commit 7edccc3

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source :rubygems
1+
source 'https://rubygems.org'
22

33
# Preempt the default loading so that we don't get an unqualified Country class imported.
44
gem 'countries', :require => 'iso3166'

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PATH
77
phony (>= 1.7.7)
88

99
GEM
10-
remote: http://rubygems.org/
10+
remote: https://rubygems.org/
1111
specs:
1212
activemodel (3.2.13)
1313
activesupport (= 3.2.13)

lib/phony_rails.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ def self.normalize_number(number, options = {})
2121
number = number.clone # Just to be sure, we don't want to change the original.
2222
number.gsub!(/[^\d\+]/, '') # Strips weird stuff from the number
2323
return if number.blank?
24-
if country_number = country_number_for(options[:country_code] || options[:default_country_code])
25-
# Add country_number if missing
24+
if country_number = country_number_for(options[:country_code])
25+
# (Force) add country_number if missing
2626
number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
27+
elsif default_country_number = country_number_for(options[:default_country_code])
28+
# Add default_country_number if missing
29+
number = "#{default_country_number}#{number}" if not number =~ /^(00|\+)/
2730
end
2831
number = Phony.normalize(number) if Phony.plausible?(number)
2932
return number.to_s

spec/lib/phony_rails_spec.rb

+29-10
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,38 @@
6868
end
6969

7070
describe 'PhonyRails#normalize_number' do
71-
it "should normalize a number with a default_country_code" do
72-
PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('31101234123')
73-
end
71+
context 'number with a country code' do
72+
73+
it "should not add default_country_code" do
74+
PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE').should eql('4790909090') # SE = +46
75+
end
76+
77+
it "should force add country_code" do
78+
PhonyRails.normalize_number('+4790909090', :country_code => 'SE').should eql('464790909090')
79+
end
7480

75-
it "should normalize a number with a country_code" do
76-
PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
77-
PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
7881
end
7982

80-
it "should handle different countries" do
81-
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
82-
PhonyRails.normalize_number('+43 664 3830412', :country_code => 'AT').should eql('436643830412')
83-
PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('442033308897')
83+
context 'number without a country code' do
84+
85+
it "should normalize with a default_country_code" do
86+
PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('31101234123')
87+
end
88+
89+
it "should normalize with a country_code" do
90+
PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
91+
PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
92+
end
93+
94+
it "should handle different countries" do
95+
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
96+
PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('442033308897')
97+
end
98+
99+
it "should prefer country_code over default_country_code" do
100+
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL').should eql('49308612906')
101+
end
102+
84103
end
85104

86105
it "should handle some edge cases" do

0 commit comments

Comments
 (0)