class Commons::Formatter::E164Phone

Constants

MEXICO_CC

@type [String] Mexico Phone Country Code

MEXICO_ISO_CODE

@type [String] Mexico ISO Country Code

MEXICO_NDC

@type [String] Mexico National Destination Code

MEXICO_REGEX
NOT_DIGITS_REGEX

Attributes

valid[RW]

Public Class Methods

canonical_phone(phone_number) click to toggle source
# File lib/commons/formatter/e164_phone.rb, line 39
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
new(phone_number) click to toggle source
# File lib/commons/formatter/e164_phone.rb, line 18
def initialize(phone_number)
  @phone = Phonelib.parse(phone_number)
  @valid = @phone.valid?
end

Public Instance Methods

country_code() click to toggle source
# File lib/commons/formatter/e164_phone.rb, line 31
def country_code
  @valid ? @phone.country_code : nil
end
format() click to toggle source
# File lib/commons/formatter/e164_phone.rb, line 23
def format
  @valid ? @phone.e164 : nil
end
format_national() click to toggle source
# File lib/commons/formatter/e164_phone.rb, line 27
def format_national
  @valid ? @phone.raw_national : nil
end
validate() click to toggle source
# File lib/commons/formatter/e164_phone.rb, line 35
def validate
  @valid && @phone.country_code == MEXICO_CC
end