Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of callback host from Rails controller #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/mandrill-rails/web_hook_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
# end
#
module Mandrill::Rails::WebHookProcessor
Undefined = Object.new.freeze

extend ActiveSupport::Concern

included do
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions spec/mandrill-rails/web_hook_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down