Sha256: 1f89836699ea8fb7a0e79eb608e3a12c26ba02cab9da81e60defd1cdddbc9a0e

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 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

2 entries across 2 versions & 1 rubygems

Version Path
pubsub_notifier-0.1.2 lib/pubsub_notifier/slack_client.rb
pubsub_notifier-0.1.1 lib/pubsub_notifier/slack_client.rb