Class: HookSniff::WebhooksResource

Inherits:
Object
  • Object
show all
Defined in:
lib/hooksniff/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ WebhooksResource

Returns a new instance of WebhooksResource.



134
135
136
# File 'lib/hooksniff/client.rb', line 134

def initialize(client)
  @client = client
end

Instance Method Details

#attempts(delivery_id) ⇒ Object

Get delivery attempts



181
182
183
184
# File 'lib/hooksniff/client.rb', line 181

def attempts(delivery_id)
  resp = @client.request(:get, "/webhooks/#{delivery_id}/attempts")
  resp.map { |a| Models::DeliveryAttempt.new(a) }
end

#batch(webhooks) ⇒ Object

Send multiple webhooks in a batch



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

def batch(webhooks)
  body = {
    webhooks: webhooks.map do |w|
      item = { endpoint_id: w[:endpoint_id], data: w[:data] }
      item[:event] = w[:event] if w[:event]
      item
    end
  }
  resp = @client.request(:post, "/webhooks/batch", body: body)
  Models::BatchResult.new(resp)
end

#export(format: nil, status: nil, date_from: nil, date_to: nil) ⇒ Object

Export deliveries



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/hooksniff/client.rb', line 187

def export(format: nil, status: nil, date_from: nil, date_to: nil)
  params = {}
  params[:format] = format if format
  params[:status] = status if status
  params[:date_from] = date_from if date_from
  params[:date_to] = date_to if date_to
  query = URI.encode_www_form(params)
  query = "?#{query}" unless query.empty?

  resp = @client.request(:get, "/webhooks/export#{query}")
  return resp if format == "csv"

  resp.map { |d| Models::Delivery.new(d) }
end

#get(delivery_id) ⇒ Object

Get a delivery by ID



147
148
149
150
# File 'lib/hooksniff/client.rb', line 147

def get(delivery_id)
  resp = @client.request(:get, "/webhooks/#{delivery_id}")
  Models::Delivery.new(resp)
end

#list(status: nil, page: 1, per_page: 20) ⇒ Object

List deliveries with optional filters



153
154
155
156
157
158
159
# File 'lib/hooksniff/client.rb', line 153

def list(status: nil, page: 1, per_page: 20)
  params = { page: page.to_s, per_page: per_page.to_s }
  params[:status] = status if status
  query = URI.encode_www_form(params)
  resp = @client.request(:get, "/webhooks?#{query}")
  Models::DeliveryList.new(resp)
end

#replay(delivery_id) ⇒ Object

Replay a delivery



162
163
164
165
# File 'lib/hooksniff/client.rb', line 162

def replay(delivery_id)
  resp = @client.request(:post, "/webhooks/#{delivery_id}/replay")
  Models::Delivery.new(resp)
end

#search(query: nil, event: nil, status: nil, endpoint_id: nil, page: 1, per_page: 20) ⇒ Object

Search deliveries with filters



203
204
205
206
207
208
209
210
211
# File 'lib/hooksniff/client.rb', line 203

def search(query: nil, event: nil, status: nil, endpoint_id: nil, page: 1, per_page: 20)
  params = { page: page.to_s, per_page: per_page.to_s }
  params[:q] = query if query
  params[:event] = event if event
  params[:status] = status if status
  params[:endpoint_id] = endpoint_id if endpoint_id
  query_str = URI.encode_www_form(params)
  @client.request(:get, "/search?#{query_str}")
end

#send(endpoint_id:, event: nil, data:) ⇒ Object

Send a webhook



139
140
141
142
143
144
# File 'lib/hooksniff/client.rb', line 139

def send(endpoint_id:, event: nil, data:)
  body = { endpoint_id: endpoint_id, data: data }
  body[:event] = event if event
  resp = @client.request(:post, "/webhooks", body: body)
  Models::Delivery.new(resp)
end