Class: God::Contacts::Webhook
- Inherits:
-
God::Contact
- Object
- God::Contact
- God::Contacts::Webhook
- Defined in:
- lib/god/contacts/webhook.rb
Class Attribute Summary collapse
-
.format ⇒ Object
Returns the value of attribute format.
-
.url ⇒ Object
Returns the value of attribute url.
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#url ⇒ Object
Returns the value of attribute url.
Attributes inherited from God::Contact
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
.format ⇒ Object
Returns the value of attribute format.
19 20 21 |
# File 'lib/god/contacts/webhook.rb', line 19 def format @format end |
.url ⇒ Object
Returns the value of attribute url.
19 20 21 |
# File 'lib/god/contacts/webhook.rb', line 19 def url @url end |
Instance Attribute Details
#format ⇒ Object
Returns the value of attribute format.
31 32 33 |
# File 'lib/god/contacts/webhook.rb', line 31 def format @format end |
#url ⇒ Object
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 |
# File 'lib/god/contacts/webhook.rb', line 33 def notify(, time, priority, category, host) data = { :message => , :time => time, :priority => priority, :category => category, :host => host } uri = URI.parse(arg(:url)) http = Net::HTTP.new(uri.host, uri.port) req = nil res = nil case arg(:format) when :form req = Net::HTTP::Post.new(uri.path) req.set_form_data(data) when :json req = Net::HTTP::Post.new(uri.path) 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 email to #{arg(:url)}: #{e.}") applog(nil, :debug, e.backtrace.join("\n")) end |
#valid? ⇒ 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 |