Sha256: 6abef2ece222fc38df129038e97db2607d6b7ec49ef0fc13589944fa8faec0f4

Contents?: true

Size: 1.47 KB

Versions: 129

Compression:

Stored size: 1.47 KB

Contents

module Webhooks::Outgoing::EventSupport
  extend ActiveSupport::Concern
  include HasUuid

  included do
    belongs_to BulletTrain::OutgoingWebhooks.parent_association
    belongs_to :event_type, class_name: "Webhooks::Outgoing::EventType"
    belongs_to :subject, polymorphic: true
    has_many :deliveries, dependent: :destroy

    before_create do
      self.payload = generate_payload
    end
  end

  def generate_payload
    {
      event_id: uuid,
      event_type: event_type_id,
      subject_id: subject_id,
      subject_type: subject_type,
      data: data
    }
  end

  def event_type_name
    payload.dig("event_type")
  end

  def endpoints
    endpoints = send(BulletTrain::OutgoingWebhooks.parent_association).webhooks_outgoing_endpoints.where(api_version: api_version).listening_for_event_type_id(event_type_id)

    case subject_type
    when "Scaffolding::AbsolutelyAbstract::CreativeConcept"
      endpoints.where(scaffolding_absolutely_abstract_creative_concept_id: [subject.id, nil])
    when "Scaffolding::CompletelyConcrete::TangibleThing"
      endpoints.where(scaffolding_absolutely_abstract_creative_concept_id: [subject.absolutely_abstract_creative_concept_id, nil])
    else
      endpoints
    end
  end

  def deliver
    endpoints.each do |endpoint|
      unless endpoint.deliveries.where(event: self).any?
        endpoint.deliveries.create(event: self, endpoint_url: endpoint.url).deliver_async
      end
    end
  end

  def label_string
    short_uuid
  end
end

Version data entries

129 entries across 129 versions & 1 rubygems

Version Path
bullet_train-outgoing_webhooks-1.10.0 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.9.0 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.8.5 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.8.4 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.8.3 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.8.2 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.8.1 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.8.0 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.23 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.22 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.21 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.20 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.19 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.18 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.17 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.16 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.15 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.14 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.13 app/models/concerns/webhooks/outgoing/event_support.rb
bullet_train-outgoing_webhooks-1.7.12 app/models/concerns/webhooks/outgoing/event_support.rb