Class: Spree::WebhookDelivery

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/webhook_delivery.rb

Constant Summary collapse

ERROR_TYPES =
%w[timeout connection_error].freeze

Instance Method Summary collapse

Instance Method Details

#complete!(response_code: nil, execution_time:, error_type: nil, request_errors: nil, response_body: nil) ⇒ Object

Mark delivery as completed with HTTP response. Triggers auto-disable check on the endpoint after failures.

Parameters:

  • response_code (Integer) (defaults to: nil)

    HTTP response code

  • execution_time (Integer)

    time in milliseconds

  • response_body (String) (defaults to: nil)

    response body from the webhook endpoint



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/spree/webhook_delivery.rb', line 51

def complete!(response_code: nil, execution_time:, error_type: nil, request_errors: nil, response_body: nil)
  is_success = response_code.present? && response_code.to_s.start_with?('2')

  update!(
    response_code: response_code,
    execution_time: execution_time,
    error_type: error_type,
    request_errors: request_errors,
    response_body: response_body,
    success: is_success,
    delivered_at: Time.current
  )

  webhook_endpoint.check_auto_disable! unless is_success
end

#failed?Boolean

Check if the delivery failed

Returns:

  • (Boolean)


34
35
36
# File 'app/models/spree/webhook_delivery.rb', line 34

def failed?
  success == false
end

#pending?Boolean

Check if the delivery is pending

Returns:

  • (Boolean)


41
42
43
# File 'app/models/spree/webhook_delivery.rb', line 41

def pending?
  delivered_at.nil?
end

#queue_for_delivery!Object

Queue this delivery for processing. Resolves the job class dynamically since it lives in the api gem.



84
85
86
# File 'app/models/spree/webhook_delivery.rb', line 84

def queue_for_delivery!
  'Spree::WebhookDeliveryJob'.constantize.perform_later(id)
end

#redeliver!Spree::WebhookDelivery

Create a new delivery with the same payload and queue it. Used to retry failed deliveries manually.

Returns:



71
72
73
74
75
76
77
78
79
80
# File 'app/models/spree/webhook_delivery.rb', line 71

def redeliver!
  new_delivery = webhook_endpoint.webhook_deliveries.create!(
    event_name: event_name,
    event_id: nil, # new delivery, not a duplicate
    payload: payload
  )

  new_delivery.queue_for_delivery!
  new_delivery
end

#successful?Boolean

Check if the delivery was successful

Returns:

  • (Boolean)


27
28
29
# File 'app/models/spree/webhook_delivery.rb', line 27

def successful?
  success == true
end