class ExceptionNotifier::IkachanNotifier

Constants

DEFAULT_FORMAT
IRC_SEQUENCE_RE
VERSION

alias

Attributes

channels[R]
client[R]
message[R]
message_format[R]

Public Class Methods

new(options) click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 37
def initialize(options)
  channel = options[:channels] || options[:channel]
  if !channel or !options[:base_url]
    raise "Some of option is missing: %s" % options
  end

  @channels = channel.is_a?(Array) ? channel : [channel]
  @client   = Client.new(options[:base_url])
  @message_format = build_message_format(options)
  @message  = nil

  @request_param_names = message_format.scan(/%{(request_[a-zA-Z_?!]+)}/).flatten.uniq
  @request_param_names.map{|n| [n, n.sub(/^request_/, '')] }.each do |param_name, attribute|
    raise "Parameter name #{param_name} is unavailable" unless request_klass.method_defined?(attribute)
  end
end

Public Instance Methods

build_message(exception, options = {}) click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 62
def build_message(exception, options = {})
  params = {
    class:    exception.class,
    message:  exception.message,
    occurred: (exception.backtrace.first rescue nil),
    hostname: (Socket.gethostname rescue nil),
  }
  params.merge!(build_params_from_request(options[:env]))
  @message = message_format % params
end
call(exception, options = {}) click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 57
def call(exception, options = {})
  build_message(exception, options)
  client.notice_all(channels, message)
end

Private Instance Methods

build_message_format(options) click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 85
def build_message_format(options)
  return options[:message_format] if options[:message_format]
  DEFAULT_FORMAT.dup.tap do |fmt|
    fmt.prepend(options[:message_prefix]) if options[:message_prefix]
    fmt.concat(options[:message_suffix])  if options[:message_suffix]
    fmt.gsub!(IRC_SEQUENCE_RE, '')        if options[:message_nocolor]
  end
end
build_params_from_request(env=nil) click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 94
def build_params_from_request(env=nil)
  return default_params_from_request unless env
  request = request_klass.new(env)
  dest = {}
  @request_param_names.map{|n| [n, n.sub(/^request_/, '')] }.each do |param_name, attribute|
    dest[param_name.to_sym] = request.send(attribute)
  end
  dest
end
default_params_from_request() click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 104
def default_params_from_request
  @request_param_names.each_with_object({}) do |name, dest|
    dest[name.to_sym] = ''
  end
end
request_klass() click to toggle source
# File lib/exception_notifier/ikachan_notifier.rb, line 74
def request_klass
  @request_klass ||= if defined?(ActionDispatch::Request)
    ActionDispatch::Request
  else
    require 'rack/request'
    Rack::Request
  end
rescue LoadError, NameError
  raise "Please use this notifier in some kind of Rack-based webapp"
end