Sha256: 4d9ce82f48cdd5d845a5160fd5bbb690580cd2708dedc57f958f91769747c0b7

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

module ExceptionNotifier
  class IrcNotifier < BaseNotifier
    def initialize(options)
      super
      @config = OpenStruct.new
      parse_options(options)
    end

    def call(exception, options={})
      message = "'#{exception.message}'"
      message += " on '#{exception.backtrace.first}'" if exception.backtrace
      if active?
        send_notice(exception, options, message) do |msg, _|
          send_message([*@config.prefix, *msg].join(' '))
        end
      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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
exception_notification-4.2.1 lib/exception_notifier/irc_notifier.rb
exception_notification-4.2.0 lib/exception_notifier/irc_notifier.rb
exception_notification-4.2.0.rc1 lib/exception_notifier/irc_notifier.rb
exception_notification_more_info-1.0.1 lib/exception_notifier/irc_notifier.rb
exception_notification_more_info-1.0.0 lib/exception_notifier/irc_notifier.rb
exception_notification-4.1.4 lib/exception_notifier/irc_notifier.rb
exception_notification-4.1.3 lib/exception_notifier/irc_notifier.rb