Class: CrmFormatter::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/crm_formatter/phone.rb

Instance Method Summary collapse

Instance Method Details

#check_phone_status(hsh) ⇒ Object

COMPARE ORIGINAL AND FORMATTED PHONE ######



20
21
22
23
24
25
26
27
# File 'lib/crm_formatter/phone.rb', line 20

def check_phone_status(hsh)
  phone = hsh[:phone]
  phone_f = hsh[:phone_f]
  status = 'invalid'
  status = phone != phone_f ? 'formatted' : 'unchanged' if phone && phone_f
  hsh[:phone_status] = status if status.present?
  hsh
end

#format_phone(phone) ⇒ Object

Call: Wrap.new.format_phone(phone)



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/crm_formatter/phone.rb', line 34

def format_phone(phone)
  regex = Regexp.new('[A-Z]+[a-z]+')
  if !phone.blank? && (phone != 'N/A' || phone != '0') && !regex.match(phone)
    phone_stripped = phone.gsub(/[^0-9]/, '')
    phone_step2 = phone_stripped && phone_stripped[0] == '1' ? phone_stripped[1..-1] : phone_stripped
    final_phone = !(phone_step2 && phone_step2.length < 10) ? "(#{phone_step2[0..2]}) #{(phone_step2[3..5])}-#{(phone_step2[6..9])}" : phone
  else
    final_phone = nil
  end
  final_phone
end

#validate_phone(phone) ⇒ Object

Call: Wrap.new.validate_phone(phone)



8
9
10
11
12
13
14
15
16
17
# File 'lib/crm_formatter/phone.rb', line 8

def validate_phone(phone)
  phone_hsh = { phone_status: nil, phone: phone, phone_f: nil }
  return phone_hsh unless phone.present?
  phone = phone&.gsub(/\s/, ' ')&.strip
  reg = Regexp.new('[(]?[0-9]{3}[ ]?[)-.]?[ ]?[0-9]{3}[ ]?[-. ][ ]?[0-9]{4}')
  phone = nil if phone.first == '0' || phone.include?('(0') || !reg.match(phone)
  phone_hsh[:phone_f] = format_phone(phone) if phone.present?
  phone_hsh = check_phone_status(phone_hsh)
  phone_hsh
end