Class: Boppers::Notifier::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/boppers/notifier/slack.rb

Constant Summary collapse

COLORS =
{
  green: "good",
  red: "danger"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token:, channel:, subscribe: nil) ⇒ Slack

Returns a new instance of Slack.



13
14
15
16
17
# File 'lib/boppers/notifier/slack.rb', line 13

def initialize(api_token:, channel:, subscribe: nil)
  @api_token = api_token
  @channel = channel
  @subscribe = subscribe
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



11
12
13
# File 'lib/boppers/notifier/slack.rb', line 11

def api_token
  @api_token
end

#channelObject (readonly)

Returns the value of attribute channel.



11
12
13
# File 'lib/boppers/notifier/slack.rb', line 11

def channel
  @channel
end

#subscribeObject (readonly)

Returns the value of attribute subscribe.



11
12
13
# File 'lib/boppers/notifier/slack.rb', line 11

def subscribe
  @subscribe
end

Instance Method Details

#call(subject, message, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/boppers/notifier/slack.rb', line 19

def call(subject, message, options)
  params = {
    token: api_token,
    text: "",
    channel: channel,
    attachments: JSON.dump(
      [
        {
          fallback: message,
          title: subject,
          text: message,
          color: COLORS.fetch(options[:color])
        }
      ]
    )
  }

  HttpClient.post("https://slack.com/api/chat.postMessage", params)
end