Sha256: fb1fcec0f07f65bf69da17475b963638c3f9122a29e22c9fadba655781c3b168

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module CRMFormatter
  class Phone

    ## Checks every phone number in table to verify that it meets phone criteria, then calls format_phone method to format Valid results.  Otherwise destroys Invalid phone fields and associations.

    # Call: Formatter.new.validate_phone(phone)
    def validate_phone(phone)
      phone_hsh = { phone: phone, valid_phone: nil, phone_edit: false }
      if phone.present?
        phone = phone&.gsub(/\s/, ' ')&.strip
        reg = Regexp.new("[(]?[0-9]{3}[ ]?[)-.]?[ ]?[0-9]{3}[ ]?[-. ][ ]?[0-9]{4}")
        return phone_hsh if phone.first == "0" || phone.include?("(0") || !reg.match(phone)
        phone_hsh[:valid_phone] = format_phone(phone)
        phone_hsh[:phone_edit] = phone_hsh[:phone] != phone_hsh[:valid_phone]
      end
      phone_hsh
    end

    #################################
    ## FORMATS PHONE AS: (000) 000-0000
    ## Assumes phone is legitimate, then formats.  Not designed to detect Valid phone number.

    # Call: Formatter.new.format_phone(phone)
    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_stripped && phone_stripped[0] == "1") ? phone_step2 = phone_stripped[1..-1] : phone_step2 = 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
      return final_phone
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
crm_formatter-1.0.7.pre.rc.1 lib/crm_formatter/phone.rb
crm_formatter-1.0.6.pre.rc.1 lib/crm_formatter/phone.rb
crm_formatter-1.0.5.pre.rc.1 lib/crm_formatter/phone.rb
crm_formatter-1.0.4.pre.rc.1 lib/crm_formatter/phone.rb