Method: String#phony_formatted
- Defined in:
- lib/phony_rails/string_extensions.rb
#phony_formatted(options = {}) ⇒ Object
Add a method to the String class so we can easily format phone numbers. This enables:
"31612341234".phony_formatted # => '06 12341234'
"31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
To first normalize a String use:
"010-12341234".phony_formatted(:normalize => :NL)
To return nil when a number is not correct (checked using Phony.plausible?) use
"010-12341234".phony_formatted(strict: true)
11 12 13 14 15 16 17 |
# File 'lib/phony_rails/string_extensions.rb', line 11 def phony_formatted( = {}) normalize_country_code = .delete(:normalize) s = (normalize_country_code ? PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s) : self.gsub(/\D/, '')) return if s.blank? return if [:strict] && !Phony.plausible?(s) Phony.format(s, .reverse_merge(:format => :national)) end |