Class: Mihari::Emitters::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/mihari/emitters/slack.rb

Constant Summary collapse

DEFAULT_CHANNEL =
"#general"
DEFAULT_USERNAME =
"Mihari"

Constants included from Mixins::Retriable

Mixins::Retriable::DEFAULT_ON

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, #run

Methods included from Mixins::Retriable

#retry_on_error

Methods included from Mixins::Configurable

#configuration_keys, #configuration_values, #configured?

Constructor Details

#initialize(*args, **kwargs) ⇒ Slack

Returns a new instance of Slack.



124
125
126
127
128
129
130
# File 'lib/mihari/emitters/slack.rb', line 124

def initialize(*args, **kwargs)
  super(*args, **kwargs)

  @webhook_url = kwargs[:webhook_url] || Mihari.config.slack_webhook_url
  @channel = kwargs[:channel] || Mihari.config.slack_channel || DEFAULT_CHANNEL
  @username = DEFAULT_USERNAME
end

Instance Attribute Details

#channelString (readonly)

Returns:

  • (String)


119
120
121
# File 'lib/mihari/emitters/slack.rb', line 119

def channel
  @channel
end

#usernameString (readonly)

Returns:

  • (String)


122
123
124
# File 'lib/mihari/emitters/slack.rb', line 122

def username
  @username
end

#webhook_urlString? (readonly)

Returns:

  • (String, nil)


116
117
118
# File 'lib/mihari/emitters/slack.rb', line 116

def webhook_url
  @webhook_url
end

Instance Method Details

#emit(title:, description:, artifacts:, tags: [], **_options) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/mihari/emitters/slack.rb', line 186

def emit(title:, description:, artifacts:, tags: [], **_options)
  return if artifacts.empty?

  attachments = to_attachments(artifacts)
  text = to_text(title: title, description: description, tags: tags)

  notifier.post(text: text, attachments: attachments, mrkdwn: true)
end

#notifierObject



150
151
152
# File 'lib/mihari/emitters/slack.rb', line 150

def notifier
  @notifier ||= ::Slack::Notifier.new(webhook_url, channel: channel, username: username)
end

#to_attachments(artifacts) ⇒ Array<Mihari::Emitters::Attachment>

Build attachements

Parameters:

Returns:



161
162
163
164
165
# File 'lib/mihari/emitters/slack.rb', line 161

def to_attachments(artifacts)
  artifacts.map do |artifact|
    Attachment.new(data: artifact.data, data_type: artifact.data_type).to_a
  end.flatten
end

#to_text(title:, description:, tags: []) ⇒ String

Build a text

Parameters:

  • title (String)
  • description (String)
  • tags (Array<String>) (defaults to: [])

Returns:

  • (String)


176
177
178
179
180
181
182
183
184
# File 'lib/mihari/emitters/slack.rb', line 176

def to_text(title:, description:, tags: [])
  tags = ["N/A"] if tags.empty?

  [
    "*#{title}*",
    "*Desc.*: #{description}",
    "*Tags*: #{tags.join(", ")}"
  ].join("\n")
end

#valid?Boolean

Check webhook URL is set. Alias of #webhook_url?

Returns:

  • (Boolean)


146
147
148
# File 'lib/mihari/emitters/slack.rb', line 146

def valid?
  webhook_url?
end

#webhook_url?Boolean

Check webhook URL is set

Returns:

  • (Boolean)


137
138
139
# File 'lib/mihari/emitters/slack.rb', line 137

def webhook_url?
  !webhook_url.nil?
end