Module: Webhooks::Outgoing::IssuingModel

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/webhooks/outgoing/issuing_model.rb

Instance Method Summary collapse

Instance Method Details

#generate_created_webhookObject



63
64
65
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 63

def generate_created_webhook
  generate_webhook(:created)
end

#generate_deleted_webhookObject



71
72
73
74
75
76
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 71

def generate_deleted_webhook
  return false unless parent.present?
  return false if parent.being_destroyed?

  generate_webhook(:deleted, async: false)
end

#generate_updated_webhookObject



67
68
69
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 67

def generate_updated_webhook
  generate_webhook(:updated)
end

#generate_webhook(action, async: true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 22

def generate_webhook(action, async: true)
  # allow individual models to opt out of generating webhooks
  return if skip_generate_webhook?(action)

  # we can only generate webhooks for objects that return their their team / parent.
  return unless parent.present?

  # Try to find an event type definition for this action.
  event_type = Webhooks::Outgoing::EventType.find_by(id: "#{self.class.name.underscore}.#{action}")

  # If the event type is defined as one that people can be subscribed to,
  # and this object has a parent where an associated outgoing webhooks endpoint could be registered.
  if event_type
    # Only generate an event record if an endpoint is actually listening for this event type.
    # If there are endpoints listening, make sure we know which API versions they're looking for.
    if (api_versions = parent.endpoint_api_versions_listening_for_event_type(event_type)).any?
      if async
        # serialization can be heavy so run it as a job
        Webhooks::Outgoing::GenerateJob.perform_later(self, action, api_versions)
      else
        generate_webhook_perform(action, api_versions)
      end
    end
  end
end

#generate_webhook_perform(action, api_versions) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 48

def generate_webhook_perform(action, api_versions)
  event_type = Webhooks::Outgoing::EventType.find_by(id: "#{self.class.name.underscore}.#{action}")

  api_versions.each do |api_version|
    webhook = send(BulletTrain::OutgoingWebhooks.parent_association).webhooks_outgoing_events.create(
      event_type_id: event_type.id,
      subject: self,
      data: api_attributes(api_version),
      api_version: api_version
    )

    webhook.deliver
  end
end

#parentObject

TODO This should probably be called ‘outgoing_webhooks_parent` to avoid colliding with downstream `parent` methods.



17
18
19
20
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 17

def parent
  return unless respond_to? BulletTrain::OutgoingWebhooks.parent_association
  send(BulletTrain::OutgoingWebhooks.parent_association)
end

#skip_generate_webhook?(action) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/concerns/webhooks/outgoing/issuing_model.rb', line 12

def skip_generate_webhook?(action)
  false
end