Class: Webhooks::Outgoing::DeliveryAttempt

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/webhooks/outgoing/delivery_attempt.rb

Instance Method Summary collapse

Instance Method Details

#attemptObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/webhooks/outgoing/delivery_attempt.rb', line 24

def attempt
  uri = URI.parse(delivery.endpoint_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  request = Net::HTTP::Post.new(uri.path)
  request.add_field("Content-Type", "application/json")
  request.body = delivery.event.payload.to_json

  begin
    response = http.request(request)
    self.response_message = response.message
    self.response_code = response.code
    self.response_body = response.body
  rescue Exception => exception
    self.response_code = 0
    self.error_message = exception.message
  end

  save
  successful?
end

#failed?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/webhooks/outgoing/delivery_attempt.rb', line 20

def failed?
  !(successful? || still_attempting?)
end

#label_stringObject



46
47
48
# File 'app/models/webhooks/outgoing/delivery_attempt.rb', line 46

def label_string
  "#{attempt_number.ordinalize} Attempt"
end

#still_attempting?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/webhooks/outgoing/delivery_attempt.rb', line 12

def still_attempting?
  error_message.nil? && response_code.nil?
end

#successful?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/webhooks/outgoing/delivery_attempt.rb', line 16

def successful?
  [200, 201, 202, 203, 204, 205, 206, 207, 226].include?(response_code)
end