Module: RaiseToSlack

Defined in:
lib/raise_to_slack.rb,
lib/raise_to_slack/version.rb

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.channel_nameObject



54
55
56
# File 'lib/raise_to_slack.rb', line 54

def self.channel_name
  ENV['RAISE_TO_SLACK_CHANNEL_NAME']
end

.debug?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/raise_to_slack.rb', line 58

def self.debug?
  ENV['RAISE_TO_SLACK_DEBUG']
end

.oauth2_tokenObject



50
51
52
# File 'lib/raise_to_slack.rb', line 50

def self.oauth2_token
  ENV['RAISE_TO_SLACK_OAUTH2_TOKEN']
end

.post(e) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/raise_to_slack.rb', line 24

def self.post(e)
  url = URI.parse('https://slack.com/api/files.upload')
  req = Net::HTTP::Post.new(url.path)
  req.set_form([
    ["content", e.backtrace.join("\n")],
    ["filename", e.class.to_s],
    ["channels", channel_name],
    ["token", oauth2_token],
  ], 'multipart/form-data')
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  res = http.start {|http| http.request(req) }
  STDERR.puts 'upload response' if debug?
  STDERR.puts res.body if debug?

  thread_ts = JSON.parse(res.body)['file']['shares'].values[0].values[0][0]['ts']
  channel_id = JSON.parse(res.body)['file']['shares'].values[0].keys[0]
  res = Net::HTTP.post_form(
    URI.parse('https://slack.com/api/chat.postMessage'),
    { 'token' => oauth2_token, 'channel' => channel_id, 'text' => e.message, 'reply_broadcast' => 'true', 'thread_ts' => thread_ts }
  )
  STDERR.puts 'post response' if debug?
  STDERR.puts res.body if debug?
  res.value
end

.runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/raise_to_slack.rb', line 7

def self.run
  begin
    yield
  rescue => e
    if channel_name.nil? || oauth2_token.nil?
      STDERR.puts "RaiseToSlack does not work. Please set ENV['RAISE_TO_SLACK_OAUTH2_TOKEN'] and ENV['RAISE_TO_SLACK_CHANNEL_NAME']."
    else
      begin
        post(e)
      rescue => e2
        STDERR.puts "RaiseToSlack failed: #{e2.message}"
      end
    end
    raise e
  end
end