Class: DataChecks::Notifiers::SlackNotifier

Inherits:
Notifier
  • Object
show all
Defined in:
lib/data_checks/notifiers/slack_notifier.rb

Instance Attribute Summary

Attributes inherited from Notifier

#options

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SlackNotifier

Returns a new instance of SlackNotifier.



8
9
10
11
12
# File 'lib/data_checks/notifiers/slack_notifier.rb', line 8

def initialize(options)
  super
  @formatter_class = options.delete(:formatter_class) || SlackDefaultFormatter
  @webhook_url = options.fetch(:webhook_url) { raise ArgumentError, "webhook_url must be configured" }
end

Instance Method Details

#notify(check_result) ⇒ Object



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

def notify(check_result)
  formatter = @formatter_class.new(check_result)

  payload = {
    attachments: [
      {
        title: formatter.title,
        text: formatter.text,
        color: formatter.color,
      },
    ],
  }

  response = post(payload)
  unless response.is_a?(Net::HTTPSuccess) && response.body == "ok"
    raise "Failed to notify slack: #{response.body.inspect}"
  end
end