Module: Slackistrano

Defined in:
lib/slackistrano.rb,
lib/slackistrano/version.rb,
lib/slackistrano/capistrano.rb

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Class Method Details

.post(team: nil, token: nil, webhook: nil, via_slackbot: false, payload: {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/slackistrano/capistrano.rb', line 11

def self.post(team: nil, token: nil, webhook: nil, via_slackbot: false, payload: {})
  if via_slackbot
    post_as_slackbot(team: team, token: token, webhook: webhook, payload: payload)
  else
    post_as_webhook(team: team, token: token, webhook: webhook, payload: payload)
  end
rescue => e
  error("[slackistrano] Error notifying Slack!")
  error("[slackistrano]   Error: #{e.inspect}")
end

.post_as_slackbot(team: nil, token: nil, webhook: nil, payload: {}) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/slackistrano/capistrano.rb', line 25

def self.post_as_slackbot(team: nil, token: nil, webhook: nil, payload: {})
  uri = URI(URI.encode("https://#{team}.slack.com/services/hooks/slackbot?token=#{token}&channel=#{payload[:channel]}"))

  text = payload[:attachments].collect { |a| a[:text] }.join("\n")

  Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
    http.request_post uri, text
  end
end

.post_as_webhook(team: nil, token: nil, webhook: nil, payload: {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/slackistrano/capistrano.rb', line 38

def self.post_as_webhook(team: nil, token: nil, webhook: nil, payload: {})
  params = {'payload' => payload.to_json}

  if webhook.nil?
    webhook = "https://#{team}.slack.com/services/hooks/incoming-webhook"
    params.merge!('token' => token)
  end

  uri = URI(webhook)
  Net::HTTP.post_form(uri, params)
end