Module: ErrorRadar::Notifications::Chatwork

Defined in:
lib/error_radar/notifications/chatwork.rb

Constant Summary collapse

API_BASE =
'https://api.chatwork.com/v2'.freeze

Class Method Summary collapse

Class Method Details

.build_message(log, event) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/error_radar/notifications/chatwork.rb', line 32

def self.build_message(log, event)
  app  = ErrorRadar::Notifier.app_name
  link = ErrorRadar::Notifier.error_url(log)

  title = case event
          when :spike
            spike = log.instance_variable_get(:@spike_data)
            "Spike — #{spike[:count]} hits in #{spike[:window_minutes]} min: #{log.error_class}"
          when :new_error
            "New error: #{log.error_class}"
          else
            "#{log.severity.capitalize} error: #{log.error_class}"
          end

  body_lines = []
  body_lines << "Source:      #{log.source || 'unknown'}"
  body_lines << "Category:    #{log.category}"
  body_lines << "Severity:    #{log.severity}"
  body_lines << "Occurrences: #{log.occurrences}"
  body_lines << "Message:     #{log.message.to_s.truncate(300)}"
  body_lines << "URL:         #{link}" if link

  "[info][title][#{app}] #{title}[/title]#{body_lines.join("\n")}[/info]"
end

.deliver(log, event = :recurring) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/error_radar/notifications/chatwork.rb', line 11

def self.deliver(log, event = :recurring)
  cfg             = ErrorRadar.config
  token, room_id, = ErrorRadar::Notifier.chatwork_db_settings(cfg)
  return if token.empty? || room_id.empty?

  uri               = URI("#{API_BASE}/rooms/#{room_id}/messages")
  http              = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl      = true
  http.open_timeout = 5
  http.read_timeout = 5

  req                    = Net::HTTP::Post.new(uri)
  req['X-ChatWorkToken'] = token
  req['Content-Type']    = 'application/x-www-form-urlencoded'
  req.body               = URI.encode_www_form(body: build_message(log, event))

  http.request(req)
rescue StandardError => e
  ErrorRadar::Tracking.warn_internal("ChatWork notification failed: #{e.message}")
end