Class: ExceptionNotifier::IkachanNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_notifier/ikachan_notifier.rb

Defined Under Namespace

Classes: Client

Constant Summary collapse

DEFAULT_FORMAT =
"\x02\x0315,4[ERROR]\x03 \x0313%{class}\x03 - %{message}\x03\x0f, %{occurred}"
IRC_SEQUENCE_RE =
Regexp.new("[\x02\x03\x0f](\\d+)?(,\\d+)?")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ IkachanNotifier

Returns a new instance of IkachanNotifier.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



53
54
55
# File 'lib/exception_notifier/ikachan_notifier.rb', line 53

def channels
  @channels
end

#clientObject (readonly)

Returns the value of attribute client.



53
54
55
# File 'lib/exception_notifier/ikachan_notifier.rb', line 53

def client
  @client
end

#messageObject (readonly)

Returns the value of attribute message.



53
54
55
# File 'lib/exception_notifier/ikachan_notifier.rb', line 53

def message
  @message
end

#message_formatObject (readonly)

Returns the value of attribute message_format.



53
54
55
# File 'lib/exception_notifier/ikachan_notifier.rb', line 53

def message_format
  @message_format
end

Instance Method Details

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



62
63
64
65
66
67
68
69
70
71
# 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 = {}) ⇒ Object



57
58
59
60
# File 'lib/exception_notifier/ikachan_notifier.rb', line 57

def call(exception, options = {})
  build_message(exception, options)
  client.notice_all(channels, message)
end