Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use specified country_code in phone number when normalizing #23

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'

# Preempt the default loading so that we don't get an unqualified Country class imported.
gem 'countries', :require => 'iso3166'
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PATH
phony (>= 1.7.7)

GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
activemodel (3.2.13)
activesupport (= 3.2.13)
Expand Down
8 changes: 5 additions & 3 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ def self.normalize_number(number, options = {})
number = number.clone # Just to be sure, we don't want to change the original.
number.gsub!(/[^\d\+]/, '') # Strips weird stuff from the number
return if number.blank?
if country_number = country_number_for(options[:country_code] || options[:default_country_code])
# Add country_number if missing
number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
if !number.starts_with?('+')
if country_number = country_number_for(options[:country_code] || options[:default_country_code])
# Add country_number if missing
number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
end
end
number = Phony.normalize(number) if Phony.plausible?(number)
return number.to_s
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/phony_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
end

it "should normalize a number with a country_code different than the specified country_code" do
PhonyRails.normalize_number('+4790909090', :country_code => 'SE').should eql('4790909090')
end

it "should handle different countries" do
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
PhonyRails.normalize_number('+43 664 3830412', :country_code => 'AT').should eql('436643830412')
Expand Down