Class: Mihari::Emitters::Slack
- Defined in:
- lib/mihari/emitters/slack.rb
Constant Summary collapse
- DEFAULT_CHANNEL =
"#general"- DEFAULT_USERNAME =
"Mihari"
Constants included from Mixins::Retriable
Instance Attribute Summary collapse
- #channel ⇒ String readonly
- #username ⇒ String readonly
- #webhook_url ⇒ String? readonly
Instance Method Summary collapse
- #emit(title:, description:, artifacts:, tags: [], **_options) ⇒ Object
-
#initialize(*args, **kwargs) ⇒ Slack
constructor
A new instance of Slack.
- #notifier ⇒ Object
-
#to_attachments(artifacts) ⇒ Array<Mihari::Emitters::Attachment>
Build attachements.
-
#to_text(title:, description:, tags: []) ⇒ String
Build a text.
-
#valid? ⇒ Boolean
Check webhook URL is set.
-
#webhook_url? ⇒ Boolean
Check webhook URL is set.
Methods inherited from Base
Methods included from Mixins::Retriable
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
#channel ⇒ String (readonly)
119 120 121 |
# File 'lib/mihari/emitters/slack.rb', line 119 def channel @channel end |
#username ⇒ String (readonly)
122 123 124 |
# File 'lib/mihari/emitters/slack.rb', line 122 def username @username end |
#webhook_url ⇒ String? (readonly)
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: [], **) return if artifacts.empty? = (artifacts) text = to_text(title: title, description: description, tags: ) notifier.post(text: text, attachments: , mrkdwn: true) end |
#notifier ⇒ Object
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
161 162 163 164 165 |
# File 'lib/mihari/emitters/slack.rb', line 161 def (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
176 177 178 179 180 181 182 183 184 |
# File 'lib/mihari/emitters/slack.rb', line 176 def to_text(title:, description:, tags: []) = ["N/A"] if .empty? [ "*#{title}*", "*Desc.*: #{description}", "*Tags*: #{tags.join(", ")}" ].join("\n") end |
#valid? ⇒ Boolean
Check webhook URL is set. Alias of #webhook_url?
146 147 148 |
# File 'lib/mihari/emitters/slack.rb', line 146 def valid? webhook_url? end |
#webhook_url? ⇒ Boolean
Check webhook URL is set
137 138 139 |
# File 'lib/mihari/emitters/slack.rb', line 137 def webhook_url? !webhook_url.nil? end |