Sha256: b4b7962dc1035efd506c32d1f87361d406829a289d269dcda0b0cb0d7b223a18

Contents?: true

Size: 896 Bytes

Versions: 1

Compression:

Stored size: 896 Bytes

Contents

module Citizenship
  def self.valid_phone!(number, opts = {})
    allow_country_prefix = opts.fetch(:allow_country_prefix, true)
    prefix_whitelist = network_prefix_white_list(opts[:only_prefixes])
    strict = opts.fetch(:strict, false)

    country_prefix = allow_country_prefix ? '((\+351|00351|351)?)' : ''
    regexp_template = "^#{country_prefix}(#{prefix_whitelist}\\d{7})$"

    phone_number = strict ? number : String(number).delete(' ').delete('-')
    raise PhoneError, :invalid_phone_number unless phone_number.match(Regexp.new(regexp_template))
    number
  end

  def self.valid_phone?(number, opts = {})
    valid_phone!(number, opts)
    true
  rescue Error
    false
  end

  private
  def self.network_prefix_white_list(prefixes)
    return '(2\d{1}|(9(3|6|2|1)))' if prefixes.nil?
    prefixes.is_a?(Array) ? "(#{prefixes.map(&:to_s).join('|')})" : "(#{prefixes})"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citizenship-1.0.0 lib/citizenship/phone.rb