Sha256: 6ab6bf157e4a491a63e46c53e1fb86fe2103b3c6af02ec09f5f41e56490b9313

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'net/http'
require 'json'
require 'uri'

module PubsubNotifier
  class SlackClient < ::PubsubNotifier::Client::Base
    attr_reader :channel, :username, :icon_emoji

    def initialize(options = {})
      @channel    = options.delete(:channel)    || default_channel
      @username   = options.delete(:username)   || default_username
      @icon_emoji = options.delete(:icon_emoji) || default_icon_emoji
    end

    def notify_success(message)
      post_slack(
        attachments: [{
          text:      message,
          color:     'good',
          mrkdwn_in: ['text'],
        }],
      )
    end

    def notify_failure(message)
      post_slack(
        attachments: [{
          text:      message,
          color:     'danger',
          mrkdwn_in: ['text'],
        }],
      )
    end

    private

      def post_slack(payload)
        Net::HTTP.post_form(
          end_point_uri,
          payload: default_payload.merge(payload).to_json,
        )
      end

      def end_point_uri
        @end_point_uri ||= URI.parse(webhook_url)
      end

      def default_payload
        {
          channel:    channel,
          link_names: 1,
          username:   username,
          icon_emoji: icon_emoji,
        }
      end

      def default_channel
        config.default_channel
      end

      def default_username
        config.default_username
      end

      def default_icon_emoji
        config.default_icon_emoji
      end

      def webhook_url
        config.webhook_url
      end

    class Config
      attr_accessor :default_channel, :default_username, :default_icon_emoji, :webhook_url

      def initialize
        @default_channel    = ENV['SLACK_DEFAULT_CHANNEL']
        @default_username   = ENV['SLACK_DEFAULT_USERNAME']
        @default_icon_emoji = ENV['SLACK_DEFAULT_ICON_EMOJI']
        @webhook_url        = ENV['SLACK_WEBHOOK_URL']
      end
    end
  end
end

PubsubNotifier.register_client :slack, PubsubNotifier::SlackClient

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pubsub_notifier-0.1.0 lib/pubsub_notifier/slack_client.rb