Class: Integral::Webhook::Endpoint

Inherits:
ApplicationRecord show all
Defined in:
app/models/integral/webhook/endpoint.rb

Overview

A Webhook::Endpoint can listen to one or more Webhook::Event, everytime an Event is created it is sent (along with its payload) to the Endpoint target_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

available_statuses

Class Method Details

.for_event(events) ⇒ Integral::Webhook::Endpoint

Returns endpoints which are listening for the provided events.

Parameters:

  • events (Array)

    list of Integral::Webhook::Event names

Returns:



21
22
23
# File 'app/models/integral/webhook/endpoint.rb', line 21

def self.for_event(events)
  where('events @> ARRAY[?]::varchar[]', Array(events))
end

Instance Method Details

#deliver(event) ⇒ Object

Deliver a particular event to the endpoint



35
36
37
# File 'app/models/integral/webhook/endpoint.rb', line 35

def deliver(event)
  Webhook::DeliveryJob.perform_later(id, event.to_json)
end

#events=(events) ⇒ Object

Override the default setter to normalise the events and validate them (TODO)



26
27
28
29
30
31
32
# File 'app/models/integral/webhook/endpoint.rb', line 26

def events=(events)
  events = Array(events).map { |event| event.to_s.underscore }
  # TODO: Validate events from specified list in Integral.config which can be used to
  # list them on the backend for click and create
  # super(Webhook::Event::EVENT_TYPES & events)
  super(events)
end