mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
9f96d094a3
Currently, the only exposed entry point into the ApplicationMailbox's configured routing system is to call `route`, which performs a lot of work to fully `process` inbound email. It'd be nice to have a way (e.g. in test) of checking which mailbox an email would route to without necessarily processing it yet.
42 lines
933 B
Ruby
42 lines
933 B
Ruby
# frozen_string_literal: true
|
|
|
|
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
|
|
|
|
def add_routes(routes)
|
|
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)
|
|
end
|
|
|
|
def route(inbound_email)
|
|
if mailbox = mailbox_for(inbound_email)
|
|
mailbox.receive(inbound_email)
|
|
else
|
|
inbound_email.bounced!
|
|
|
|
raise RoutingError
|
|
end
|
|
end
|
|
|
|
def mailbox_for(inbound_email)
|
|
routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
|
|
end
|
|
|
|
private
|
|
attr_reader :routes
|
|
end
|
|
end
|
|
|
|
require "action_mailbox/router/route"
|