1 2 3 4 5 6 7 8 9 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 38 39 40 41 42 43 44 45 46 47 48 49
|
# frozen_string_literal: true
class UniformNotifier
class Slack < Base
POSSIBLE_OPTIONS = %i[username channel].freeze
class << self
@slack = nil
def active?
@slack
end
def setup_connection(config = {})
webhook_url, options = parse_config(config)
fail_connection('webhook_url required for Slack notification') unless webhook_url
require 'slack-notifier'
@slack = ::Slack::Notifier.new webhook_url, options
rescue LoadError
fail_connection 'You must install the slack-notifier gem to use Slack notification: `gem install slack-notifier`'
end
protected
def _out_of_channel_notify(data)
message = data.values.compact.join("\n")
notify(message)
end
private
def fail_connection(message)
@slack = nil
raise NotificationError, message
end
def notify(message)
@slack.ping message
end
def parse_config(config)
options = config.select { |name, value| POSSIBLE_OPTIONS.include?(name) && !value.nil? }
[config[:webhook_url], options]
end
end
end
end
|