Skip to content

Commit

Permalink
Better PhonyRails.plausible_number? method. Closes #179.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joost Hietbrink committed Sep 5, 2018
1 parent e6294a8 commit 99f494d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,31 @@ def self.extract_default_country_number(options = {}, current_instance = nil)
options[:default_country_number] || country_number_for(country_code) || default_country_number
end

# Returns the country dail code (eg. '31') for a number (eg. +31612341234).
# Should probably be named 'country_number_from_number'.
def self.country_code_from_number(number)
return nil unless Phony.plausible?(number)
Phony.split(Phony.normalize(number)).first
end

# Returns the country (eg. 'NL') for a number (eg. +31612341234).
def self.country_from_number(number)
return nil unless Phony.plausible?(number)
country_codes_hash.select { |_country, hash| hash['country_code'] == country_code_from_number(number) }.keys[0]
end

# Wrapper for Phony.plausible?. Takes the same options as #normalize_number.
# NB: This method calls #normalize_number and passes _options_ directly to that method.
# It uses the 'cc' option for Phony. This was a required param before?
def self.plausible_number?(number, options = {})
return false if number.blank?
number = extract_extension(number).first
number = normalize_number(number, options)
country_number = options[:country_number] || country_number_for(options[:country_code]) ||
country_code_from_number(number) ||
options[:default_country_number] || country_number_for(options[:default_country_code]) ||
default_country_number
puts "Validating #{number} with #{country_number}"
Phony.plausible? number, cc: country_number
rescue StandardError
false
Expand Down
16 changes: 15 additions & 1 deletion spec/lib/phony_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ class NormalHome < ActiveRecord::Base
subject { described_class }
let(:valid_number) { '1 555 555 5555' }
let(:invalid_number) { '123456789 123456789 123456789 123456789' }
let(:another_invalid_number) { '441212' }
let(:normalizable_number) { '555 555 5555' }
let(:formatted_french_number_with_country_code) { '+33 627899541' }
let(:empty_number) { '' }
Expand All @@ -418,8 +419,11 @@ class NormalHome < ActiveRecord::Base
is_expected.to be_plausible_number valid_number, country_code: 'US'
end

it 'returns false for an invalid number' do
it 'returns false for an invalid numbers' do
is_expected.not_to be_plausible_number invalid_number
is_expected.not_to be_plausible_number invalid_number, country_code: 'US'
is_expected.not_to be_plausible_number another_invalid_number
is_expected.not_to be_plausible_number another_invalid_number, country_code: 'US'
end

it 'returns true for a normalizable number' do
Expand Down Expand Up @@ -451,6 +455,10 @@ class NormalHome < ActiveRecord::Base
is_expected.not_to be_plausible_number normalizable_number, country_code: 'US'
end

it 'should pass Github issue #95' do
is_expected.to be_plausible_number '+358414955444', default_country_code: :de
end

context 'with default_country_code set' do
before { PhonyRails.default_country_code = 'FR' }
after { PhonyRails.default_country_code = nil }
Expand Down Expand Up @@ -501,6 +509,12 @@ class NormalHome < ActiveRecord::Base
end
end

describe 'PhonyRails.country_code_from_number' do
it 'returns the code of the plausible phone number' do
expect(PhonyRails.country_code_from_number('+32475000000')).to eq '32'
end
end

describe 'PhonyRails.country_from_number' do
it 'returns the country of the plausible phone number' do
expect(PhonyRails.country_from_number('+32475000000')).to eq 'BE'
Expand Down

0 comments on commit 99f494d

Please sign in to comment.