class Backup::Notifier::Slack
Attributes
The channel to send messages to
The emoji icon to display along with the notification
See www.emoji-cheat-sheet.com for a list of icons.
Default: :floppy_disk:
Array of statuses for which the log file should be attached.
Available statuses are: `:success`, `:warning` and `:failure`. Default: [:warning, :failure]
The username to display along with the notification
The incoming webhook url
Public Class Methods
# File lib/backup/notifier/slack.rb, line 35 def initialize(model, &block) super instance_eval(&block) if block_given? @send_log_on ||= [:warning, :failure] @icon_emoji ||= ':floppy_disk:' end
Private Instance Methods
# File lib/backup/notifier/slack.rb, line 80 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
# 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
# File lib/backup/notifier/slack.rb, line 116 def log_field(status) send_log = send_log_on.include?(status) return { :title => "Detailed Backup Log", :value => Logger.messages.map(&:formatted_lines).flatten.join("\n"), :short => false, } if send_log end
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 62 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.merge!(:expects => 200) # raise error if unsuccessful Excon.post(uri, options) end
# 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
# File lib/backup/notifier/slack.rb, line 142 def uri @uri ||= webhook_url end