Skip to content

Commit

Permalink
Merge pull request #191 from airbrake/airbrake-713-nil-notifier
Browse files Browse the repository at this point in the history
airbrake-ruby,notifier: use the Null Object pattern for notifiers
  • Loading branch information
kyrylo authored Apr 21, 2017
2 parents b008474 + a627629 commit 15b43ea
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Airbrake Ruby Changelog

### master

* Return `Airbrake::NilNotifier` when no notifiers are configured and
`Airbrake.[]` is called
([#191](https://github.com/airbrake/airbrake-ruby/pull/191))

### [v2.0.0][v2.0.0] (March 21, 2017)

* **IMPORTANT:** Removed the `component/action` API deprecated
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ Retrieves a configured notifier.
Airbrake[:my_notifier] #=> Airbrake::Notifier
```

If the notifier is not configured, returns an instance of
`Airbrake::NilNotifier` (a no-op version of `Airbrake::Notifier`).

#### Airbrake.notify

Sends an exception to Airbrake asynchronously.
Expand Down
17 changes: 9 additions & 8 deletions lib/airbrake-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ module Airbrake

##
# A Hash that holds all notifiers. The keys of the Hash are notifier
# names, the values are Airbrake::Notifier instances.
@notifiers = {}
# names, the values are Airbrake::Notifier instances. If a notifier is not
# assigned to the hash, then it returns a null object (NilNotifier).
@notifiers = Hash.new(NilNotifier.new)

class << self
##
Expand Down Expand Up @@ -146,7 +147,7 @@ def configure(notifier_name = :default)
# @return [Airbrake::Promise]
# @see .notify_sync
def notify(exception, params = {})
@notifiers[:default] && @notifiers[:default].notify(exception, params)
@notifiers[:default].notify(exception, params)
end

##
Expand All @@ -159,7 +160,7 @@ def notify(exception, params = {})
# @return [Hash{String=>String}] the reponse from the server
# @see .notify
def notify_sync(exception, params = {})
@notifiers[:default] && @notifiers[:default].notify_sync(exception, params)
@notifiers[:default].notify_sync(exception, params)
end

##
Expand Down Expand Up @@ -189,7 +190,7 @@ def notify_sync(exception, params = {})
# @return [void]
# @note Once a filter was added, there's no way to delete it
def add_filter(filter = nil, &block)
@notifiers[:default] && @notifiers[:default].add_filter(filter, &block)
@notifiers[:default].add_filter(filter, &block)
end

##
Expand All @@ -208,7 +209,7 @@ def add_filter(filter = nil, &block)
# @return [Airbrake::Notice] the notice built with help of the given
# arguments
def build_notice(exception, params = {})
@notifiers[:default] && @notifiers[:default].build_notice(exception, params)
@notifiers[:default].build_notice(exception, params)
end

##
Expand All @@ -222,7 +223,7 @@ def build_notice(exception, params = {})
#
# @return [void]
def close
@notifiers[:default] && @notifiers[:default].close
@notifiers[:default].close
end

##
Expand All @@ -237,7 +238,7 @@ def close
# @option deploy_params [Symbol] :version
# @return [void]
def create_deploy(deploy_params)
@notifiers[:default] && @notifiers[:default].create_deploy(deploy_params)
@notifiers[:default].create_deploy(deploy_params)
end
end
end
19 changes: 19 additions & 0 deletions lib/airbrake-ruby/notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,23 @@ def add_filters_for_config_keys
add_filter(Filters::KeysWhitelist.new(@config.logger, *@config.whitelist_keys))
end
end

##
# NilNotifier is a no-op notifier, which mimics +Airbrake::Notifier+ and
# serves only for the purpose of making the library API easier to use.
#
# @since 2.1.0
class NilNotifier
def notify(_exception, _params = {}); end

def notify_sync(_exception, _params); end

def add_filter(_filter = nil, &_block); end

def build_notice(_exception, _params); end

def close; end

def create_deploy(_deploy_params); end
end
end
10 changes: 8 additions & 2 deletions spec/airbrake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@
end

after do
described_class.instance_variable_set(:@notifiers, {})
described_class.instance_variable_set(
:@notifiers,
Hash.new(Airbrake::NilNotifier.new)
)
end

shared_examples 'non-configured notifier handling' do |method|
it "returns nil if there is no configured notifier when using #{method}" do
described_class.instance_variable_set(:@notifiers, {})
described_class.instance_variable_set(
:@notifiers,
Hash.new(Airbrake::NilNotifier.new)
)
expect(described_class.__send__(method, 'bingo')).to be_nil
end
end
Expand Down

0 comments on commit 15b43ea

Please sign in to comment.