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.



74
75
76
77
78
# File 'lib/coinbase/webhook.rb', line 74

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:) ⇒ 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...' }]
)

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`.

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/coinbase/webhook.rb', line 34

def create(network_id:, notification_uri:, event_type:, event_filters:)
  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
      }
    )
  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:



53
54
55
56
57
# File 'lib/coinbase/webhook.rb', line 53

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.



152
153
154
155
156
157
158
159
160
# File 'lib/coinbase/webhook.rb', line 152

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:



111
112
113
# File 'lib/coinbase/webhook.rb', line 111

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.



104
105
106
# File 'lib/coinbase/webhook.rb', line 104

def event_type
  @model.event_type
end

#idString

Returns the ID of the webhook.

Returns:

  • (String)

    The ID of the webhook.



83
84
85
# File 'lib/coinbase/webhook.rb', line 83

def id
  @model.id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Webhook



178
179
180
# File 'lib/coinbase/webhook.rb', line 178

def inspect
  to_s
end

#network_idSymbol

Returns the network ID associated with the webhook.

Returns:

  • (Symbol)

    The network ID of the webhook.



90
91
92
# File 'lib/coinbase/webhook.rb', line 90

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.



97
98
99
# File 'lib/coinbase/webhook.rb', line 97

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.



119
120
121
# File 'lib/coinbase/webhook.rb', line 119

def signature_header
  @model.signature_header
end

#to_sString

Returns a String representation of the Webhook.

Returns:

  • (String)

    a String representation of the Webhook



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/coinbase/webhook.rb', line 164

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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/coinbase/webhook.rb', line 130

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