Class: EmailProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/services/email_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(email) ⇒ EmailProcessor

Returns a new instance of EmailProcessor.



4
5
6
7
8
# File 'app/services/email_processor.rb', line 4

def initialize(email)
  @email = email
  email_address = @email.from[:email]
  @user = User.get_user_by_email_or_alias(email_address)  
end

Instance Method Details

#create_file_upload(attachment, inbox_item) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/email_processor.rb', line 53

def create_file_upload(attachment, inbox_item)
  raise "No inbox item" unless inbox_item.present?

  path = store_file_locally(attachment)
  
  file_upload = FileUpload.new({
    inbox_item_id: inbox_item.id, 
    filename: attachment.original_filename, 
    mime: attachment.content_type, 
    file_type: attachment.content_type.split('/')[0],
    local_file: path,
    path: path
  })
  file_upload.save
  return file_upload
rescue => e
  Rails.logger.info "Rescue at Email Processor create_file_upload line 48: #{e}"
  return false
end

#create_inbox_itemObject

end process



27
28
29
30
31
32
33
34
# File 'app/services/email_processor.rb', line 27

def create_inbox_item
  inbox_item = @user.inbox_items.new(subject: formatted_subject, body: @email.raw_body)
  inbox_item.save!
  return inbox_item
rescue => e
  Rails.logger.info "Rescue at Email Processor create_inbox_item, line 28: #{e}"
  return nil
end

#directoryObject



49
50
51
# File 'app/services/email_processor.rb', line 49

def directory
  File.join(Rails.root, "public", "system", "email_attachments")
end

#formatted_bodyObject



40
41
42
# File 'app/services/email_processor.rb', line 40

def formatted_body
  transcoded email.body.gsub(/Sent from my iPhone/, '')
end

#formatted_subjectObject



36
37
38
# File 'app/services/email_processor.rb', line 36

def formatted_subject
  transcoded @email.subject.gsub(/FWD:|Fwd:|Fw:|Re:|RE:/, '').strip
end

#processObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/email_processor.rb', line 10

def process

  inbox_item = create_inbox_item
      
  @email.attachments.each_with_index do |attachment,i|
    begin
      file_upload = create_file_upload(attachment, inbox_item)
      InboxItems::MoveEmailAttachmentToS3Job.perform_later(file_upload) if file_upload
    rescue => e
      
      Rails.logger.info "Rescue from error at EmailProcessor attachments each with index line 17: #{e}"
      next  
    end
  end # end each attachment

end

#store_file_locally(attachment) ⇒ Object



73
74
75
76
77
78
79
80
# File 'app/services/email_processor.rb', line 73

def store_file_locally(attachment)
  name = attachment.original_filename
  path = File.join(directory, name)
  File.open(path, "wb") { |f| f.write(attachment.read) }
  return path
rescue => e
  Rails.logger.info "Rescue at EmailProcessor store_file_locally line 57: #{e}"
end

#transcoded(content) ⇒ Object



44
45
46
47
# File 'app/services/email_processor.rb', line 44

def transcoded(content)
  detection = CharlockHolmes::EncodingDetector.detect(content)
  CharlockHolmes::Converter.convert content, detection[:encoding], 'UTF-8'
end