Class: Coinbase::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/webhook.rb

Overview

A representation of a Webhook. This class provides methods to create, list, update, and delete webhooks that are used to receive notifications of specific events.

Constant Summary collapse

ERC20_TRANSFER_EVENT =

Event type for ERC20 transfer

'erc20_transfer'
ERC721_TRANSFER_EVENT =

Event type for ERC721 transfer

'erc721_transfer'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Webhook

Initializes a new Webhook object.

Parameters:

Raises:

  • (ArgumentError)

    If the model is not a Coinbase::Client::Webhook.



78
79
80
81
82
# File 'lib/coinbase/webhook.rb', line 78

def initialize(model)
  raise ArgumentError, 'model must be a Webhook' unless model.is_a?(Coinbase::Client::Webhook)

  @model = model
end

Class Method Details

.create(network_id:, notification_uri:, event_type:, event_filters:, signature_header: '') ⇒ Coinbase::Webhook

Creates a new webhook for a specified network.

Examples:

Create a new webhook

webhook = Coinbase::Webhook.create(
  network_id: :ethereum_mainnet,
  notification_uri: 'https://example.com/callback',
  event_type: 'transaction',
  event_filters: [{ 'contract_address' => '0x...', 'from_address' => '0x...', 'to_address' => '0x...' }],
  signature_header: 'example_header'
)

Parameters:

  • network_id (String)

    The network ID for which the webhook is created.

  • notification_uri (String)

    The URI where notifications should be sent.

  • event_type (String)

    The type of event for the webhook. Must be one of the following:

    • ‘Coinbase::Webhook::ERC20_TRANSFER_EVENT`

    • ‘Coinbase::Webhook::ERC721_TRANSFER_EVENT`

  • event_filters (Array<Hash>)

    Filters applied to the events that determine which specific events trigger the webhook. Each filter should be a hash that can include keys like ‘contract_address`, `from_address`, or `to_address`.

  • signature_header (String) (defaults to: '')

    The custom header to be used for x-webhook-signature header on callbacks, so developers can verify the requests are coming from Coinbase.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/coinbase/webhook.rb', line 37

def create(network_id:, notification_uri:, event_type:, event_filters:, signature_header: '')
  model = Coinbase.call_api do
    webhooks_api.create_webhook(
      create_webhook_request: {
        network_id: Coinbase.normalize_network(network_id),
        notification_uri: notification_uri,
        event_type: event_type,
        event_filters: event_filters,
        signature_header: signature_header
      }
    )
  end

  new(model)
end

.listEnumerable<Coinbase::Webhook>

Enumerates the webhooks. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…

Returns:



57
58
59
60
61
# File 'lib/coinbase/webhook.rb', line 57

def list
  Coinbase::Pagination.enumerate(method(:fetch_webhooks_page).to_proc) do |webhook|
    Coinbase::Webhook.new(webhook)
  end
end

Instance Method Details

#deleteself

Deletes the webhook.

Examples:

Delete a webhook

webhook.delete

Returns:

  • (self)

    Returns the Webhook object with nil attributes.



156
157
158
159
160
161
162
163
164
# File 'lib/coinbase/webhook.rb', line 156

def delete
  Coinbase.call_api do
    webhooks_api.delete_webhook(id)
  end

  @model = nil

  self
end

#event_filtersArray<Coinbase::Client::WebhookEventFilter>

Returns the event filters applied to the webhook.

Returns:



115
116
117
# File 'lib/coinbase/webhook.rb', line 115

def event_filters
  @model.event_filters
end

#event_typeString

Returns the event type of the webhook.

Returns:

  • (String)

    The type of event the webhook listens for.



108
109
110
# File 'lib/coinbase/webhook.rb', line 108

def event_type
  @model.event_type
end

#idString

Returns the ID of the webhook.

Returns:

  • (String)

    The ID of the webhook.



87
88
89
# File 'lib/coinbase/webhook.rb', line 87

def id
  @model.id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Webhook



182
183
184
# File 'lib/coinbase/webhook.rb', line 182

def inspect
  to_s
end

#network_idSymbol

Returns the network ID associated with the webhook.

Returns:

  • (Symbol)

    The network ID of the webhook.



94
95
96
# File 'lib/coinbase/webhook.rb', line 94

def network_id
  Coinbase.to_sym(@model.network_id)
end

#notification_uriString

Returns the notification URI of the webhook.

Returns:

  • (String)

    The URI where notifications are sent.



101
102
103
# File 'lib/coinbase/webhook.rb', line 101

def notification_uri
  @model.notification_uri
end

#signature_headerString

Returns the signature header for the webhook. It is used as the value of callback header with key ‘x-webhook-signature’.

Returns:

  • (String)

    The signature header value.



123
124
125
# File 'lib/coinbase/webhook.rb', line 123

def signature_header
  @model.signature_header
end

#to_sString

Returns a String representation of the Webhook.

Returns:

  • (String)

    a String representation of the Webhook



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/coinbase/webhook.rb', line 168

def to_s
  Coinbase.pretty_print_object(
    self.class,
    id: @model.id,
    network_id: @model.network_id,
    event_type: @model.event_type,
    notification_uri: @model.notification_uri,
    event_filters: @model.event_filters.map(&:to_hash).to_json,
    signature_header: @model.signature_header
  )
end

#update(notification_uri:) ⇒ self

Updates the webhook with a new notification URI.

Examples:

Update the notification URI of a webhook

webhook.update(notification_uri: 'https://new-url.com/callback')

Parameters:

  • notification_uri (String)

    The new URI for webhook notifications.

Returns:

  • (self)

    Returns the updated Webhook object.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/coinbase/webhook.rb', line 134

def update(notification_uri:)
  model = Coinbase.call_api do
    webhooks_api.update_webhook(
      id,
      update_webhook_request: {
        notification_uri: notification_uri,
        event_filters: event_filters.map(&:to_hash)
      }
    )
  end

  @model = model

  self
end