Class: Pushpop::Slack

Inherits:
Step
  • Object
show all
Defined in:
lib/pushpop-slack.rb,
lib/pushpop-slack/version.rb

Constant Summary collapse

PLUGIN_NAME =
'slack'
VERSION =
'0.2.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_attachmentsObject

Returns the value of attribute _attachments.



19
20
21
# File 'lib/pushpop-slack.rb', line 19

def _attachments
  @_attachments
end

#_channelObject

Returns the value of attribute _channel.



14
15
16
# File 'lib/pushpop-slack.rb', line 14

def _channel
  @_channel
end

#_iconObject

Returns the value of attribute _icon.



17
18
19
# File 'lib/pushpop-slack.rb', line 17

def _icon
  @_icon
end

#_icon_typeObject

Returns the value of attribute _icon_type.



18
19
20
# File 'lib/pushpop-slack.rb', line 18

def _icon_type
  @_icon_type
end

#_messageObject

Returns the value of attribute _message.



16
17
18
# File 'lib/pushpop-slack.rb', line 16

def _message
  @_message
end

#_unfurlObject

Returns the value of attribute _unfurl.



20
21
22
# File 'lib/pushpop-slack.rb', line 20

def _unfurl
  @_unfurl
end

#_usernameObject

Returns the value of attribute _username.



15
16
17
# File 'lib/pushpop-slack.rb', line 15

def _username
  @_username
end

Instance Method Details

#attachment(attachment) ⇒ Object



99
100
101
102
103
# File 'lib/pushpop-slack.rb', line 99

def attachment(attachment)
  self._attachments = [] unless self._attachments

  self._attachments.push(attachment)
end

#channel(channel) ⇒ Object



87
88
89
# File 'lib/pushpop-slack.rb', line 87

def channel(channel)
  self._channel = channel
end

#clear_settingsObject



38
39
40
41
42
43
44
45
# File 'lib/pushpop-slack.rb', line 38

def clear_settings
  self._username = nil
  self._message = nil
  self._icon = nil
  self._icon_type = nil
  self._attachments = nil
  self._unfurl = nil
end

#configure(last_response = nil, step_responses = nil) ⇒ Object



130
131
132
# File 'lib/pushpop-slack.rb', line 130

def configure(last_response=nil, step_responses=nil)
  self.instance_exec(last_response, step_responses, &block)
end

#icon(icon) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pushpop-slack.rb', line 105

def icon(icon)
  if icon[0..3] == 'http'
    self._icon_type = 'url'
    self._icon = icon
  else
    self._icon_type = 'emoji'
    self._icon = icon

    # Make sure the emoji is wrapped in colons
    if self._icon[0] != ':'
      self._icon = ":#{self._icon}"
    end

    if self._icon[self._icon.length - 1] != ':'
      self._icon = "#{self._icon}:"
    end
  end

  self._icon
end

#message(message) ⇒ Object



95
96
97
# File 'lib/pushpop-slack.rb', line 95

def message(message)
  self._message = ::Slack::Notifier::LinkFormatter.format(message)
end

#optionsObject



57
58
59
60
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
# File 'lib/pushpop-slack.rb', line 57

def options
  opts = {}

  if _channel
    if _channel[0] == '#'
      opts['channel'] = _channel
    else
      opts['channel'] = "##{_channel}"
    end
  end

  if _username
    opts['username'] = _username
  end

  if _icon && _icon_type
    opts["icon_#{_icon_type}"] = _icon
  end

  if _attachments
    opts['attachments'] = _attachments
  end

  if _unfurl
    opts['unfurl_links'] = true
  end

  return opts
end

#run(last_response = nil, step_responses = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pushpop-slack.rb', line 22

def run(last_response=nil, step_responses=nil)


  ret = configure(last_response, step_responses)

  if _message
    send_message
  else
    Pushpop.logger.debug("No slack message sent - message was not set")
  end

  clear_settings

  ret
end

#send_messageObject



47
48
49
50
51
52
53
54
55
# File 'lib/pushpop-slack.rb', line 47

def send_message
  unless WEBHOOK_URL.nil? || WEBHOOK_URL.empty?
    notifier = ::Slack::Notifier.new WEBHOOK_URL

    notifier.ping _message, options
  else
    Pushpop.logger.debug("Could not send slack message - SLACK_WEBHOOK_URL is nil or empty")
  end
end

#unfurl(should = true) ⇒ Object



126
127
128
# File 'lib/pushpop-slack.rb', line 126

def unfurl(should = true)
  self._unfurl = should
end

#username(username) ⇒ Object



91
92
93
# File 'lib/pushpop-slack.rb', line 91

def username(username)
  self._username = username
end