Sha256: 53300b54f8ae5bf4dc022e7a909c284ac777c5c0e4489ec0e2f36fad253f056f

Contents?: true

Size: 1.35 KB

Versions: 11

Compression:

Stored size: 1.35 KB

Contents

module Notification
    class Webhook
        require 'uri'
        require 'net/http'
        require 'net/https'
        require 'openssl'
        require 'json'

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

        def backup_notification(result, date, database, backup_path)
            return if @webhook_url.nil? || @secret.nil?
            
            data = {
                domain: ENV['DEFAULT_URL'] || "#{database} Backup",
                backupPath: result ? backup_path : nil,
                backupDate: date,
            }.to_json
            
            uri = URI.parse(@webhook_url)
            https = Net::HTTP.new(uri.host, uri.port)
            https.use_ssl = uri.scheme == "https"
            request = Net::HTTP::Post.new(uri.path.empty? ? "/" : uri.path, initHeader = {'Content-Type' =>'application/json', 'x-hub-signature' => generate_signature("#{data}")})
            request.body = "#{data}"
            begin
                response = https.request(request)
                response.to_hash
            rescue => e
                puts "Webhook error: \n\t#{e.message}"
            end
        end
    end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
capistrano-ops-0.2.0 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.9 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.8 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.7 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.6 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.5 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.4 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.3 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.2 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.1 lib/capistrano/ops/notification/webhook.rb
capistrano-ops-0.1.0 lib/capistrano/ops/notification/webhook.rb