WebhookAdapters are autoregistered

This commit is contained in:
Eric Schultz 2021-02-09 17:46:19 -06:00 committed by Eric Schultz
parent 095f241ae1
commit a538f25ecd
6 changed files with 23 additions and 18 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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