Sha256: 13d92f1b361ec7857995235e3a01781b80cce46261de6cc0494a95b5cd755d6b

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

class UniformNotifier
  class Slack < Base
    POSSIBLE_OPTIONS = %i[username channel].freeze

    @slack = nil

    class << self
      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 do |name, value|
          POSSIBLE_OPTIONS.include?(name) && !value.nil?
        end

        [config[:webhook_url], options]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uniform_notifier-1.13.0 lib/uniform_notifier/slack.rb
uniform_notifier-1.12.1 lib/uniform_notifier/slack.rb
uniform_notifier-1.12.0 lib/uniform_notifier/slack.rb