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

Extension option added to normalize_number #176

Merged
merged 5 commits into from
Mar 20, 2018
Merged
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
6 changes: 5 additions & 1 deletion lib/phony_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def self.country_codes_hash
# :country_code => The country code we should use.
# :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).
# :add_plus => Add a '+' in front so we know the country code is added. (default: true)
# :extension => Include the extension. (default: true)
# This idea came from:
# http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
def self.normalize_number(number, options = {})
Expand All @@ -64,7 +65,10 @@ def self.normalize_number(number, options = {})
normalized_number = Phony.normalize(number)
options[:add_plus] = true if options[:add_plus].nil? && Phony.plausible?(normalized_number)
normalized_number = options[:add_plus] ? "+#{normalized_number}" : normalized_number
format_extension(normalized_number, ext)

options[:extension] = true if options[:extension].nil?
normalized_number = options[:extension] ? format_extension(normalized_number, ext) : normalized_number
normalized_number
rescue StandardError
original_number # If all goes wrong .. we still return the original input.
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/phony_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ class NormalHome < ActiveRecord::Base
expect(PhonyRails.normalize_number("+32 3 226 94 97#{prefix}123", default_country_code: 'BE')).to eql('+3232269497 x123')
expect(PhonyRails.normalize_number("0450 764 000#{prefix}123", default_country_code: 'AU')).to eql('+61450764000 x123')
end

it "should remove #{prefix} extension (with extension: false)" do
expect(PhonyRails.normalize_number("0031-70-4157134#{prefix}123", extension: false, country_code: 'NL')).to eql('+31704157134')
expect(PhonyRails.normalize_number("+31-70-4157134#{prefix}123", extension: false, country_code: 'NL')).to eql('+31704157134')
expect(PhonyRails.normalize_number("0322-69497#{prefix}123", extension: false, country_code: 'BE')).to eql('+3232269497')
end
end
end

Expand Down