Class: God::Contacts::Webhook

Inherits:
God::Contact show all
Defined in:
lib/god/contacts/webhook.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from God::Contact

#group, #info, #name

Instance Method Summary collapse

Methods inherited from God::Contact

#arg, defaults, #friendly_name, generate, normalize, valid?

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name, #prepare, #reset

Class Attribute Details

.formatObject

Returns the value of attribute format



19
20
21
# File 'lib/god/contacts/webhook.rb', line 19

def format
  @format
end

.urlObject

Returns the value of attribute url



19
20
21
# File 'lib/god/contacts/webhook.rb', line 19

def url
  @url
end

Instance Attribute Details

#formatObject

Returns the value of attribute format



31
32
33
# File 'lib/god/contacts/webhook.rb', line 31

def format
  @format
end

#urlObject

Returns the value of attribute url



31
32
33
# File 'lib/god/contacts/webhook.rb', line 31

def url
  @url
end

Instance Method Details

#notify(message, time, priority, category, host) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/god/contacts/webhook.rb', line 33

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

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/god/contacts/webhook.rb', line 24

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