class AlexanderGrahamBell::Phone
Constants
- AREA_CODE_REGEX
- EXTENSION_REGEX
- INTERNATIONAL_REGEX
modified from www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s02.html#I_programlisting4_d1e22044 ORIGINAL_REGEX = /^(+?1)?*(?([2-9]d))?*([2-9]d{2})[-.s]*(d{4})s*((x|ext)s*d+)?$/i
- LINE_NUMBER_REGEX
- PHONE_REGEX
- TELEPHONE_EXCHANGE_REGEX
Attributes
area_code[R]
extension[R]
international[R]
line_number[R]
telephone_exchange[R]
Public Class Methods
new(phone_number)
click to toggle source
# File lib/alexander_graham_bell/phone.rb, line 14 def initialize(phone_number) unless phone_number.instance_of?(String) raise TypeError, "Expected phone_number to be of type String but got #{phone_number.class.name}" end phone_number = clean(phone_number) @international, @area_code, @telephone_exchange, @line_number, @extension = PHONE_REGEX.match(phone_number).captures end
Public Instance Methods
number()
click to toggle source
# File lib/alexander_graham_bell/phone.rb, line 29 def number [@international, @area_code, @telephone_exchange, @line_number].reject(&:nil?).join end
number_with_extension(extension_separator = ' ext ')
click to toggle source
# File lib/alexander_graham_bell/phone.rb, line 33 def number_with_extension(extension_separator = ' ext ') extension = @extension.nil? ? '' : extension_separator + @extension number + extension end
valid?()
click to toggle source
# File lib/alexander_graham_bell/phone.rb, line 25 def valid? return [@telephone_exchange, @line_number].all? end
Private Instance Methods
clean(dirty_number)
click to toggle source
# File lib/alexander_graham_bell/phone.rb, line 40 def clean(dirty_number) dirty_number.gsub(/[-.()\s]/, '') end