1 2 3 4 5 6 7 8 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
# frozen_string_literal: true
module ExceptionNotifier
class IrcNotifier < BaseNotifier
def initialize(options)
super
@config = OpenStruct.new
parse_options(options)
end
def call(exception, options = {})
errors_count = options[:accumulated_errors_count].to_i
occurrences = "(#{errors_count} times)" if errors_count > 1
message = "#{occurrences}'#{exception.message}'"
message += " on '#{exception.backtrace.first}'" if exception.backtrace
return unless active?
send_notice(exception, options, message) do |msg, _|
send_message([*@config.prefix, *msg].join(" "))
end
end
def send_message(message)
CarrierPigeon.send @config.irc.merge(message: message)
end
private
def parse_options(options)
nick = options.fetch(:nick, "ExceptionNotifierBot")
password = options[:password] ? ":#{options[:password]}" : nil
domain = options.fetch(:domain, nil)
port = options[:port] ? ":#{options[:port]}" : nil
channel = options.fetch(:channel, "#log")
notice = options.fetch(:notice, false)
ssl = options.fetch(:ssl, false)
join = options.fetch(:join, false)
uri = "irc://#{nick}#{password}@#{domain}#{port}/#{channel}"
prefix = options.fetch(:prefix, nil)
recipients = options[:recipients] ? options[:recipients].join(", ") + ":" : nil
@config.prefix = [*prefix, *recipients].join(" ")
@config.irc = {uri: uri, ssl: ssl, notice: notice, join: join}
end
def active?
valid_uri? @config.irc[:uri]
end
def valid_uri?(uri)
URI.parse(uri)
rescue URI::InvalidURIError
false
end
end
end
|