Permalink
Browse files

IRC transport stuff

  • Loading branch information...
LTe committed Jan 29, 2011
1 parent 988ab78 commit d6b738ffdc0e9660984c690fbce5004a008bedb3
View
@@ -1,5 +1,9 @@
= CHANGELOG
+== 0.1.0
+* global dispatch hook
+* dispatch hook for each transport
+
== 0.0.6 (24.01.2011)
* config.ingore specifies ignored exceptions
* support for Rails 2
View
@@ -1,3 +1,4 @@
source "http://rubygems.org"
gemspec
+
View
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
- exceptioner (0.0.5)
+ exceptioner (0.0.6.pre1)
+ isaac (~> 0.2.6)
mail (~> 2.2)
xmpp4r (~> 0.5)
@@ -10,6 +11,7 @@ GEM
specs:
activesupport (3.0.3)
i18n (0.5.0)
+ isaac (0.2.6)
mail (2.2.14)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -30,6 +32,7 @@ PLATFORMS
DEPENDENCIES
exceptioner!
+ isaac (~> 0.2.6)
mail (~> 2.2)
mocha
rack
View
@@ -21,6 +21,8 @@ Gem::Specification.new do |s|
s.add_dependency("mail", ["~> 2.2"])
s.add_dependency("xmpp4r", ["~> 0.5"])
+ s.add_dependency("isaac", ["~> 0.2.6"])
+ s.add_dependency("redmine_client")
s.add_development_dependency("rack")
s.add_development_dependency("mocha")
View
@@ -1,22 +1,26 @@
require 'exceptioner/core_ext/class/attribute'
require 'exceptioner/core_ext/module/attribute_accessors'
require 'exceptioner/core_ext/string/inflections'
+require 'exceptioner/dispatchable'
require 'exceptioner/version'
require 'exceptioner/railtie' if defined?(Rails::Railtie)
module Exceptioner
+ extend Dispatchable
class ExceptionerError < StandardError; end
autoload :Middleware, 'exceptioner/middleware'
autoload :Notifier, 'exceptioner/notifier'
-
+
module Transport
autoload :Mail, 'exceptioner/transport/mail/mail'
autoload :Jabber, 'exceptioner/transport/jabber/jabber'
+ autoload :Redmine, 'exceptioner/transport/redmine/redmine'
+ autoload :IRC, 'exceptioner/transport/irc/irc'
end
- # Define how to deliver exceptions data.
+ # Define how to deliver exceptions data.
# For example :mail, :jabber, :irc
mattr_accessor :transports
@@transports = [:mail]
@@ -44,15 +48,22 @@ module Transport
ActionController::UnknownAction
]
- # Array of ignored exceptions.
+ # Array of ignored exceptions.
# By default it's set to exceptions defined in DEFAULT_IGNORED_EXCEPTIONS
mattr_accessor :ignore
@@ignore = DEFAULT_IGNORED_EXCEPTIONS.dup
+ mattr_accessor :irc_bot
+ @@irc_bot =
+
def self.setup
yield self
end
+ def self.init
+ add_default_dispatchers
+ end
+
def self.mail
Transport::Mail
end
@@ -61,6 +72,14 @@ def self.jabber
Transport::Jabber
end
+ def self.irc
+ Transport::IRC
+ end
+
+ def self.redmine
+ Transport::Redmine
+ end
+
def self.notify(exception, options = {})
Notifier.dispatch(exception, options)
end
@@ -69,6 +88,30 @@ def self.config
self
end
+ def self.reset_dispatchers
+ clear_dispatchers
+ add_default_dispatchers
+ end
+
+ def self.add_default_dispatchers
+ disallow_development_environment
+ disallow_ignored_exceptions
+ end
+
+ def self.disallow_development_environment
+ dispatch do |exception|
+ ! development_environments.include?(environment_name)
+ end
+ end
+
+ def self.disallow_ignored_exceptions
+ dispatch do |exception|
+ ! Array(ignore).collect(&:to_s).include?(exception.class.name)
+ end
+ end
+
end
require 'exceptioner/support/rails2' if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR == 2
+
+Exceptioner.init
@@ -0,0 +1,22 @@
+module Exceptioner
+ module Dispatchable
+
+ def dispatchers
+ @dispatchers ||= []
+ end
+
+ def clear_dispatchers
+ @dispatchers = []
+ end
+
+ def dispatch(clear = false, &block)
+ clear_dispatchers if clear
+ dispatchers << block
+ end
+
+ def run_dispatchers(exception)
+ dispatchers.all? { |dispatcher| dispatcher.call(exception) != false }
+ end
+
+ end
+end
@@ -2,10 +2,12 @@ module Exceptioner
class Notifier
def self.dispatch(exception, options = {})
- if dispatch_exception?(exception)
+ if config.run_dispatchers(exception)
options = determine_options(exception, options.dup)
determine_transports(options) do |transport|
- transport.deliver(options)
+ if transport.run_dispatchers(exception)
+ transport.deliver(options)
+ end
end
end
end
@@ -48,17 +50,6 @@ def self.config
Exceptioner
end
- def self.dispatch_exception?(exception)
- ! (config.development_environments.include?(config.environment_name) || ignore_exception?(exception))
- end
-
- def self.ignore_exception?(exception)
- if config.ignore
- # TODO: stringify config.ignore once
- Array(config.ignore).collect(&:to_s).include?(exception_class_name(exception))
- end
- end
-
# Determines class of exception.
def self.exception_class_name(exception)
exception.is_a?(Exception) ? exception.class.name : exception.to_s
@@ -1,6 +1,9 @@
+require 'exceptioner/dispatchable'
+
module Exceptioner::Transport
class Base
+ extend Exceptioner::Dispatchable
class_attribute :sender
@@ -10,11 +13,10 @@ class Base
class_attribute :subject
-
def self.deliver(options = {})
raise Exceptioner::ExceptionerError, 'Implement deliver class method in your Exceptioner::Transport::Base subclass'
end
-
+
protected
def self.default_options
{
@@ -0,0 +1,83 @@
+require 'isaac/bot'
+require 'digest/sha1'
+require 'erb'
+require 'exceptioner/transport/base'
+require 'exceptioner/transport/helper'
+
+module Exceptioner::Transport
+ class IRC < Base
+ class_attribute :server
+ class_attribute :port
+ class_attribute :nick
+ class_attribute :channel
+ class_attribute :bot
+ class_attribute :exceptions
+
+ def self.init(options)
+ options = options.dup
+ options = default_options.merge(options)
+
+ self.bot = Isaac::Bot.new do
+ configure do |c|
+ c.nick = options[:nick]
+ c.server = options[:server]
+ c.port = options[:port]
+ end
+
+ on :connect do
+ join self.channel || "#drug.pl"
+ end
+
+ on :channel, /!all/ do
+ self.exceptions.each do |exp|
+ msg channel, exp[:link]
+ end
+ end
+
+ end
+
+ thread = Thread.new do
+ self.bot.start
+ end
+
+ self.exceptions = {}
+
+ return self.bot, thread
+ end
+
+ def self.deliver(options = {})
+ body = prepare_message(options)
+ self.bot.msg '#drug.pl', body
+ end
+
+ def self.prepare_message(options)
+ body = render(options)
+ exception = add_exception(body, options)
+ "Exception!: " + exception[:link]
+ end
+
+ def self.post_body(body, options = { :provider => :pastebin })
+ case options[:provider]
+ when :pastebin
+ Net::HTTP.post_form(URI.parse("http://pastebin.com/api_public.php"), { :paste_code => body, :paste_private => 1 }).body
+ end
+ end
+
+ def self.default_options
+ {
+ :server => self.server || "chat.freenode.net",
+ :port => self.port || 6667,
+ :nick => self.nick || "ExceptionerBot"
+ }.merge!(super)
+ end
+
+ def self.add_exception(body, exception)
+ hash = Digest::SHA1.hexdigest(exception.to_s)
+ self.exceptions[hash] ||= { :options => exception, :link => post_body(body), :body => body, :counter => 1 }
+ self.exceptions[hash][:counter] += 1
+
+ exceptions[hash]
+ end
+
+ end
+end
@@ -0,0 +1,32 @@
+require 'erb'
+require 'redmine_client'
+
+module Exceptioner::Transport
+
+ class Redmine < Base
+ class_attribute :project # id_or_identifier of redmine project
+ class_attribute :options # redmine issue options
+
+ def self.connection(&block)
+ RedmineClient::Base.configure(&block)
+ end
+
+ def self.deliver(options = {})
+ options[:subject] ||= prefixed_subject(options)
+ options[:description] ||= render(options)
+ begin
+ issue = RedmineClient::Issue.new(
+ :subject => options[:subject],
+ :description => options[:description],
+ :project_id => self.project
+ )
+ issue.save
+ rescue ActiveResource::UnauthorizedAccess
+ puts 'Warning: Unauthorized Access please check http://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication'
+ rescue ActiveResource::ResourceNotFound
+ puts "Warning: Can't find project #{self.project}"
+ end
+ end
+ end
+
+end
Oops, something went wrong.

0 comments on commit d6b738f

Please sign in to comment.