Class: AirbrakePlugin

Inherits:
Campfire::PollingBot::Plugin show all
Defined in:
lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb

Constant Summary

Constants inherited from Campfire::PollingBot::Plugin

Campfire::PollingBot::Plugin::HALT

Instance Attribute Summary

Attributes inherited from Campfire::PollingBot::Plugin

#bot, #config

Instance Method Summary collapse

Methods inherited from Campfire::PollingBot::Plugin

accepts, accepts?, #accepts?, bot, bot=, directory, directory=, inherited, load_all, load_plugin_classes, #logger, logger, #priority, priority, requires_config, #requires_config?, requires_config?, setup_database, subclasses, #to_s

Constructor Details

#initializeAirbrakePlugin

Returns a new instance of AirbrakePlugin.



9
10
11
12
# File 'lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb', line 9

def initialize
  super
  @api = Airbrake::API.new(config['domain'], config['auth_token'])
end

Instance Method Details

#announce(errors) ⇒ Object



52
53
54
55
56
57
# File 'lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb', line 52

def announce(errors)
  msg = "Got #{errors.length} new error#{errors.length > 1 ? 's' : ''} from Airbrake" + 
        (errors.length > 5 ? ". Here are the first 5:" : ":")
  bot.say(msg)
  errors.first(5).each { |e| bot.say("#{e.summary} (#{@api.error_url(e)})") }
end

#handle_errorsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb', line 41

def handle_errors
  # fetch errors we know about, announce new ones, and remove resolved ones
  unresolved_errors = @api.errors
  known_errors = Airbrake::Error.all
  new_errors = unresolved_errors - known_errors
  resolved_errors = known_errors - unresolved_errors
  resolved_errors.each {|e| e.destroy }
  new_errors.each {|e| e.save }
  announce(new_errors) if new_errors.any?
end

#heartbeatObject



32
33
34
35
36
37
38
39
# File 'lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb', line 32

def heartbeat
  # check every <check_interval> seconds (heartbeat is called every 3 sec)
  num_heartbeats = config['check_interval'] / Campfire::PollingBot::HEARTBEAT_INTERVAL
  @heartbeat_counter ||= 0
  @heartbeat_counter += 1
  return unless (@heartbeat_counter % num_heartbeats) == 1
  handle_errors
end

#helpObject

return array of available commands and descriptions



60
61
62
63
# File 'lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb', line 60

def help
  [["resolve <error number>", "mark an error as resolved"],
   ["unresolve <error number>", "mark an error as unresolved"]]
end

#process(message) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/campfire/polling_bot/plugins/airbrake/airbrake_plugin.rb', line 14

def process(message)
  case message.command
  when /((?:un(?:-)?)?resolve) error (?:#|number)?\s*(\d+)/
    action, error_num = $1, $2
    res = @api.resolve_error(error_num, action == "resolve")
    case res.code
    when 200
      bot.say("Ok, #{action}d error ##{error_num}")
    when 404
      bot.say("Hmm. Airbrake couldn't find error ##{error_num}")
    else
      bot.say("Huh. Airbrake gave me this response:")
      bot.paste(res.to_s)
    end
    return HALT
  end
end