Class: EmailProcessor
- Inherits:
-
Object
- Object
- EmailProcessor
- Defined in:
- app/services/email_processor.rb
Instance Method Summary collapse
- #create_file_upload(attachment, inbox_item) ⇒ Object
-
#create_inbox_item ⇒ Object
end process.
- #directory ⇒ Object
- #formatted_body ⇒ Object
- #formatted_subject ⇒ Object
-
#initialize(email) ⇒ EmailProcessor
constructor
A new instance of EmailProcessor.
- #process ⇒ Object
- #store_file_locally(attachment) ⇒ Object
Constructor Details
#initialize(email) ⇒ EmailProcessor
3 4 5 6 |
# File 'app/services/email_processor.rb', line 3 def initialize(email) @email = email @user = User.find_by_email(@email.from[:email]) end |
Instance Method Details
#create_file_upload(attachment, inbox_item) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/email_processor.rb', line 45 def create_file_upload(, inbox_item) raise "No inbox item" unless inbox_item.present? path = store_file_locally() file_upload = FileUpload.new({ inbox_item_id: inbox_item.id, filename: .original_filename, mime: .content_type, file_type: .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_item ⇒ Object
end process
24 25 26 27 28 29 30 31 |
# File 'app/services/email_processor.rb', line 24 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 |
#directory ⇒ Object
41 42 43 |
# File 'app/services/email_processor.rb', line 41 def directory File.join(Rails.root, "public", "system", "email_attachments") end |
#formatted_body ⇒ Object
37 38 39 |
# File 'app/services/email_processor.rb', line 37 def formatted_body email.body.gsub(/Sent from my iPhone/, '') end |
#formatted_subject ⇒ Object
33 34 35 |
# File 'app/services/email_processor.rb', line 33 def formatted_subject @email.subject.gsub(/FWD:|Fwd:|Fw:|Re:|RE:/, '').strip end |
#process ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/services/email_processor.rb', line 8 def process inbox_item = create_inbox_item @email..each_with_index do |,i| begin file_upload = create_file_upload(, 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
65 66 67 68 69 70 71 72 |
# File 'app/services/email_processor.rb', line 65 def store_file_locally() name = .original_filename path = File.join(directory, name) File.open(path, "wb") { |f| f.write(.read) } return path rescue => e Rails.logger.info "Rescue at EmailProcessor store_file_locally line 57: #{e}" end |