WebhookAdapters are autoregistered
This commit is contained in:
parent
095f241ae1
commit
a538f25ecd
6 changed files with 23 additions and 18 deletions
|
@ -16,14 +16,7 @@ class ObjectEventHookConfig < ApplicationRecord
|
||||||
|
|
||||||
serialize :object_event_types, Array
|
serialize :object_event_types, Array
|
||||||
|
|
||||||
WEBHOOK = {
|
|
||||||
open_fn: 'open_fn'
|
|
||||||
}.freeze
|
|
||||||
|
|
||||||
def webhook
|
def webhook
|
||||||
case webhook_service
|
Houdini::WebhookAdapter.build(webhook_service, configuration.symbolize_keys)
|
||||||
when WEBHOOK[:open_fn]
|
|
||||||
Houdini::WebhookAdapter::OpenFn.new(configuration)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -39,6 +39,4 @@ module Houdini
|
||||||
mattr_accessor :core_classes, default: {supporter: 'Supporter', nonprofit: 'Nonprofit'}
|
mattr_accessor :core_classes, default: {supporter: 'Supporter', nonprofit: 'Nonprofit'}
|
||||||
|
|
||||||
mattr_accessor :event_publisher, default: Houdini::EventPublisher.new
|
mattr_accessor :event_publisher, default: Houdini::EventPublisher.new
|
||||||
|
|
||||||
mattr_accessor :webhook_adapter
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -65,7 +65,6 @@ module Houdini
|
||||||
|
|
||||||
config.houdini.listeners = []
|
config.houdini.listeners = []
|
||||||
|
|
||||||
|
|
||||||
initializer 'houdini.set_configuration', before: 'factory_bot.set_fixture_replacement' do |app|
|
initializer 'houdini.set_configuration', before: 'factory_bot.set_fixture_replacement' do |app|
|
||||||
app.config.to_prepare do
|
app.config.to_prepare do
|
||||||
Houdini.core_classes = app.config.houdini.core_classes
|
Houdini.core_classes = app.config.houdini.core_classes
|
||||||
|
|
|
@ -4,11 +4,11 @@ class Houdini::WebhookAdapter
|
||||||
extend ActiveSupport::Autoload
|
extend ActiveSupport::Autoload
|
||||||
include ActiveModel::AttributeAssignment
|
include ActiveModel::AttributeAssignment
|
||||||
|
|
||||||
autoload :OpenFn
|
autoload :OpenFnAdapter
|
||||||
|
|
||||||
attr_accessor :webhook_url, :headers
|
attr_accessor :webhook_url, :headers
|
||||||
def initialize(attributes={})
|
def initialize(**attributes)
|
||||||
assign_attributes(attributes) if attributes
|
assign_attributes(**attributes) if attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
def transmit(payload)
|
def transmit(payload)
|
||||||
|
@ -19,4 +19,19 @@ class Houdini::WebhookAdapter
|
||||||
headers: headers
|
headers: headers
|
||||||
)
|
)
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
# 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
|
# 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
|
end
|
|
@ -11,10 +11,10 @@ RSpec.describe ObjectEventHookConfig, type: :model do
|
||||||
describe '.webhook' do
|
describe '.webhook' do
|
||||||
it 'returns an instance of OpenFn webhook' do
|
it 'returns an instance of OpenFn webhook' do
|
||||||
webhook = double
|
webhook = double
|
||||||
expect(Houdini::WebhookAdapter::OpenFn)
|
expect(Houdini::WebhookAdapter)
|
||||||
.to receive(:new)
|
.to receive(:build)
|
||||||
|
.with(open_fn_config.webhook_service, open_fn_config.configuration.symbolize_keys)
|
||||||
.and_return(webhook)
|
.and_return(webhook)
|
||||||
.with(open_fn_config.configuration)
|
|
||||||
result = open_fn_config.webhook
|
result = open_fn_config.webhook
|
||||||
expect(result).to eq(webhook)
|
expect(result).to eq(webhook)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue