From e49a55ca880bf0b3d1fe7beff1aba8da75c790df Mon Sep 17 00:00:00 2001 From: AMHOL Date: Fri, 5 Apr 2019 11:47:47 +0800 Subject: [PATCH] Allow configuration of callback host from Rails controller --- lib/mandrill-rails/web_hook_processor.rb | 16 ++++++++++- .../mandrill-rails/web_hook_processor_spec.rb | 28 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/mandrill-rails/web_hook_processor.rb b/lib/mandrill-rails/web_hook_processor.rb index 6162c77..9be3f7a 100644 --- a/lib/mandrill-rails/web_hook_processor.rb +++ b/lib/mandrill-rails/web_hook_processor.rb @@ -31,6 +31,8 @@ # end # module Mandrill::Rails::WebHookProcessor + Undefined = Object.new.freeze + extend ActiveSupport::Concern included do @@ -71,6 +73,15 @@ def on_unhandled_mandrill_events!(new_setting=nil) @on_unhandled_mandrill_events end + # Sets or gets the callback host + def callback_host(callback_host = Undefined) + if Undefined === callback_host + @callback_host + else + @callback_host = callback_host + end + end + def ignore_unhandled_events! on_unhandled_mandrill_events! :ignore end @@ -90,7 +101,10 @@ def show # Handles controller :create action (corresponds to a POST from Mandrill). def create - processor = Mandrill::WebHook::Processor.new(params, self) + processor = Mandrill::WebHook::Processor.new( + params, + self.class.callback_host || self + ) processor.on_unhandled_mandrill_events = self.class.on_unhandled_mandrill_events! processor.run! head(:ok) diff --git a/spec/mandrill-rails/web_hook_processor_spec.rb b/spec/mandrill-rails/web_hook_processor_spec.rb index 64a7eee..f087e84 100644 --- a/spec/mandrill-rails/web_hook_processor_spec.rb +++ b/spec/mandrill-rails/web_hook_processor_spec.rb @@ -98,6 +98,34 @@ def head(*args) ; end processor_instance.create end + context "with a custom callback host" do + let(:custom_callback_host) { double('CallbackHost') } + let(:params) do + { + "mandrill_events" => JSON.dump([ + { + "event" => "hard_bounce" + } + ]) + } + end + before do + WebHookProcessorTestHarness.callback_host(custom_callback_host) + processor_instance.params = params + end + after do + WebHookProcessorTestHarness.callback_host(nil) + end + it "returns head(:ok) and dispatces the event to callback host" do + expect(processor_instance).to receive(:head).with(:ok) + expect_any_instance_of(Mandrill::WebHook::Processor).to receive(:on_unhandled_mandrill_events=).with(:log) + expect(custom_callback_host).to receive(:handle_hard_bounce).with( + Mandrill::WebHook::EventDecorator["event" => "hard_bounce"] + ) + processor_instance.create + end + end + context "when unhandled events set to raise exceptions" do it "delegates the setting to the processor" do processor_instance.class.unhandled_events_raise_exceptions!