lib/phony_rails.rb in phony_rails-0.12.10 vs lib/phony_rails.rb in phony_rails-0.12.11

- old
+ new

@@ -25,10 +25,11 @@ # :add_plus => Add a '+' in front so we know the country code is added. (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 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 = options[:country_number] || country_number_for(options[:country_code]) options[:add_plus] = true if options[:add_plus].nil? @@ -42,18 +43,23 @@ # 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 if not (number =~ /\A\+/) # if we don't have a + if Phony.plausible?("#{_default_country_number}#{number}") || !Phony.plausible?(number) || country_code_from_number(number).nil? number = "#{_default_country_number}#{number}" + elsif (number =~ /^0[^0]/) && Phony.plausible?("#{_default_country_number}#{number.gsub(/^0/, '')}") + # 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. + number = "#{_default_country_number}#{number.gsub(/^0/, '')}" end end # number = "#{_default_country_number}#{number}" unless Phony.plausible?(number) end normalized_number = Phony.normalize(number) options[:add_plus] = true if options[:add_plus].nil? && Phony.plausible?(normalized_number) options[:add_plus] ? "+#{normalized_number}" : normalized_number rescue - number # If all goes wrong .. we still return the original input. + original_number # If all goes wrong .. we still return the original input. end def self.country_code_from_number(number) return nil unless Phony.plausible?(number) Phony.split(Phony.normalize(number)).first