2018-09-28 15:19:43 -04:00
|
|
|
class ActionMailbox::Router
|
2018-09-20 20:16:19 -04:00
|
|
|
class RoutingError < StandardError; end
|
|
|
|
|
2018-09-19 18:52:16 -04:00
|
|
|
def initialize
|
2018-09-20 20:16:19 -04:00
|
|
|
@routes = []
|
2018-09-19 18:52:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_routes(routes)
|
2018-09-20 20:16:19 -04:00
|
|
|
routes.each do |(address, mailbox_name)|
|
|
|
|
add_route address, to: mailbox_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_route(address, to:)
|
|
|
|
routes.append Route.new(address, to: to)
|
2018-09-17 20:49:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def route(inbound_email)
|
2018-09-20 20:27:41 -04:00
|
|
|
if mailbox = match_to_mailbox(inbound_email)
|
2018-09-20 20:16:19 -04:00
|
|
|
mailbox.receive(inbound_email)
|
|
|
|
else
|
2018-09-27 19:44:41 -04:00
|
|
|
inbound_email.bounced!
|
|
|
|
|
2018-09-20 20:16:19 -04:00
|
|
|
raise RoutingError
|
|
|
|
end
|
2018-09-17 20:49:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
attr_reader :routes
|
|
|
|
|
2018-09-20 20:27:41 -04:00
|
|
|
def match_to_mailbox(inbound_email)
|
2018-09-20 20:16:19 -04:00
|
|
|
routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
|
2018-09-17 20:49:47 -04:00
|
|
|
end
|
|
|
|
end
|
2018-09-20 20:16:19 -04:00
|
|
|
|
2018-09-28 15:19:43 -04:00
|
|
|
require "action_mailbox/router/route"
|