aboutsummaryrefslogtreecommitdiffstats
path: root/lib
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
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')
-rw-r--r--lib/action_mailroom/router.rb22
-rw-r--r--lib/action_mailroom/router/route.rb26
-rw-r--r--lib/action_mailroom/test_helper.rb2
3 files changed, 46 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"
diff --git a/lib/action_mailroom/router/route.rb b/lib/action_mailroom/router/route.rb
new file mode 100644
index 0000000000..316c77d711
--- /dev/null
+++ b/lib/action_mailroom/router/route.rb
@@ -0,0 +1,26 @@
+class ActionMailroom::Router::Route
+ class InvalidAddressError < StandardError; end
+
+ attr_reader :address, :mailbox_name
+
+ def initialize(address, to:)
+ @address, @mailbox_name = address, to
+ end
+
+ def match?(inbound_email)
+ case address
+ when String
+ inbound_email.mail.to.include?(address)
+ when Regexp
+ inbound_email.mail.to.detect { |recipient| address.match?(recipient) }
+ when Proc
+ address.call(inbound_email)
+ else
+ address.try(:match?, inbound_email) || raise(InvalidAddressError)
+ end
+ end
+
+ def mailbox_class
+ "#{mailbox_name.to_s.capitalize}Mailbox".constantize
+ end
+end
diff --git a/lib/action_mailroom/test_helper.rb b/lib/action_mailroom/test_helper.rb
index fbfb2797d9..33d32ec899 100644
--- a/lib/action_mailroom/test_helper.rb
+++ b/lib/action_mailroom/test_helper.rb
@@ -1,3 +1,5 @@
+require "mail"
+
module ActionMailroom
module TestHelper
# Create an InboundEmail record using an eml fixture in the format of message/rfc822