Class: God::Contacts::Slack
- Inherits:
-
God::Contact
- Object
- God::Contact
- God::Contacts::Slack
- Defined in:
- lib/god/contacts/slack.rb
Class Attribute Summary collapse
-
.account ⇒ Object
Returns the value of attribute account.
-
.channel ⇒ Object
Returns the value of attribute channel.
-
.emoji ⇒ Object
Returns the value of attribute emoji.
-
.format ⇒ Object
Returns the value of attribute format.
-
.notify_channel ⇒ Object
Returns the value of attribute notify_channel.
-
.token ⇒ Object
Returns the value of attribute token.
-
.username ⇒ Object
Returns the value of attribute username.
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#emoji ⇒ Object
Returns the value of attribute emoji.
-
#format ⇒ Object
Returns the value of attribute format.
-
#notify_channel ⇒ Object
Returns the value of attribute notify_channel.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
Attributes inherited from God::Contact
Instance Method Summary collapse
- #api_url ⇒ Object
- #notify(message, time, priority, category, host) ⇒ Object
- #request(text) ⇒ Object
- #text(data) ⇒ Object
- #valid? ⇒ Boolean
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
.account ⇒ Object
Returns the value of attribute account.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def account @account end |
.channel ⇒ Object
Returns the value of attribute channel.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def channel @channel end |
.emoji ⇒ Object
Returns the value of attribute emoji.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def emoji @emoji end |
.format ⇒ Object
Returns the value of attribute format.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def format @format end |
.notify_channel ⇒ Object
Returns the value of attribute notify_channel.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def notify_channel @notify_channel end |
.token ⇒ Object
Returns the value of attribute token.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def token @token end |
.username ⇒ Object
Returns the value of attribute username.
22 23 24 |
# File 'lib/god/contacts/slack.rb', line 22 def username @username end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def account @account end |
#channel ⇒ Object
Returns the value of attribute channel.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def channel @channel end |
#emoji ⇒ Object
Returns the value of attribute emoji.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def emoji @emoji end |
#format ⇒ Object
Returns the value of attribute format.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def format @format end |
#notify_channel ⇒ Object
Returns the value of attribute notify_channel.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def notify_channel @notify_channel end |
#token ⇒ Object
Returns the value of attribute token.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
36 37 38 |
# File 'lib/god/contacts/slack.rb', line 36 def username @username end |
Instance Method Details
#api_url ⇒ Object
65 66 67 |
# File 'lib/god/contacts/slack.rb', line 65 def api_url URI.parse("https://#{arg(:account)}.slack.com/services/hooks/incoming-webhook?token=#{arg(:token)}") end |
#notify(message, time, priority, category, host) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/god/contacts/slack.rb', line 53 def notify(, time, priority, category, host) text = text({ :message => , :time => time, :priority => priority, :category => category, :host => host }) request(text) end |
#request(text) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/god/contacts/slack.rb', line 69 def request(text) http = Net::HTTP.new(api_url.host, api_url.port) http.use_ssl = true req = Net::HTTP::Post.new(api_url.request_uri) req.body = { :link_names => 1, :text => text, :channel => arg(:channel) }.tap { |payload| payload[:username] = arg(:username) if arg(:username) payload[:icon_emoji] = arg(:emoji) if arg(:emoji) }.to_json res = http.request(req) case res when Net::HTTPSuccess self.info = "successfully notified slack on channel #{arg(:channel)}" 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 |
#text(data) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/god/contacts/slack.rb', line 38 def text(data) text = "" text << "<!channel> " if arg(:notify_channel) if RUBY_VERSION =~ /^1\.8/ text << arg(:format).gsub(/%\{(\w+)\}/) do |match| data[$1.to_sym] end else text << arg(:format) % data end text end |
#valid? ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/god/contacts/slack.rb', line 29 def valid? valid = true valid &= complain("Attribute 'account' must be specified", self) unless arg(:account) valid &= complain("Attribute 'token' must be specified", self) unless arg(:token) valid end |