Class: Spree::WebhookDelivery
- Inherits:
-
Object
- Object
- Spree::WebhookDelivery
- Defined in:
- app/models/spree/webhook_delivery.rb
Constant Summary collapse
- ERROR_TYPES =
%w[timeout connection_error].freeze
Instance Attribute Summary collapse
-
#payload_secrets ⇒ Object
Credentials stripped from the persisted payload.
Instance Method Summary collapse
-
#complete!(response_code: nil, execution_time:, error_type: nil, request_errors: nil, response_body: nil) ⇒ Object
Mark delivery as completed with HTTP response.
-
#deliverable_payload ⇒ Hash
Payload as it should be sent over the wire.
-
#failed? ⇒ Boolean
Check if the delivery failed.
-
#pending? ⇒ Boolean
Check if the delivery is pending.
-
#queue_for_delivery! ⇒ Object
Queue this delivery for processing.
-
#redeliver! ⇒ Spree::WebhookDelivery
Create a new delivery with the same payload and queue it.
-
#successful? ⇒ Boolean
Check if the delivery was successful.
Instance Attribute Details
#payload_secrets ⇒ Object
Credentials stripped from the persisted payload. Never written to the database — held in memory on a single delivery instance, assigned by WebhookDeliveryJob when it runs. See Spree::WebhookPayloadRedaction.
13 14 15 |
# File 'app/models/spree/webhook_delivery.rb', line 13 def payload_secrets @payload_secrets end |
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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/spree/webhook_delivery.rb', line 56 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 |
#deliverable_payload ⇒ Hash
Payload as it should be sent over the wire.
Re-attaches any credentials withheld from the persisted column. Once this record has been reloaded from the database those secrets are gone for good, so a redelivery sends the redacted placeholder — single-use tokens are not replayable anyway.
This is deliberate: keeping a recoverable copy would put the credential back at rest, which is what redaction exists to prevent. If a delivery is lost (worker crash between creation and delivery), the customer requests a new reset, which mints a fresh token.
85 86 87 |
# File 'app/models/spree/webhook_delivery.rb', line 85 def deliverable_payload Spree::WebhookPayloadRedaction.merge(payload, payload_secrets) end |
#failed? ⇒ Boolean
Check if the delivery failed
39 40 41 |
# File 'app/models/spree/webhook_delivery.rb', line 39 def failed? success == false end |
#pending? ⇒ Boolean
Check if the delivery is pending
46 47 48 |
# File 'app/models/spree/webhook_delivery.rb', line 46 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.
106 107 108 |
# File 'app/models/spree/webhook_delivery.rb', line 106 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.
93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/spree/webhook_delivery.rb', line 93 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
32 33 34 |
# File 'app/models/spree/webhook_delivery.rb', line 32 def successful? success == true end |