From a538f25ecddfa9dc52eb036a50c936516fb53c85 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Tue, 9 Feb 2021 17:46:19 -0600 Subject: [PATCH] WebhookAdapters are autoregistered --- app/models/object_event_hook_config.rb | 9 +------- gems/bess/lib/houdini.rb | 2 -- gems/bess/lib/houdini/engine.rb | 1 - gems/bess/lib/houdini/webhook_adapter.rb | 21 ++++++++++++++++--- .../{open_fn.rb => open_fn_adapter.rb} | 2 +- spec/models/object_event_hook_config_spec.rb | 6 +++--- 6 files changed, 23 insertions(+), 18 deletions(-) rename gems/bess/lib/houdini/webhook_adapter/{open_fn.rb => open_fn_adapter.rb} (67%) diff --git a/app/models/object_event_hook_config.rb b/app/models/object_event_hook_config.rb index 35d14c80..ce4973e6 100644 --- a/app/models/object_event_hook_config.rb +++ b/app/models/object_event_hook_config.rb @@ -16,14 +16,7 @@ class ObjectEventHookConfig < ApplicationRecord serialize :object_event_types, Array - WEBHOOK = { - open_fn: 'open_fn' - }.freeze - def webhook - case webhook_service - when WEBHOOK[:open_fn] - Houdini::WebhookAdapter::OpenFn.new(configuration) - end + Houdini::WebhookAdapter.build(webhook_service, configuration.symbolize_keys) end end diff --git a/gems/bess/lib/houdini.rb b/gems/bess/lib/houdini.rb index 65098f71..9ba15f01 100644 --- a/gems/bess/lib/houdini.rb +++ b/gems/bess/lib/houdini.rb @@ -39,6 +39,4 @@ module Houdini mattr_accessor :core_classes, default: {supporter: 'Supporter', nonprofit: 'Nonprofit'} mattr_accessor :event_publisher, default: Houdini::EventPublisher.new - - mattr_accessor :webhook_adapter end diff --git a/gems/bess/lib/houdini/engine.rb b/gems/bess/lib/houdini/engine.rb index b9ef1693..0f3d043d 100644 --- a/gems/bess/lib/houdini/engine.rb +++ b/gems/bess/lib/houdini/engine.rb @@ -65,7 +65,6 @@ module Houdini config.houdini.listeners = [] - initializer 'houdini.set_configuration', before: 'factory_bot.set_fixture_replacement' do |app| app.config.to_prepare do Houdini.core_classes = app.config.houdini.core_classes diff --git a/gems/bess/lib/houdini/webhook_adapter.rb b/gems/bess/lib/houdini/webhook_adapter.rb index cdd99a07..6b74e1f7 100644 --- a/gems/bess/lib/houdini/webhook_adapter.rb +++ b/gems/bess/lib/houdini/webhook_adapter.rb @@ -4,11 +4,11 @@ class Houdini::WebhookAdapter extend ActiveSupport::Autoload include ActiveModel::AttributeAssignment - autoload :OpenFn + autoload :OpenFnAdapter attr_accessor :webhook_url, :headers - def initialize(attributes={}) - assign_attributes(attributes) if attributes + def initialize(**attributes) + assign_attributes(**attributes) if attributes end def transmit(payload) @@ -19,4 +19,19 @@ class Houdini::WebhookAdapter headers: headers ) end + + ADAPTER = 'Adapter' + private_constant :ADAPTER + + # based on ActiveJob's configuration + class << self + + def build(name, options) + lookup(name).new(**options) + end + + def lookup(name) + const_get(name.to_s.camelize << ADAPTER) + end + end end diff --git a/gems/bess/lib/houdini/webhook_adapter/open_fn.rb b/gems/bess/lib/houdini/webhook_adapter/open_fn_adapter.rb similarity index 67% rename from gems/bess/lib/houdini/webhook_adapter/open_fn.rb rename to gems/bess/lib/houdini/webhook_adapter/open_fn_adapter.rb index d40618e5..a7a40e43 100644 --- a/gems/bess/lib/houdini/webhook_adapter/open_fn.rb +++ b/gems/bess/lib/houdini/webhook_adapter/open_fn_adapter.rb @@ -1,4 +1,4 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE -class Houdini::WebhookAdapter::OpenFn < Houdini::WebhookAdapter +class Houdini::WebhookAdapter::OpenFnAdapter < Houdini::WebhookAdapter end diff --git a/spec/models/object_event_hook_config_spec.rb b/spec/models/object_event_hook_config_spec.rb index 9b10f666..65c95d92 100644 --- a/spec/models/object_event_hook_config_spec.rb +++ b/spec/models/object_event_hook_config_spec.rb @@ -11,10 +11,10 @@ RSpec.describe ObjectEventHookConfig, type: :model do describe '.webhook' do it 'returns an instance of OpenFn webhook' do webhook = double - expect(Houdini::WebhookAdapter::OpenFn) - .to receive(:new) + expect(Houdini::WebhookAdapter) + .to receive(:build) + .with(open_fn_config.webhook_service, open_fn_config.configuration.symbolize_keys) .and_return(webhook) - .with(open_fn_config.configuration) result = open_fn_config.webhook expect(result).to eq(webhook) end