Sha256: 26e5740a528cc30ea29101f6f72f87a4a603f3a6a9ffa10f08bdaa8f52b763ce

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

module Commons
  module Formatter
    class E164Phone
      attr_accessor :valid

      # @type [String]
      # Mexico ISO Country Code
      MEXICO_ISO_CODE = 'MX'.freeze
      # @type [String]
      # Mexico Phone Country Code
      MEXICO_CC = '52'.freeze
      # @type [String]
      # Mexico National Destination Code
      MEXICO_NDC = ''.freeze
      MEXICO_REGEX = /^(521?\d{10}|\d{10})$/
      NOT_DIGITS_REGEX = /[^0-9]/

      def initialize(phone_number)
        @phone = Phonelib.parse(phone_number)
        @valid = @phone.valid?
      end

      def format
        @valid ? @phone.e164 : nil
      end

      def format_national
        @valid ? @phone.raw_national : nil
      end

      def country_code
        @valid ? @phone.country_code : nil
      end

      def validate
        @valid && @phone.country_code == MEXICO_CC
      end

      def self.canonical_phone(phone_number)
        phone_digits = phone_number.gsub(NOT_DIGITS_REGEX, '')
        return nil unless MEXICO_REGEX.match(phone_digits)

        phone_digits.split(//).last(10).join
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
commons_yellowme-0.16.0 lib/commons/formatter/e164_phone.rb
commons_yellowme-0.15.0 lib/commons/formatter/e164_phone.rb
commons_yellowme-0.12.0 lib/commons/formatter/e164_phone.rb
commons_yellowme-0.11.3 lib/commons/formatter/e164_phone.rb
commons_yellowme-0.11.2 lib/commons/formatter/e164_phone.rb
commons_yellowme-0.11.1 lib/commons/formatter/e164_phone.rb
commons_yellowme-0.11.0 lib/commons/formatter/e164_phone.rb