Class: ExceptionsToHipchat::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/exceptions_to_hipchat/notifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, client = nil) ⇒ Notifier

Returns a new instance of Notifier.



5
6
7
8
9
10
11
12
13
# File 'lib/exceptions_to_hipchat/notifier.rb', line 5

def initialize(app, options = {}, client = nil)
  @app = app
  @client = client || HipChat::Client.new(options[:api_token] || raise("HipChat API token is required"))
  @room = options[:room] || raise("HipChat room is required")
  @color = options[:color] || :red
  @notify = options[:notify]
  @user = (options[:user] || "Notifier")[0...14]
  @ignore = options[:ignore]
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
# File 'lib/exceptions_to_hipchat/notifier.rb', line 15

def call(env)
  @app.call(env)
rescue Exception => exception
  send_to_hipchat(exception) unless @ignore && @ignore.match(exception.to_s)
  raise exception
end

#message_for(exception) ⇒ Object



30
31
32
# File 'lib/exceptions_to_hipchat/notifier.rb', line 30

def message_for(exception)
  "[#{exception.class}] #{exception}"
end

#send_to_hipchat(exception) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/exceptions_to_hipchat/notifier.rb', line 22

def send_to_hipchat(exception)
  begin
    @client[@room].send(@user, message_for(exception), :color => @color, :notify => @notify)
  rescue => hipchat_exception
    $stderr.puts "\nWARN: Unable to send message to HipChat: #{hipchat_exception}\n"
  end
end