Method: Client#process_email

Defined in:
lib/client.rb

#process_email(mail, uid) ⇒ Object

Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service. Meta will content information like: [‘from’, ‘to’, ‘cc’, …, ‘body_url’]. This information is then used by navi-ai to parse the body content.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/client.rb', line 122

def process_email(mail, uid)
  meta = Hash.new
  custom_uid = (Time.now.to_f * 1000).to_s + "_" + mail.__id__.to_s

  invalid_to_email = mail.to.nil? || !mail.to.is_a?(Array)

  invalid_cc_email = mail.cc.nil? || !mail.cc.is_a?(Array)

  unless mail.from.nil?
    if defined? mail.from.first
      meta["from"] = mail.from.first
    else
      meta["from"] = mail.from
    end
  end
  meta["to"] = invalid_to_email ? mail.to : mail.to.join(";")
  meta["cc"] = mail.cc.join(";") unless invalid_cc_email
  meta["subject"] = mail.subject
  meta["date"] = mail.date.to_s

  if mail.multipart?
    for i in 0...mail.parts.length
      m = download(mail.parts[i], custom_uid)
      meta.merge!(m) unless m.nil?
    end
  else
    m = download(mail, custom_uid)
    meta.merge!(m) unless m.nil?
  end

  save(meta, "meta/#{uid.to_s + '_' + custom_uid}")
end