diff options
Diffstat (limited to 'lib/action_mailbox/router.rb')
-rw-r--r-- | lib/action_mailbox/router.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/action_mailbox/router.rb b/lib/action_mailbox/router.rb new file mode 100644 index 0000000000..8ba3ad0bae --- /dev/null +++ b/lib/action_mailbox/router.rb @@ -0,0 +1,36 @@ +class ActionMailbox::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 = match_to_mailbox(inbound_email) + mailbox.receive(inbound_email) + else + inbound_email.bounced! + + raise RoutingError + end + end + + private + attr_reader :routes + + def match_to_mailbox(inbound_email) + routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class) + end +end + +require "action_mailbox/router/route" |