lib/phony_rails.rb in phony_rails-0.14.6 vs lib/phony_rails.rb in phony_rails-0.14.7

- old
+ new

@@ -1,6 +1,7 @@ # frozen_string_literal: true + require 'phony' require 'phony_rails/string_extensions' require 'validators/phony_validator' require 'phony_rails/version' require 'yaml' @@ -38,10 +39,11 @@ # :country_number => The country dial code (eg. 31 for NL). # :default_country_number => Fallback country code. # :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 = {}) return if number.nil? original_number = number @@ -61,12 +63,15 @@ number = normalize_number_default_country(number, _default_country_number) end 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) - rescue + + 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 def self.normalize_number_default_country(number, default_country_number) # We try to add the default country number and see if it is a @@ -90,21 +95,26 @@ def self.country_code_from_number(number) return nil unless Phony.plausible?(number) Phony.split(Phony.normalize(number)).first end + 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. 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]) || options[:default_country_number] || country_number_for(options[:default_country_code]) || default_country_number Phony.plausible? number, cc: country_number - rescue + rescue StandardError false end COMMON_EXTENSIONS = /[ ]*(ext|ex|x|xt|#|:)+[^0-9]*\(?([-0-9]{1,})\)?#?$/i @@ -140,10 +150,10 @@ send("#{attribute_name}=", new_value) if new_value || attribute_name != attribute end end def assign_values_for_phony_symbol_options(options) - symbol_options = [:country_number, :default_country_number, :country_code, :default_country_code] + symbol_options = %i[country_number default_country_number country_code default_country_code] symbol_options.each do |option| options[option] = send(options[option]) if options[option].is_a?(Symbol) end end end