Class: Textris::PhoneFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/textris/phone_formatter.rb

Class Method Summary collapse

Class Method Details

.format(phone = '') ⇒ Object



4
5
6
7
# File 'lib/textris/phone_formatter.rb', line 4

def format(phone = '')
  return phone if is_a_short_code?(phone) || is_alphameric?(phone) || phone.nil?
  "#{'+' unless phone.start_with?('+')}#{phone}"
end

.is_a_phone_number?(phone) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/textris/phone_formatter.rb', line 15

def is_a_phone_number?(phone)
  Phony.plausible?(phone)
end

.is_a_short_code?(phone) ⇒ Boolean

Short codes have more dependencies and limitations; but this is a good general start

Returns:

  • (Boolean)


11
12
13
# File 'lib/textris/phone_formatter.rb', line 11

def is_a_short_code?(phone)
  !!phone.to_s.match(/\A\d{4,6}\z/)
end

.is_alphameric?(phone) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/textris/phone_formatter.rb', line 19

def is_alphameric?(phone)
  # \A               # Start of the string
  # (?=.*[a-zA-Z])   # Lookahead to ensure there is at least one letter in the entire string
  # [a-zA-z\d]{1,11} # Between 1 and 11 characters in the string
  # \z               # End of the string
  !!phone.to_s.match(/\A(?=.*[a-zA-Z])[a-zA-z\d]{1,11}\z/)
end