1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/action_mailbox/base.rb

58 lines
1.2 KiB
Ruby
Raw Normal View History

require "active_support/rescuable"
require "action_mailbox/callbacks"
require "action_mailbox/routing"
class ActionMailbox::Base
include ActiveSupport::Rescuable
include ActionMailbox::Callbacks, ActionMailbox::Routing
attr_reader :inbound_email
delegate :mail, :delivered!, :bounced!, to: :inbound_email
2018-09-17 20:49:47 -04:00
2018-10-01 08:16:10 -04:00
delegate :logger, to: ActionMailbox
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
def perform_processing
run_callbacks :process do
track_status_of_inbound_email do
process
end
2018-09-18 19:42:38 -04:00
end
rescue => exception
# TODO: Include a reference to the inbound_email in the exception raised so error handling becomes easier
rescue_with_handler(exception) || raise
end
2018-09-17 20:49:47 -04:00
def process
# Overwrite in subclasses
2018-09-17 20:49:47 -04:00
end
2018-10-01 08:16:10 -04:00
def finished_processing?
inbound_email.delivered? || inbound_email.bounced?
end
2018-10-03 15:14:28 -04:00
def bounce_with(message)
inbound_email.bounced!
message.deliver_later
end
private
def track_status_of_inbound_email
inbound_email.processing!
yield
inbound_email.delivered! unless inbound_email.bounced?
2018-10-17 12:42:54 -04:00
rescue
inbound_email.failed!
raise
end
end