Class: ExceptionNotifier::HipchatNotifier

Inherits:
BaseNotifier show all
Defined in:
lib/exception_notifier/hipchat_notifier.rb

Instance Attribute Summary collapse

Attributes inherited from BaseNotifier

#base_options

Instance Method Summary collapse

Methods inherited from BaseNotifier

#_post_callback, #_pre_callback, #send_notice

Constructor Details

#initialize(options) ⇒ HipchatNotifier

Returns a new instance of HipchatNotifier.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/exception_notifier/hipchat_notifier.rb', line 9

def initialize(options)
  super
  begin
    api_token         = options.delete(:api_token)
    room_name         = options.delete(:room_name)
    opts              = {
      api_version: options.delete(:api_version) || 'v1'
    }
    opts[:server_url] = options.delete(:server_url) if options[:server_url]
    @from             = options.delete(:from) || 'Exception'
    @room             = HipChat::Client.new(api_token, opts)[room_name]
    @message_template = options.delete(:message_template) || lambda { |exception, errors_count|
      msg = if errors_count > 1
              "The exception occurred #{errors_count} times: '#{Rack::Utils.escape_html(exception.message)}'"
            else
              "A new exception occurred: '#{Rack::Utils.escape_html(exception.message)}'"
            end
      msg += " on '#{exception.backtrace.first}'" if exception.backtrace
      msg
    }
    @message_options = options
    @message_options[:color] ||= 'red'
  rescue StandardError
    @room = nil
  end
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



5
6
7
# File 'lib/exception_notifier/hipchat_notifier.rb', line 5

def from
  @from
end

#message_optionsObject

Returns the value of attribute message_options.



7
8
9
# File 'lib/exception_notifier/hipchat_notifier.rb', line 7

def message_options
  @message_options
end

#roomObject

Returns the value of attribute room.



6
7
8
# File 'lib/exception_notifier/hipchat_notifier.rb', line 6

def room
  @room
end

Instance Method Details

#call(exception, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/exception_notifier/hipchat_notifier.rb', line 36

def call(exception, options = {})
  return unless active?

  message = @message_template.call(exception, options[:accumulated_errors_count].to_i)
  send_notice(exception, options, message, @message_options) do |msg, message_opts|
    @room.send(@from, msg, message_opts)
  end
end