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

Returns a new instance of SlackNotifier.

Parameters:

  • webhook (String) (defaults to: nil)

    Web Hook URL

  • channel (String) (defaults to: nil)

    チャンネル名

  • username (String) (defaults to: nil)

    ユーザー名



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

エラーログを送信

Parameters:

  • err (StandardError)

    エラーオブジェクト

  • channel (String) (defaults to: nil)

    チャンネル名

  • username (String) (defaults to: nil)

    ユーザー名

  • webhook (String) (defaults to: nil)

    Web Hook URL



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

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

Parameters:

  • message (String)

    送信メッセージ

  • channel (String) (defaults to: nil)

    チャンネル名

  • username (String) (defaults to: nil)

    ユーザー名

  • webhook (String) (defaults to: nil)

    Web Hook URL

  • at (String) (defaults to: nil)

    メンション先



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

エラーログを送信

Parameters:

  • err (StandardError)

    エラーオブジェクト



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

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

Parameters:

  • message (String)

    送信メッセージ

  • at (String) (defaults to: nil)

    メンション先



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

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