Class: MocaRlibs::SlackNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/moca_rlibs/slack_notifier.rb

Overview

Slack通知クライアント

Constant Summary collapse

SLACK_WEBHOOK_URL =
ENV["#{PREFIX}_SLACK_WEBHOOK_URL"]
SLACK_DEFAULT_CHANNEL =
ENV["#{PREFIX}_SLACK_DEFAULT_CHANNEL"]
SLACK_DEFAULT_USER =
ENV["#{PREFIX}_SLACK_DEFAULT_USER"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webhook: nil, channel: nil, username: nil) ⇒ SlackNotifier



23
24
25
26
27
28
29
# File 'lib/moca_rlibs/slack_notifier.rb', line 23

def initialize(webhook: nil, channel: nil, username: nil)
  @client = Slack::Notifier.new(
    webhook || SLACK_WEBHOOK_URL,
    channel:  channel  || SLACK_DEFAULT_CHANNEL,
    username: username || SLACK_DEFAULT_USER
  )
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/moca_rlibs/slack_notifier.rb', line 14

def client
  @client
end

Class Method Details

.error(err, channel: nil, username: nil, webhook: nil) ⇒ Object

エラーログを送信



64
65
66
67
68
# File 'lib/moca_rlibs/slack_notifier.rb', line 64

def self.error(err, channel: nil, username: nil, webhook: nil)
  backtrace = (err.backtrace&.join || 'no backtrace info').sub('`', '\'').slice(0, 2000)
  new(webhook: webhook, channel: channel, username: username)
    .send("#{err.message}\n```\n#{backtrace}\n```", at: 'here')
end

.send(message, channel: nil, username: nil, webhook: nil, at: nil) ⇒ Object

シンプルなテキストを送信する



53
54
55
56
# File 'lib/moca_rlibs/slack_notifier.rb', line 53

def self.send(message, channel: nil, username: nil, webhook: nil, at: nil)
  new(webhook: webhook, channel: channel, username: username)
    .send(message, at: at)
end

Instance Method Details

#error(err) ⇒ Object

エラーログを送信



42
43
44
# File 'lib/moca_rlibs/slack_notifier.rb', line 42

def error(err)
  send("#{err.message}\n```#{err.backtrace}```", at: 'here')
end

#send(message, at: nil) ⇒ Object

シンプルなテキストを送信する



35
36
37
# File 'lib/moca_rlibs/slack_notifier.rb', line 35

def send(message, at: nil)
  @client.ping("#{at ? "<!#{at}> " : ''}#{message}")
end