Class: Assinafy::Resources::WebhookResource
- Inherits:
-
BaseResource
- Object
- BaseResource
- Assinafy::Resources::WebhookResource
- Defined in:
- lib/assinafy/resources/webhook_resource.rb,
sig/assinafy.rbs
Overview
Webhook subscription, event-type catalog, delivery history, and retries.
See https://api.assinafy.com.br/v1/docs#webhooks for the full documentation of these endpoints.
Constant Summary
Constants inherited from BaseResource
BaseResource::AUTH_HEADERS, BaseResource::PAGINATION_HEADERS, BaseResource::PATH_SEGMENT
Instance Method Summary collapse
-
#get(account_id_override = nil) ⇒ Hash?
Fetch the current webhook subscription.
-
#inactivate(account_id_override = nil) ⇒ Hash
Inactivate (but keep) the account's webhook subscription.
-
#list_dispatches(params = {}, account_id_override = nil) ⇒ Hash{Symbol=>Array,Hash}
List webhook delivery attempts (dispatches) with pagination metadata.
-
#list_event_types ⇒ Array<Hash>
Catalogue of supported event-type identifiers.
-
#register(payload, account_id_override = nil) ⇒ Hash
(also: #update)
Create or replace the account's webhook subscription.
-
#retry_dispatch(dispatch_id, account_id_override = nil) ⇒ Hash
Force a single dispatch to be re-attempted.
Methods inherited from BaseResource
Constructor Details
This class inherits a constructor from Assinafy::Resources::BaseResource
Instance Method Details
#get(account_id_override = nil) ⇒ Hash?
Fetch the current webhook subscription. Returns nil on 404
(no subscription configured yet).
90 91 92 93 94 95 96 |
# File 'lib/assinafy/resources/webhook_resource.rb', line 90 def get(account_id_override = nil) acc_id = account_id(account_id_override) call_optional('Failed to fetch webhook subscription') do http_get("accounts/#{acc_id}/webhooks/subscriptions") end end |
#inactivate(account_id_override = nil) ⇒ Hash
Inactivate (but keep) the account's webhook subscription. Stops deliveries without losing the configured event set.
115 116 117 118 119 120 121 122 123 |
# File 'lib/assinafy/resources/webhook_resource.rb', line 115 def inactivate(account_id_override = nil) acc_id = account_id(account_id_override) @logger.info('Inactivating webhook subscription') call('Failed to inactivate webhook subscription') do http_put("accounts/#{acc_id}/webhooks/inactivate") end end |
#list_dispatches(params = {}, account_id_override = nil) ⇒ Hash{Symbol=>Array,Hash}
List webhook delivery attempts (dispatches) with pagination metadata.
180 181 182 183 184 185 186 |
# File 'lib/assinafy/resources/webhook_resource.rb', line 180 def list_dispatches(params = {}, account_id_override = nil) acc_id = account_id(account_id_override) call_list('Failed to list webhook dispatches') do http_get("accounts/#{acc_id}/webhooks", params) end end |
#list_event_types ⇒ Array<Hash>
Catalogue of supported event-type identifiers.
144 145 146 147 148 |
# File 'lib/assinafy/resources/webhook_resource.rb', line 144 def list_event_types call_array('Failed to list webhook event types') do http_get('webhooks/event-types') end end |
#register(payload, account_id_override = nil) ⇒ Hash Also known as: update
Create or replace the account's webhook subscription. The API uses
PUT subscriptions for both create and update semantics, hence the
name register (with an update alias).
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/assinafy/resources/webhook_resource.rb', line 44 def register(payload, account_id_override = nil) p = require_payload(payload, 'Webhook payload').transform_keys(&:to_sym) raise ValidationError.new('Webhook URL is required') if p[:url].to_s.strip.empty? raise ValidationError.new('Webhook email is required') if p[:email].to_s.strip.empty? events = require_array(p[:events], 'Webhook events') unless events.all? { |event| event.is_a?(String) && !event.strip.empty? } raise ValidationError.new('Webhook events must be non-empty Strings') end acc_id = account_id(account_id_override) body = { url: p[:url], email: p[:email], events: events, is_active: p.key?(:is_active) ? require_boolean(p[:is_active], 'is_active') : true } @logger.info('Registering webhook subscription') call('Failed to register webhook') do http_put("accounts/#{acc_id}/webhooks/subscriptions", body_params(body)) end end |
#retry_dispatch(dispatch_id, account_id_override = nil) ⇒ Hash
Force a single dispatch to be re-attempted.
212 213 214 215 216 217 218 219 |
# File 'lib/assinafy/resources/webhook_resource.rb', line 212 def retry_dispatch(dispatch_id, account_id_override = nil) acc_id = account_id(account_id_override) did = require_id(dispatch_id, 'Dispatch ID') call('Failed to retry webhook dispatch') do http_post("accounts/#{acc_id}/webhooks/#{did}/retry") end end |