From 8a0a1034955544ee2e4c1f85317c0db84f3aa55b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 28 Sep 2018 12:19:43 -0700 Subject: ActionMailroom -> ActionMailbox We didn't end up using the mailroom metaphor directly, so let's stick with a more conventional naming strategy. --- lib/action_mailbox/router.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/action_mailbox/router.rb (limited to 'lib/action_mailbox/router.rb') 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" -- cgit v1.2.3