Class: BoringBackup::Notifiers::Slack

Inherits:
Object
  • Object
show all
Defined in:
lib/boring_backup/notifiers/slack.rb

Constant Summary collapse

OPEN_TIMEOUT =
5
READ_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#webhook_urlObject

Returns the value of attribute webhook_url.



10
11
12
# File 'lib/boring_backup/notifiers/slack.rb', line 10

def webhook_url
  @webhook_url
end

Instance Method Details

#notify(result) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/boring_backup/notifiers/slack.rb', line 12

def notify(result)
  uri = URI(webhook_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.open_timeout = OPEN_TIMEOUT
  http.read_timeout = READ_TIMEOUT

  request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json")
  request.body = {text: result.messages.join("\n")}.to_json

  response = http.request(request)

  success = response.is_a?(Net::HTTPSuccess)

  warn "Slack notify failed: #{response.code} #{response.body}" unless success

  success
rescue => e
  # Never fail a backup because of a notification error
  warn "Slack notify error: #{e.message}"
end