Sha256: 30afc4f8ee2240a18decd11f75748af4d9359a761a9f20b96d42de20ebd3ff63

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

# 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
uniform_notifier-1.16.0 lib/uniform_notifier/slack.rb
uniform_notifier-1.15.0 lib/uniform_notifier/slack.rb
uniform_notifier-1.14.2 lib/uniform_notifier/slack.rb
uniform_notifier-1.14.1 lib/uniform_notifier/slack.rb