diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-19 15:52:16 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-19 15:52:16 -0700 |
commit | da697e84445f8f750b8a7d0fa916f429eda8aae4 (patch) | |
tree | 510ad1391fedcdc7a3433ef4af128007fa7fbfa0 | |
parent | ee6d9545dd69a96ae30fdacf264db83fbaa1e4dd (diff) | |
download | rails-da697e84445f8f750b8a7d0fa916f429eda8aae4.tar.gz rails-da697e84445f8f750b8a7d0fa916f429eda8aae4.tar.bz2 rails-da697e84445f8f750b8a7d0fa916f429eda8aae4.zip |
Attach a concrete router to the root mailbox and use it
Don't think this is how it's going to stay. Doesn't feel like the right place for it.
-rw-r--r-- | app/jobs/action_mailroom/deliver_inbound_email_to_mailroom_job.rb | 2 | ||||
-rw-r--r-- | lib/action_mailroom/mailbox.rb | 18 | ||||
-rw-r--r-- | lib/action_mailroom/mailbox/routing.rb | 15 | ||||
-rw-r--r-- | lib/action_mailroom/router.rb | 8 | ||||
-rw-r--r-- | test/unit/mailbox/routing_test.rb | 23 | ||||
-rw-r--r-- | test/unit/router_test.rb | 3 |
6 files changed, 55 insertions, 14 deletions
diff --git a/app/jobs/action_mailroom/deliver_inbound_email_to_mailroom_job.rb b/app/jobs/action_mailroom/deliver_inbound_email_to_mailroom_job.rb index 0aeccff96e..bc5150afcb 100644 --- a/app/jobs/action_mailroom/deliver_inbound_email_to_mailroom_job.rb +++ b/app/jobs/action_mailroom/deliver_inbound_email_to_mailroom_job.rb @@ -3,7 +3,7 @@ module ActionMailroom queue_as :action_mailroom_inbound_email def perform(inbound_email) - # ActionMailroom::Router.receive inbound_email + ActionMailroom::Mailbox.route inbound_email end end end diff --git a/lib/action_mailroom/mailbox.rb b/lib/action_mailroom/mailbox.rb index a2c097a42f..3518449794 100644 --- a/lib/action_mailroom/mailbox.rb +++ b/lib/action_mailroom/mailbox.rb @@ -1,21 +1,19 @@ require "active_support/rescuable" + require "action_mailroom/mailbox/callbacks" +require "action_mailroom/mailbox/routing" class ActionMailroom::Mailbox - include ActiveSupport::Rescuable, Callbacks + include ActiveSupport::Rescuable + include Callbacks, Routing - class << self - def receive(inbound_email) - new(inbound_email).perform_processing - end + attr_reader :inbound_email + delegate :mail, to: :inbound_email - def routing(routes) - @router = ActionMailroom::Router.new(routes) - end + def self.receive(inbound_email) + new(inbound_email).perform_processing end - attr_reader :inbound_email - delegate :mail, to: :inbound_email def initialize(inbound_email) @inbound_email = inbound_email diff --git a/lib/action_mailroom/mailbox/routing.rb b/lib/action_mailroom/mailbox/routing.rb new file mode 100644 index 0000000000..9f082c8aa5 --- /dev/null +++ b/lib/action_mailroom/mailbox/routing.rb @@ -0,0 +1,15 @@ +module ActionMailroom::Mailbox::Routing + extend ActiveSupport::Concern + + class_methods do + attr_reader :router + + def routing(routes) + (@router ||= ActionMailroom::Router.new).add_routes(routes) + end + + def route(inbound_email) + @router.route(inbound_email) + end + end +end diff --git a/lib/action_mailroom/router.rb b/lib/action_mailroom/router.rb index 066d7163e4..bf0001f1ae 100644 --- a/lib/action_mailroom/router.rb +++ b/lib/action_mailroom/router.rb @@ -1,6 +1,10 @@ class ActionMailroom::Router - def initialize(routes) - @routes = routes + def initialize + @routes = {} + end + + def add_routes(routes) + @routes.merge!(routes) end def route(inbound_email) diff --git a/test/unit/mailbox/routing_test.rb b/test/unit/mailbox/routing_test.rb new file mode 100644 index 0000000000..bdb670e956 --- /dev/null +++ b/test/unit/mailbox/routing_test.rb @@ -0,0 +1,23 @@ +require_relative '../../test_helper' + +class ApplicationMailbox < ActionMailroom::Mailbox + routing "replies@example.com" => :replies +end + +class RepliesMailbox < ActionMailroom::Mailbox + def process + $processed = mail.subject + end +end + +class ActionMailroom::Mailbox::RoutingTest < ActiveSupport::TestCase + setup do + $processed = false + @inbound_email = create_inbound_email("welcome.eml") + end + + test "string routing" do + ApplicationMailbox.route @inbound_email + assert_equal "Discussion: Let's debate these attachments", $processed + end +end diff --git a/test/unit/router_test.rb b/test/unit/router_test.rb index 7dbacbb865..6bfd880b67 100644 --- a/test/unit/router_test.rb +++ b/test/unit/router_test.rb @@ -9,7 +9,8 @@ end module ActionMailroom class RouterTest < ActiveSupport::TestCase setup do - @router = ActionMailroom::Router.new('replies@example.com' => :replies) + @router = ActionMailroom::Router.new + @router.add_routes('replies@example.com' => :replies) $processed = false end |