Class: NoopBackup::Notifiers::Slack

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#webhook_urlObject

Returns the value of attribute webhook_url.



7
8
9
# File 'lib/noop_backup/notifiers/slack.rb', line 7

def webhook_url
  @webhook_url
end

Instance Method Details

#notify(text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/noop_backup/notifiers/slack.rb', line 9

def notify(text)
  uri = URI(webhook_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
  request.body = {text: text}.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