2018-12-14 05:06:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-26 16:18:42 -05:00
|
|
|
module ActionMailbox
|
|
|
|
# Encapsulates the routes that live on the ApplicationMailbox and performs the actual routing when
|
|
|
|
# an inbound_email is received.
|
|
|
|
class Router
|
|
|
|
class RoutingError < StandardError; end
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@routes = []
|
|
|
|
end
|
2018-09-19 18:52:16 -04:00
|
|
|
|
2018-12-26 16:18:42 -05:00
|
|
|
def add_routes(routes)
|
|
|
|
routes.each do |(address, mailbox_name)|
|
|
|
|
add_route address, to: mailbox_name
|
|
|
|
end
|
2018-09-20 20:16:19 -04:00
|
|
|
end
|
|
|
|
|
2018-12-26 16:18:42 -05:00
|
|
|
def add_route(address, to:)
|
|
|
|
routes.append Route.new(address, to: to)
|
|
|
|
end
|
2018-09-17 20:49:47 -04:00
|
|
|
|
2018-12-26 16:18:42 -05:00
|
|
|
def route(inbound_email)
|
2019-05-04 13:53:08 -04:00
|
|
|
if mailbox = mailbox_for(inbound_email)
|
2018-12-26 16:18:42 -05:00
|
|
|
mailbox.receive(inbound_email)
|
|
|
|
else
|
|
|
|
inbound_email.bounced!
|
2018-09-27 19:44:41 -04:00
|
|
|
|
2018-12-26 16:18:42 -05:00
|
|
|
raise RoutingError
|
|
|
|
end
|
2018-09-20 20:16:19 -04:00
|
|
|
end
|
2018-09-17 20:49:47 -04:00
|
|
|
|
2019-05-04 13:53:08 -04:00
|
|
|
def mailbox_for(inbound_email)
|
2019-08-01 03:41:26 -04:00
|
|
|
routes.detect { |route| route.match?(inbound_email) }&.mailbox_class
|
2019-05-04 13:53:08 -04:00
|
|
|
end
|
|
|
|
|
2018-12-26 16:18:42 -05:00
|
|
|
private
|
|
|
|
attr_reader :routes
|
|
|
|
end
|
2018-09-17 20:49:47 -04:00
|
|
|
end
|
2018-09-20 20:16:19 -04:00
|
|
|
|
2018-09-28 15:19:43 -04:00
|
|
|
require "action_mailbox/router/route"
|