Class: Anomonitor::Notifiers::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/anomonitor/notifiers/webhook.rb

Instance Method Summary collapse

Constructor Details

#initialize(url: Anomonitor.config.webhook_url) ⇒ Webhook

Returns a new instance of Webhook.



10
11
12
# File 'lib/anomonitor/notifiers/webhook.rb', line 10

def initialize(url: Anomonitor.config.webhook_url)
  @url = url
end

Instance Method Details

#deliver(anomaly, event: DETECTED) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/anomonitor/notifiers/webhook.rb', line 14

def deliver(anomaly, event: DETECTED)
  return false if @url.nil? || @url.to_s.strip.empty?

  payload = build_payload(anomaly, event.to_s)
  response = post_json(payload)
  success = response.is_a?(Net::HTTPSuccess)

  unless success
    Anomonitor.logger.warn(
      "[Anomonitor] Webhook failed: HTTP #{response&.code} #{response&.body}"
    )
  end

  success
rescue StandardError => e
  Anomonitor.logger.warn("[Anomonitor] Webhook error: #{e.message}")
  false
end

#deliver_digest(payload) ⇒ Object



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

def deliver_digest(payload)
  return false if @url.nil? || @url.to_s.strip.empty?

  body =
    if slack_incoming_webhook?
      count = payload[:count] || payload["count"] || 0
      { text: "*Anomonitor* digest — #{count} anomalies <#{Anomonitor.config.dashboard_path}/anomalies>" }
    else
      payload
    end
  response = post_json(body)
  response.is_a?(Net::HTTPSuccess)
rescue StandardError => e
  Anomonitor.logger.warn("[Anomonitor] Digest webhook error: #{e.message}")
  false
end