Module: PhonyRails
- Defined in:
- lib/phony_rails.rb,
lib/phony_rails/version.rb
Defined Under Namespace
Modules: Extension
Constant Summary collapse
- VERSION =
"0.3.2"
Class Method Summary collapse
- .country_number_for(country_code) ⇒ Object
-
.normalize_number(number, options = {}) ⇒ Object
This method requires a country_code attribute (eg. NL) and phone_number to be set.
Class Method Details
.country_number_for(country_code) ⇒ Object
9 10 11 |
# File 'lib/phony_rails.rb', line 9 def self.country_number_for(country_code) ISO3166::Country::Data[country_code].try(:[], 'country_code') end |
.normalize_number(number, options = {}) ⇒ Object
This method requires a country_code attribute (eg. NL) and phone_number to be set. Options:
: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).
This idea came from:
http://www.redguava.com.au/2011/06/rails-convert-phone-numbers-to-international-format-for-sms/
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/phony_rails.rb', line 21 def self.normalize_number(number, = {}) return if number.nil? 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 = [:country_number] || country_number_for([:country_code]) # (Force) add country_number if missing number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/ elsif default_country_number = [:default_country_number] || country_number_for([:default_country_code]) # Add default_country_number if missing number = "#{default_country_number}#{number}" if not number =~ /^(00|\+|#{default_country_number})/ end number = Phony.normalize(number) if Phony.plausible?(number) return number.to_s rescue number # If all goes wrong .. we still return the original input. end |