class Backup::Notifier::Slack

Attributes

channel[RW]

The channel to send messages to

icon_emoji[RW]

The emoji icon to display along with the notification

See www.emoji-cheat-sheet.com for a list of icons.

Default: :floppy_disk:

send_log_on[RW]

Array of statuses for which the log file should be attached.

Available statuses are: ‘:success`, `:warning` and `:failure`. Default: [:warning, :failure]

username[RW]

The username to display along with the notification

webhook_url[RW]

The incoming webhook url

Public Class Methods

new(model, &block) click to toggle source
Calls superclass method Backup::Notifier::Base::new
# File lib/backup/notifier/slack.rb, line 34
def initialize(model, &block)
  super
  instance_eval(&block) if block_given?

  @send_log_on ||= [:warning, :failure]
  @icon_emoji  ||= ":floppy_disk:"
end

Private Instance Methods

attachment(status) click to toggle source
# File lib/backup/notifier/slack.rb, line 79
def attachment(status)
  {
    fallback: "#{title(status)} - Job: #{model.label} (#{model.trigger})",
    text: title(status),
    color: color(status),
    fields: [
      {
        title: "Job",
        value: "#{model.label} (#{model.trigger})",
        short: false
      },
      {
        title: "Started",
        value: model.started_at,
        short: true
      },
      {
        title: "Finished",
        value: model.finished_at,
        short: true
      },
      {
        title: "Duration",
        value: model.duration,
        short: true
      },
      {
        title: "Version",
        value: "Backup v#{Backup::VERSION}\nRuby: #{RUBY_DESCRIPTION}",
        short: false
      },
      log_field(status)
    ].compact
  }
end
color(status) click to toggle source
# File lib/backup/notifier/slack.rb, line 126
def color(status)
  case status
  when :success then "good"
  when :failure then "danger"
  when :warning then "warning"
  end
end
log_field(status) click to toggle source
# File lib/backup/notifier/slack.rb, line 115
def log_field(status)
  send_log = send_log_on.include?(status)
  return unless send_log

  {
    title: "Detailed Backup Log",
    value: Logger.messages.map(&:formatted_lines).flatten.join("\n"),
    short: false
  }
end
notify!(status) click to toggle source

Notify the user of the backup operation results.

‘status` indicates one of the following:

‘:success` : The backup completed successfully. : Notification will be sent if `on_success` is `true`.

‘:warning` : The backup completed successfully, but warnings were logged. : Notification will be sent if `on_warning` or `on_success` is `true`.

‘:failure` : The backup operation failed. : Notification will be sent if `on_warning` or `on_success` is `true`.

# File lib/backup/notifier/slack.rb, line 61
def notify!(status)
  data = {
    text: message.call(model, status: status_data_for(status)),
    attachments: [attachment(status)]
  }
  [:channel, :username, :icon_emoji].each do |param|
    val = send(param)
    data.merge!(param => val) if val
  end

  options = {
    headers: { "Content-Type" => "application/x-www-form-urlencoded" },
    body: URI.encode_www_form(payload: JSON.dump(data))
  }
  options[:expects] = 200 # raise error if unsuccessful
  Excon.post(uri, options)
end
title(status) click to toggle source
# File lib/backup/notifier/slack.rb, line 134
def title(status)
  case status
  when :success then "Backup Completed Successfully!"
  when :failure then "Backup Failed!"
  when :warning then "Backup Completed Successfully (with Warnings)!"
  end
end
uri() click to toggle source
# File lib/backup/notifier/slack.rb, line 142
def uri
  @uri ||= webhook_url
end