Sha256: dbc0fd0de420de6aa7fdb598b039fa243b0c4ef8aea341b3331c6732a601a99a

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

require "mail"

class Ppl::Adapter::EmailScraper::Mail

  attr_writer :storage_adapter

  def scrape_contacts(email)
    email = Mail.new(email)
    contacts = []

    sender = scrape_sender(email)
    unless sender.nil?
      contacts << sender
    end

    contacts
  end

  private

  def scrape_sender(email)
    from = email[:from]
    unless from.nil?
      sender = Ppl::Entity::Contact.new
      sender.name = Ppl::Entity::Name.new
      display_name = from.tree.addresses.first.display_name
      sender.name.full = Mail::Encodings.value_decode(display_name) unless display_name.nil?
      sender.email_addresses << Ppl::Entity::EmailAddress.new(from.tree.addresses.first.address)
      sender.id = generate_contact_id(sender)
      sender.name.full = sender.id if display_name.nil?
      sender
    end
  end

  def generate_contact_id(contact)
    if !contact.name.full.nil?
      generate_contact_id_from_name(contact.name)
    elsif !contact.email_addresses.empty?
      contact.email_addresses.first.address
    end
  end

  def generate_contact_id_from_name(name)
    name = name.full.downcase.tr(" ", "_")
    base = name
    suffix = 0
    until @storage_adapter.load_contact(name).nil?
      suffix += 1
      name = "#{base}_#{suffix}"
    end
    name
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ppl-3.0.1 lib/ppl/adapter/email_scraper/mail.rb
ppl-3.0.0 lib/ppl/adapter/email_scraper/mail.rb
ppl-2.4.1 lib/ppl/adapter/email_scraper/mail.rb
ppl-2.4.0 lib/ppl/adapter/email_scraper/mail.rb
ppl-2.3.3 lib/ppl/adapter/email_scraper/mail.rb