Class: AbacatePay::Clients::WebhookClient

Inherits:
Client
  • Object
show all
Defined in:
lib/abacate_pay/clients/webhook_client.rb

Overview

Client for managing webhook endpoint registrations in the AbacatePay API.

This manages where AbacatePay delivers events. To verify and parse an inbound delivery, use Webhooks.construct_event.

Constant Summary collapse

URI =
"webhooks"

Instance Method Summary collapse

Constructor Details

#initialize(client = nil) ⇒ WebhookClient

Returns a new instance of WebhookClient.

Parameters:

  • client (Faraday::Connection, nil) (defaults to: nil)

    Optional Faraday client



15
16
17
# File 'lib/abacate_pay/clients/webhook_client.rb', line 15

def initialize(client = nil)
  super(URI, client)
end

Instance Method Details

#create(name:, endpoint:, secret:, events:) ⇒ Resources::WebhookEndpoints

Registers a new webhook endpoint.

The secret is what AbacatePay signs deliveries with — pass the same value to Webhooks.construct_event when handling them.

Parameters:

  • name (String)

    Identifying name

  • endpoint (String)

    HTTPS URL that will receive the events

  • secret (String)

    Signing secret

  • events (Array<String>)

    Event types to subscribe to

Returns:

Raises:

  • (ArgumentError)

    if the endpoint is not an HTTPS URL



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/abacate_pay/clients/webhook_client.rb', line 44

def create(name:, endpoint:, secret:, events:)
  raise ArgumentError, "endpoint must be an HTTPS URL, got #{endpoint.inspect}" unless https_url?(endpoint)
  raise ArgumentError, "events must not be empty" if Array(events).empty?

  response = request("POST", "create", json: {
                       name: name,
                       endpoint: endpoint,
                       secret: secret,
                       events: Array(events)
                     })
  Resources::WebhookEndpoints.new(response)
end

#delete(id) ⇒ Resources::WebhookEndpoints

Returns The deleted webhook.

Parameters:

  • id (String)

    The webhook ID

Returns:



59
60
61
62
# File 'lib/abacate_pay/clients/webhook_client.rb', line 59

def delete(id)
  response = request("POST", "delete", json: { id: id })
  Resources::WebhookEndpoints.new(response)
end

#get(id) ⇒ Resources::WebhookEndpoints

Parameters:

  • id (String)

    The webhook ID

Returns:



28
29
30
31
# File 'lib/abacate_pay/clients/webhook_client.rb', line 28

def get(id)
  response = request("GET", "get", params: { id: id })
  Resources::WebhookEndpoints.new(response)
end

#list(**params) ⇒ Array<Resources::WebhookEndpoints>

Parameters:

  • params (Hash)

    Optional params (limit, after, before, startDate, endDate)

Returns:



21
22
23
24
# File 'lib/abacate_pay/clients/webhook_client.rb', line 21

def list(**params)
  response = request("GET", "list", params: params.empty? ? nil : params)
  Array(response).map { |data| Resources::WebhookEndpoints.new(data) }
end