Sha256: 387eb3b42c28fd8b9e7a46ee5337c10aa2e6a578f39383fbf4d7c886548bda94

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module FDE
  module Slack
    class Message
      BLUE = '#BDE5F8'.freeze
      GREEN = '#DFF2BF'.freeze
      YELLOW = '#FEEFB3'.freeze
      RED = '#FFBABA'.freeze

      attr_accessor :username,
        :title,
        :author,
        :fields,
        :footer,
        :color

      def initialize(title, fields, options = {})
        @title = title
        @fields = fields
        if options[:username]
          @username = options[:username] || 'FDE Slack Notifier'
        end
        if options[:author]
          @author = options[:author]
        end
        if options[:footer]
          @footer = options[:footer]
        end
      end

      def info(channel)
        @color = BLUE
        send(channel)
      end

      def success(channel)
        @color = GREEN
        send(channel)
      end

      def warning(channel)
        @color = YELLOW
        send(channel)
      end

      def error(channel)
        @color = RED
        send(channel)
      end

      def add_field(field)
        @fields << field.to_h
      end

      private

      def send(channel)
        notifier = ::Slack::Notifier.new(
          FDE::Slack::Notification.config.webhook,
          channel: channel,
          username: @username
        )
        notifier.ping message_hash
      end

      def message_hash
        { attachments: [ attachment_hash ] }
      end

      def attachment_hash
        hash = {
          fallback: @title,
          ts: Time.now.to_i,
          color: @color,
          fields: @fields
        }
        hash.merge!(@author.to_h)
        hash.merge!(@footer.to_h)
        hash
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fde-slack-notification-0.2.0 lib/slack/message.rb