class SlackNotifier::Message

Public Class Methods

compose(text:, channel: nil, nickname: nil, icon_emoji: nil, report: nil, report_title: nil, report_color: nil) click to toggle source
# File lib/slack_notifier/message.rb, line 3
def self.compose(text:, channel: nil, nickname: nil, icon_emoji: nil, report: nil, report_title: nil, report_color: nil)
  channel ||= Config.default_channel
  nickname ||= Config.default_nickname
  icon_emoji ||= Config.default_icon_emoji
  report_color ||= Config.default_report_color
  report_title ||= Config.default_report_title
  new(text, channel, nickname, icon_emoji, report, report_title, report_color)
end
new(text, channel, nickname, icon, report, report_title, report_color) click to toggle source
# File lib/slack_notifier/message.rb, line 40
def initialize(text, channel, nickname, icon, report, report_title, report_color)
  @text = text
  @channel = channel
  @nickname = nickname
  @icon_emoji = icon
  @report = report
  @report_title = report_title
  @report_color = report_color
  @created_at = Time.now
end
send(text:, channel: nil, nickname: nil, icon_emoji: nil, report: nil, report_title: nil, report_color: nil) click to toggle source
# File lib/slack_notifier/message.rb, line 12
def self.send(text:, channel: nil, nickname: nil, icon_emoji: nil, report: nil, report_title: nil, report_color: nil)
  compose(
    text: text,
    channel: channel,
    nickname: nickname,
    icon_emoji: icon_emoji,
    report: report,
    report_title: report_title,
    report_color: report_color
  ).tap(&:deliver)
rescue => ex
  raise ex if Config.raise_delivery_errors
  puts ex.message
  puts ex.backtrace.join("\n")
end

Public Instance Methods

deliver() click to toggle source
# File lib/slack_notifier/message.rb, line 28
def deliver
  cmd = %(curl -X POST --data-urlencode 'payload=#{payload}' #{Config.webhook_url})
  puts "Executing: `#{cmd}`"

  result = `#{cmd}`
  puts "Result: #{result}"

  Time.now
end

Private Instance Methods

attachment_param() click to toggle source
# File lib/slack_notifier/message.rb, line 70
def attachment_param
  return {} if @report.nil? || @report.empty?
  {
      attachments: [
          {
              title: @report_title,
              color: @report_color,
              text: key_value_pairs_to_s(@report)
          }
      ]
  }
end
base_param() click to toggle source
# File lib/slack_notifier/message.rb, line 58
def base_param
  {
      username: @nickname,
      text: @text.gsub('\'', '').gsub('`', ''),
      channel: @channel
  }
end
icon_param() click to toggle source
# File lib/slack_notifier/message.rb, line 66
def icon_param
  { icon_emoji: @icon_emoji }
end
key_value_pairs_to_s(hash) click to toggle source
# File lib/slack_notifier/message.rb, line 83
def key_value_pairs_to_s(hash)
  hash.map do |k, v|
    "#{k.to_s.gsub(/_/, ' ').titleize}: #{v.to_s.gsub('\'', '').gsub('`', '')}"
  end.join("\n")
end
payload() click to toggle source
# File lib/slack_notifier/message.rb, line 51
def payload
  payload_hash = base_param
  payload_hash.merge!(icon_param)
  payload_hash.merge!(attachment_param)
  payload_hash.to_json
end