Class: OopsieExceptions::WebhookClient

Inherits:
Object
  • Object
show all
Defined in:
lib/oopsie_exceptions/webhook_client.rb

Class Method Summary collapse

Class Method Details

.post(webhook, payload) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/oopsie_exceptions/webhook_client.rb', line 10

def post(webhook, payload)
  uri = URI.parse(webhook.url)
  config = OopsieExceptions.configuration

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = config.open_timeout
  http.read_timeout = config.timeout

  request = Net::HTTP::Post.new(uri.request_uri)
  request["Content-Type"] = "application/json"
  request["User-Agent"] = "OopsieExceptions/#{VERSION}"

  webhook.headers.each { |k, v| request[k] = v }

  request.body = payload.is_a?(String) ? payload : payload.to_json

  response = http.request(request)

  unless response.is_a?(Net::HTTPSuccess)
    log(:warn, "Webhook #{webhook.name} responded #{response.code}: #{response.body.to_s[0, 500]}")
  end

  response
rescue => e
  log(:error, "Failed to deliver to #{webhook.name}: #{e.message}")
  nil
end