Class: Fuik::WebhookEvent

Inherits:
ApplicationRecord show all
Includes:
Filterable
Defined in:
app/models/fuik/webhook_event.rb,
app/models/fuik/webhook_event/filterable.rb

Defined Under Namespace

Modules: Filterable

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.event_class_for(provider, event_type) ⇒ Object



7
8
9
# File 'app/models/fuik/webhook_event.rb', line 7

def self.event_class_for(provider, event_type)
  "#{provider.camelize}::#{event_type.tr("./:-[]", "_").camelize}".safe_constantize
end

Instance Method Details

#failed!(error = nil) ⇒ Boolean

Mark the event as failed and record the error.

Parameters:

  • error (String, nil) (defaults to: nil)

    optional error message or object

Returns:

  • (Boolean)

    true if the update succeeded



36
37
38
# File 'app/models/fuik/webhook_event.rb', line 36

def failed!(error = nil)
  update!(status: "failed", error: error.to_s)
end

#payloadObject



18
19
20
21
22
# File 'app/models/fuik/webhook_event.rb', line 18

def payload
  @payload ||= JSON.parse(body)
rescue JSON::ParserError
  {}
end

#process_later!Object



53
54
55
56
57
# File 'app/models/fuik/webhook_event.rb', line 53

def process_later!
  event_class = self.class.event_class_for(provider, event_type)

  Fuik.webhook_processing_job_class.constantize.perform_later(event_class.name, self) if event_class
end

#processed!Boolean

Mark the event as successfully processed.

Returns:

  • (Boolean)

    true if the update succeeded



27
28
29
# File 'app/models/fuik/webhook_event.rb', line 27

def processed!
  update!(status: "processed")
end

#retry!Boolean

Retry a failed event. Resets its status back to pending, clears the error, and enqueues processing via WebhookProcessingJob.

Returns:

  • (Boolean)

    true if the update succeeded

Raises:

  • (RuntimeError)

    if the event is not in a failed state



46
47
48
49
50
51
# File 'app/models/fuik/webhook_event.rb', line 46

def retry!
  raise "Can only retry failed events" unless failed?

  update!(status: "pending", error: nil)
  process_later!
end