2018-09-18 19:26:30 -04:00
|
|
|
require "active_support/rescuable"
|
2018-09-19 18:52:16 -04:00
|
|
|
|
2018-09-18 19:42:38 -04:00
|
|
|
require "action_mailroom/mailbox/callbacks"
|
2018-09-19 18:52:16 -04:00
|
|
|
require "action_mailroom/mailbox/routing"
|
2018-09-18 19:26:30 -04:00
|
|
|
|
2018-09-17 20:01:52 -04:00
|
|
|
class ActionMailroom::Mailbox
|
2018-09-19 18:52:16 -04:00
|
|
|
include ActiveSupport::Rescuable
|
|
|
|
include Callbacks, Routing
|
2018-09-18 19:26:30 -04:00
|
|
|
|
2018-09-19 18:52:16 -04:00
|
|
|
attr_reader :inbound_email
|
|
|
|
delegate :mail, to: :inbound_email
|
2018-09-17 20:49:47 -04:00
|
|
|
|
2018-09-19 18:52:16 -04:00
|
|
|
def self.receive(inbound_email)
|
|
|
|
new(inbound_email).perform_processing
|
2018-09-17 20:49:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(inbound_email)
|
|
|
|
@inbound_email = inbound_email
|
|
|
|
end
|
|
|
|
|
2018-09-18 19:31:16 -04:00
|
|
|
def perform_processing
|
2018-09-19 19:56:07 -04:00
|
|
|
run_callbacks :process do
|
|
|
|
track_status_of_inbound_email do
|
2018-09-19 19:54:49 -04:00
|
|
|
process
|
|
|
|
end
|
2018-09-18 19:42:38 -04:00
|
|
|
end
|
2018-09-18 19:26:30 -04:00
|
|
|
rescue => exception
|
2018-09-18 20:13:49 -04:00
|
|
|
# TODO: Include a reference to the inbound_email in the exception raised so error handling becomes easier
|
2018-09-18 19:26:30 -04:00
|
|
|
rescue_with_handler(exception) || raise
|
|
|
|
end
|
|
|
|
|
2018-09-17 20:49:47 -04:00
|
|
|
def process
|
2018-09-18 19:26:30 -04:00
|
|
|
# Overwrite in subclasses
|
2018-09-17 20:49:47 -04:00
|
|
|
end
|
2018-09-19 19:54:49 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def track_status_of_inbound_email
|
|
|
|
inbound_email.processing!
|
|
|
|
yield
|
|
|
|
inbound_email.delivered!
|
|
|
|
rescue => exception
|
|
|
|
inbound_email.failed!
|
|
|
|
raise
|
|
|
|
end
|
2018-09-17 20:01:52 -04:00
|
|
|
end
|