Sha256: 4191d330403c4453a7f2806e3b7bdfd6830901830135a3874c4c6a7dcbc709bd

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

# Send a notice to a webhook.
#
# url    - The String webhook URL.
# format - The Symbol format [ :form | :json ] (default: :form).

require 'net/http'
require 'uri'

CONTACT_DEPS[:webhook] = ['json']
CONTACT_DEPS[:webhook].each do |d|
  require d
end

module God
  module Contacts

    class Webhook < Contact
      class << self
        attr_accessor :url, :format
      end

      self.format = :form

      def valid?
        valid = true
        valid &= complain("Attribute 'url' must be specified", self) unless arg(:url)
        valid &= complain("Attribute 'format' must be one of [ :form | :json ]", self) unless [:form, :json].include?(arg(:format))
        valid
      end

      attr_accessor :url, :format

      def notify(message, time, priority, category, host)
        data = {
          :message => message,
          :time => time,
          :priority => priority,
          :category => category,
          :host => host
        }

        uri = URI.parse(arg(:url))
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true if uri.scheme == "https"

        req = nil
        res = nil

        case arg(:format)
          when :form
            req = Net::HTTP::Post.new(uri.request_uri)
            req.set_form_data(data)
          when :json
            req = Net::HTTP::Post.new(uri.request_uri)
            req.body = data.to_json
        end

        res = http.request(req)

        case res
          when Net::HTTPSuccess
            self.info = "sent webhook to #{arg(:url)}"
          else
            self.info = "failed to send webhook to #{arg(:url)}: #{res.error!}"
        end
      rescue Object => e
        applog(nil, :info, "failed to send webhook to #{arg(:url)}: #{e.message}")
        applog(nil, :debug, e.backtrace.join("\n"))
      end

    end

  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
resurrected_god-0.14.0 lib/god/contacts/webhook.rb
mcproc-2016.2.20 lib/god/contacts/webhook.rb
god-0.13.7 lib/god/contacts/webhook.rb
god-0.13.6 lib/god/contacts/webhook.rb
god-0.13.5 lib/god/contacts/webhook.rb