smartinez87/exception_notification — pull request #139 by jweslley (2013-04-01 04:05:43+00:00)
Title: make exception_notification a generic notification driver

First at all, I must to say this is a huge change. But, it's backward compatible and totally [tested](https://travis-ci.org/jweslley/exception_notification/builds).

This pull request transforms the exception_notification gem in a notification driver, allowing easily to add new forms to notify errors besides email and campfire. As proof-of-concept, I implemented a webhook notifier, which notify about errors via HTTP protocol, useful for publish errors using my hubot instance. (In fact, this was the origin of all of this sutff :smile: : )

Besides, tool makers can use exception_notification as an abstraction to send notifications about errors using a simple API like this:

```ruby
ExceptionNotifier.notify_exception(exception, options)
```

Instead of making ugly things like [these](https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/exception_handler.rb).

### Notifiers

The ExceptionNotifier middleware nows accepts multiple notifiers.

Notifiers are objects which respond to method `#call(exception, options)` in order to execute some action: send an email, publish to a chat room, call a webhook or whatever your crazy little mind wants!

You can configure your notifiers passing a hash of options to the ExceptionNotifier middleware in the given form:

Whatever::Application.config.middleware.use ExceptionNotifier,
:email => { ... email options ... },
:campfire => { ... campfire options ... },
...

Though, to this thing work, notifiers MUST BE implemented under the
ExceptionNotifier namespace and MUST HAVE a name ending 'Notifier'. e.g:
'ExceptionNotifier::EmailNotifier'.

Moreover, any notifier can be added/removed later in some place of
the application, like a Rails initializer, using the method
'ExceptionNotifier#register_exception_notifier(name, notifier)' since
the `notifier` responds to `#call(exception, options)` method. Example:

ExceptionNotifier.register_exception_notifier :test,
lambda { |exception, options| ... do something ... }

By now, the only notifier implemented are EmailNotifier, CampfireNotifier and WebhookNotifier.

### Background notifications

In order to send background notifications, just code:

```ruby
ExceptionNotifier.notify_exception(exception, options)
```

The old-way to send background notification is still valid, but shows up a deprecation notice.

### Backwards compatibility

Although the configuration hash SHOULD BE in the following style:

Whatever::Application.config.middleware.use ExceptionNotifier,
:email => { ... email options ... },
:campfire => { ... campfire options ... },
...

The old style is still accepted (but shows a deprecation notice):

Whatever::Application.config.middleware.use ExceptionNotifier,
... email options ... ,
:campfire => { ... campfire options ... },
...

For more info, please check out the README.md file. I have updated it in order to reflect all of these things!