diff options
author | George Claghorn <george@basecamp.com> | 2018-12-24 15:16:22 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-12-25 21:32:35 -0500 |
commit | a5b2fff64ca0c1fa7be5124f40a251d991c10a85 (patch) | |
tree | 33a79841402b7151e52d9ad3949ce54f320c10aa /actionmailbox/lib/action_mailbox/router.rb | |
parent | 4298df00ae6219b9b5b7c40f281d4fa4d66f4383 (diff) | |
parent | dcddff1d2d0c695318670686a27429a76f20ae03 (diff) | |
download | rails-a5b2fff64ca0c1fa7be5124f40a251d991c10a85.tar.gz rails-a5b2fff64ca0c1fa7be5124f40a251d991c10a85.tar.bz2 rails-a5b2fff64ca0c1fa7be5124f40a251d991c10a85.zip |
Import Action Mailbox
Diffstat (limited to 'actionmailbox/lib/action_mailbox/router.rb')
-rw-r--r-- | actionmailbox/lib/action_mailbox/router.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/actionmailbox/lib/action_mailbox/router.rb b/actionmailbox/lib/action_mailbox/router.rb new file mode 100644 index 0000000000..0f041a8389 --- /dev/null +++ b/actionmailbox/lib/action_mailbox/router.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +# Encapsulates the routes that live on the ApplicationMailbox and performs the actual routing when +# an inbound_email is received. +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" |