Class: Notification::Webhook

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/ops/rails/lib/notification/webhook.rb

Instance Method Summary collapse

Constructor Details

#initializeWebhook

Returns a new instance of Webhook.



9
10
11
12
13
14
15
# File 'lib/capistrano/ops/rails/lib/notification/webhook.rb', line 9

def initialize
  @webhook_url = ENV['WEBHOOK_URL']
  @secret = ENV['WEBHOOK_SECRET']
  @conn = Faraday.new(url: @webhook_url) do |faraday|
    faraday.headers['Content-Type'] = 'application/json'
  end
end

Instance Method Details

#backup_notification(result, webhook_data, _notification_level) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/capistrano/ops/rails/lib/notification/webhook.rb', line 21

def backup_notification(result, webhook_data, _notification_level)
  return if @webhook_url.nil? || @secret.nil?
  return if result && notification_level == 'error'

  @date = webhook_data[:date]
  @database = webhook_data[:database]
  @backup_path = webhook_data[:backup_path]

  @data = {
    domain: ENV['DEFAULT_URL'] || "#{@database} Backup",
    backupPath: result ? @backup_path : nil,
    backupDate: @date
  }.to_json

  begin
    @response = @conn.post do |req|
      req.headers['x-hub-signature'] = generate_signature(@data.to_s)
      req.body = @data
    end

    @response.to_hash
  rescue StandardError => e
    puts "Webhook error: \n\t#{e.message}"
  end
end

#generate_signature(payload_body) ⇒ Object



17
18
19
# File 'lib/capistrano/ops/rails/lib/notification/webhook.rb', line 17

def generate_signature(payload_body)
  "md5=#{OpenSSL::HMAC.hexdigest('md5', ENV['WEBHOOK_SECRET'], payload_body)}"
end