Sha256: 38da1419f3ecd7164372e927f47090fde6c567ed91130b692a6066fa6c9a7f6a

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

class String
  # Usage:
  #   "+31 (0)30 1234 123".phony_normalized # => '+31301234123'
  #   "(0)30 1234 123".phony_normalized # => '301234123'
  #   "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
  def phony_normalized(options = {})
    raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)
    options = options.dup
    PhonyRails.normalize_number(self, options)
  end

  # 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)
  # When an error occurs during conversion it will return the original String.
  # To raise an error use:
  #   "somestring".phone_formatted(raise: true)
  def phony_formatted(options = {})
    raise ArgumentError, "Expected options to be a Hash, got #{options.inspect}" unless options.is_a?(Hash)
    options = options.dup
    normalize_country_code = options.delete(:normalize)
    s, ext = PhonyRails.extract_extension(self)
    s = (normalize_country_code ? PhonyRails.normalize_number(s, default_country_code: normalize_country_code.to_s, add_plus: false) : s.gsub(/\D/, ''))
    return if s.blank?
    return if options[:strict] && !Phony.plausible?(s)
    PhonyRails.format_extension(Phony.format(s, options.reverse_merge(format: :national)), ext)
  rescue StandardError
    raise if options[:raise]
    s
  end

  # The bang method
  def phony_formatted!(options = {})
    raise ArgumentError, 'The :strict options is only supported in the phony_formatted (non bang) method.' if options[:strict]
    replace(phony_formatted(options))
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phony_rails-0.14.11 lib/phony_rails/string_extensions.rb
phony_rails-0.14.10 lib/phony_rails/string_extensions.rb
phony_rails-0.14.9 lib/phony_rails/string_extensions.rb
phony_rails-0.14.7 lib/phony_rails/string_extensions.rb