Class: God::Contacts::Slack

Inherits:
God::Contact show all
Defined in:
lib/god/contacts/slack.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

.channelObject

Returns the value of attribute channel.



22
23
24
# File 'lib/god/contacts/slack.rb', line 22

def channel
  @channel
end

.emojiObject

Returns the value of attribute emoji.



22
23
24
# File 'lib/god/contacts/slack.rb', line 22

def emoji
  @emoji
end

.formatObject

Returns the value of attribute format.



22
23
24
# File 'lib/god/contacts/slack.rb', line 22

def format
  @format
end

.notify_channelObject

Returns the value of attribute notify_channel.



22
23
24
# File 'lib/god/contacts/slack.rb', line 22

def notify_channel
  @notify_channel
end

.urlObject

Returns the value of attribute url.



22
23
24
# File 'lib/god/contacts/slack.rb', line 22

def url
  @url
end

.usernameObject

Returns the value of attribute username.



22
23
24
# File 'lib/god/contacts/slack.rb', line 22

def username
  @username
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



35
36
37
# File 'lib/god/contacts/slack.rb', line 35

def channel
  @channel
end

#emojiObject

Returns the value of attribute emoji.



35
36
37
# File 'lib/god/contacts/slack.rb', line 35

def emoji
  @emoji
end

#formatObject

Returns the value of attribute format.



35
36
37
# File 'lib/god/contacts/slack.rb', line 35

def format
  @format
end

#notify_channelObject

Returns the value of attribute notify_channel.



35
36
37
# File 'lib/god/contacts/slack.rb', line 35

def notify_channel
  @notify_channel
end

#urlObject

Returns the value of attribute url.



35
36
37
# File 'lib/god/contacts/slack.rb', line 35

def url
  @url
end

#usernameObject

Returns the value of attribute username.



35
36
37
# File 'lib/god/contacts/slack.rb', line 35

def username
  @username
end

Instance Method Details

#api_urlObject



57
58
59
# File 'lib/god/contacts/slack.rb', line 57

def api_url
  URI.parse arg(:url)
end

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/god/contacts/slack.rb', line 45

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

  request(text)
end

#request(text) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/god/contacts/slack.rb', line 61

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 do |payload|
    payload[:username] = arg(:username) if arg(:username)
    payload[:icon_emoji] = arg(:emoji) if arg(:emoji)
  end.to_json

  res = http.request(req)

  self.info = case res
              when Net::HTTPSuccess
                "successfully notified slack on channel #{arg(:channel)}"
              else
                "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



37
38
39
40
41
42
43
# File 'lib/god/contacts/slack.rb', line 37

def text(data)
  text = +''
  text << '<!channel> ' if arg(:notify_channel)
  text << (arg(:format) % data)

  text
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/god/contacts/slack.rb', line 29

def valid?
  valid = true
  valid &= complain("Attribute 'url' must be specified", self) unless arg(:url)
  valid
end