class ContactParser

Constants

VERSION

Public Class Methods

new(str) click to toggle source
# File lib/contact_parser.rb, line 5
def initialize(str)
  @str = str
end

Public Instance Methods

bulk_parse() click to toggle source
# File lib/contact_parser.rb, line 9
def bulk_parse
  split_records
  names_and_emails = split_names_and_email(split_records)
  contact_list_cleaned_for_hash = remove_angle_bracket_from_emails(names_and_emails)
  convert_contacts_to_hash(contact_list_cleaned_for_hash)
end
convert_contacts_to_hash(name_and_email_array) click to toggle source
# File lib/contact_parser.rb, line 29
def convert_contacts_to_hash(name_and_email_array)
  name_and_email_array.each_with_object(final_hash) do |e, hash|
    first_name = e.first
    last_name = e[-2]
    email = e.last

    if valid_contact?(first_name, last_name, email)
      contact_hash = Hash.new
      contact_hash[:first_name] = first_name
      contact_hash[:last_name] = last_name
      contact_hash[:email] = email
      hash[:accepted].push(contact_hash)
    else
      hash[:rejected] << e.join(' ')
    end
  end
end
final_hash() click to toggle source
# File lib/contact_parser.rb, line 47
def final_hash
  {
    accepted: [],
    rejected: []
  }
end
remove_angle_bracket_from_emails(name_and_email_array) click to toggle source
# File lib/contact_parser.rb, line 24
def remove_angle_bracket_from_emails(name_and_email_array)
  name_and_email_array.map { |e| e.last.gsub!(/<|>/, '') }
  name_and_email_array
end
split_names_and_email(split_records_array) click to toggle source
# File lib/contact_parser.rb, line 20
def split_names_and_email(split_records_array)
  split_records_array.map { |e| e.split(' ') }
end
split_records() click to toggle source
# File lib/contact_parser.rb, line 16
def split_records
  @str.split(',')
end
valid_contact?(first_name, last_name, email) click to toggle source
# File lib/contact_parser.rb, line 54
def valid_contact?(first_name, last_name, email)
  if !EmailAddress.valid?(email)
    return false
  elsif first_name.nil?
    return false
  elsif last_name.nil?
    return false
  else
    true
  end
end