aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_mailroom/router.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2018-09-20 17:16:19 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2018-09-20 17:16:19 -0700
commitd14f54b0e085128b5305c806a4fe01f07b97b8fa (patch)
treeb7b9c0772ad800f9074f7440aee5d9c00dc4431e /lib/action_mailroom/router.rb
parent2d6c79413d01f48df11ef76e65ba45673dcf580c (diff)
downloadrails-d14f54b0e085128b5305c806a4fe01f07b97b8fa.tar.gz
rails-d14f54b0e085128b5305c806a4fe01f07b97b8fa.tar.bz2
rails-d14f54b0e085128b5305c806a4fe01f07b97b8fa.zip
Expand router with real routing object and 4-way address options
Diffstat (limited to 'lib/action_mailroom/router.rb')
-rw-r--r--lib/action_mailroom/router.rb22
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/action_mailroom/router.rb b/lib/action_mailroom/router.rb
index bf0001f1ae..c3fa183438 100644
--- a/lib/action_mailroom/router.rb
+++ b/lib/action_mailroom/router.rb
@@ -1,20 +1,34 @@
class ActionMailroom::Router
+ class RoutingError < StandardError; end
+
def initialize
- @routes = {}
+ @routes = []
end
def add_routes(routes)
- @routes.merge!(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)
- locate_mailbox(inbound_email).receive(inbound_email)
+ if mailbox = locate_mailbox(inbound_email)
+ mailbox.receive(inbound_email)
+ else
+ raise RoutingError
+ end
end
private
attr_reader :routes
def locate_mailbox(inbound_email)
- "#{routes[inbound_email.mail.to.first].to_s.capitalize}Mailbox".constantize
+ routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
end
end
+
+require "action_mailroom/router/route"