Class: Spree::Webhooks::DeliverWebhook

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/webhooks/deliver_webhook.rb

Constant Summary collapse

TIMEOUT =
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delivery:, secret_key:) ⇒ DeliverWebhook



15
16
17
18
# File 'app/services/spree/webhooks/deliver_webhook.rb', line 15

def initialize(delivery:, secret_key:)
  @delivery = delivery
  @secret_key = secret_key
end

Class Method Details

.call(delivery:, secret_key:) ⇒ Object



11
12
13
# File 'app/services/spree/webhooks/deliver_webhook.rb', line 11

def self.call(delivery:, secret_key:)
  new(delivery: delivery, secret_key: secret_key).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/spree/webhooks/deliver_webhook.rb', line 20

def call
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  response = make_request
  execution_time = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round

  @delivery.complete!(
    response_code: response.code.to_i,
    execution_time: execution_time,
    response_body: response.body.to_s.truncate(10_000)
  )
rescue Net::OpenTimeout, Net::ReadTimeout => e
  execution_time = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round
  Rails.error.report(e, context: { webhook_delivery_id: @delivery.id, url: @delivery.url })
  @delivery.complete!(
    execution_time: execution_time,
    error_type: 'timeout',
    request_errors: e.message
  )
rescue StandardError => e
  execution_time = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round
  Rails.error.report(e, context: { webhook_delivery_id: @delivery.id, url: @delivery.url })
  @delivery.complete!(
    execution_time: execution_time,
    error_type: 'connection_error',
    request_errors: e.message
  )
end