lib/phony_rails.rb in phony_rails-0.14.9 vs lib/phony_rails.rb in phony_rails-0.14.10

- old
+ new

@@ -74,11 +74,11 @@ 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 # correct phone number. See https://github.com/joost/phony_rails/issues/87#issuecomment-89324426 - unless number =~ /\A\(?\+/ # if we don't have a + + unless number =~ /\A\(?(\+|00)/ # if we don't have a + or 00 return "#{default_country_number}#{number}" if Phony.plausible?("#{default_country_number}#{number}") || !Phony.plausible?(number) || country_code_from_number(number).nil? # If the number starts with ONE zero (two might indicate a country code) # and this is a plausible number for the default_country # we prefer that one. return "#{default_country_number}#{number.gsub(/^0/, '')}" if (number =~ /^0[^0]/) && Phony.plausible?("#{default_country_number}#{number.gsub(/^0/, '')}") @@ -95,26 +95,31 @@ options[:default_country_code] end 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 Phony.plausible? number, cc: country_number rescue StandardError false