Skip to content

Commit

Permalink
Auto corrected by following Format Ruby Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Awesome Code committed Oct 10, 2019
1 parent bff46c2 commit 22e387e
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 55 deletions.
40 changes: 33 additions & 7 deletions lib/uniform_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,43 @@
require 'uniform_notifier/terminal_notifier'

class UniformNotifier
AVAILABLE_NOTIFIERS = %i[alert console growl honeybadger xmpp rails_logger
customized_logger airbrake rollbar bugsnag slack raise
sentry terminal_notifier].freeze
AVAILABLE_NOTIFIERS = %i[
alert
console
growl
honeybadger
xmpp
rails_logger
customized_logger
airbrake
rollbar
bugsnag
slack
raise
sentry
terminal_notifier
].freeze

NOTIFIERS = [JavascriptAlert, JavascriptConsole, Growl, HoneybadgerNotifier, Xmpp, RailsLogger,
CustomizedLogger, AirbrakeNotifier, RollbarNotifier, BugsnagNotifier, Raise, Slack,
SentryNotifier, TerminalNotifier].freeze
NOTIFIERS = [
JavascriptAlert,
JavascriptConsole,
Growl,
HoneybadgerNotifier,
Xmpp,
RailsLogger,
CustomizedLogger,
AirbrakeNotifier,
RollbarNotifier,
BugsnagNotifier,
Raise,
Slack,
SentryNotifier,
TerminalNotifier
].freeze

class NotificationError < StandardError; end

class <<self
class << self
attr_accessor(*AVAILABLE_NOTIFIERS)

def active_notifiers
Expand Down
5 changes: 1 addition & 4 deletions lib/uniform_notifier/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ def self._out_of_channel_notify(data)

exception = Exception.new(data[:title])
exception.set_backtrace(data[:backtrace]) if data[:backtrace]
Bugsnag.notify(exception, opt.merge(
grouping_hash: data[:body] || data[:title],
notification: data
))
Bugsnag.notify(exception, opt.merge(grouping_hash: data[:body] || data[:title], notification: data))
end
end
end
16 changes: 5 additions & 11 deletions lib/uniform_notifier/growl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def self.setup_connection(growl)
setup_connection_gntp(growl)
rescue LoadError
@growl = nil
raise NotificationError, 'You must install the ruby-growl or the ruby_gntp gem to use Growl notification: `gem install ruby-growl` or `gem install ruby_gntp`'
raise NotificationError,
'You must install the ruby-growl or the ruby_gntp gem to use Growl notification: `gem install ruby-growl` or `gem install ruby_gntp`'
end
end

Expand Down Expand Up @@ -44,11 +45,8 @@ def self.setup_connection_gntp(growl)
end
@password ||= nil
@host ||= 'localhost'
@growl = GNTP.new('uniform_notifier', @host, @password, 23053)
@growl.register(notifications: [{
name: 'uniform_notifier',
enabled: true,
}])
@growl = GNTP.new('uniform_notifier', @host, @password, 23_053)
@growl.register(notifications: [{ name: 'uniform_notifier', enabled: true }])

notify 'Uniform Notifier Growl has been turned on (using GNTP)' if !growl.instance_of?(Hash) || !growl[:quiet]
end
Expand All @@ -67,11 +65,7 @@ def self.notify(message)
if defined?(::Growl) && @growl.is_a?(::Growl)
@growl.notify('uniform_notifier', 'Uniform Notifier', message)
elsif defined?(::GNTP) && @growl.is_a?(::GNTP)
@growl.notify(
name: 'uniform_notifier',
title: 'Uniform Notifier',
text: message
)
@growl.notify(name: 'uniform_notifier', title: 'Uniform Notifier', text: message)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/uniform_notifier/terminal_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def self._out_of_channel_notify(data)
begin
require 'terminal-notifier'
rescue LoadError
raise NotificationError, 'You must install the terminal-notifier gem to use terminal_notifier: `gem install terminal-notifier`'
raise NotificationError,
'You must install the terminal-notifier gem to use terminal_notifier: `gem install terminal-notifier`'
end
end

Expand Down
6 changes: 2 additions & 4 deletions lib/uniform_notifier/xmpp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.setup_connection(xmpp_information)
@xmpp = xmpp_information
@receiver = xmpp_information[:receiver]
@password = xmpp_information[:password]
@account = xmpp_information[:account]
@account = xmpp_information[:account]
@show_online_status = xmpp_information[:show_online_status]
@stay_connected = xmpp_information[:stay_connected].nil? ? true : xmpp_information[:stay_connected]

Expand Down Expand Up @@ -48,9 +48,7 @@ def self.connect

def self.notify(message)
connect unless @stay_connected
message = Jabber::Message.new(@receiver, message)
.set_type(:normal)
.set_subject('Uniform Notifier')
message = Jabber::Message.new(@receiver, message).set_type(:normal).set_subject('Uniform Notifier')
@xmpp.send(message)
end

Expand Down
8 changes: 2 additions & 6 deletions spec/uniform_notifier/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

RSpec.describe UniformNotifier::Base do
context '#inline_channel_notify' do
before do
allow(UniformNotifier::Base).to receive(:active?).and_return(true)
end
before { allow(UniformNotifier::Base).to receive(:active?).and_return(true) }
it 'should keep the compatibility' do
expect(UniformNotifier::Base).to receive(:_inline_notify).once.with(title: 'something')
UniformNotifier::Base.inline_notify('something')
end
end
context '#out_of_channel_notify' do
before do
allow(UniformNotifier::Base).to receive(:active?).and_return(true)
end
before { allow(UniformNotifier::Base).to receive(:active?).and_return(true) }
it 'should keep the compatibility' do
expect(UniformNotifier::Base).to receive(:_out_of_channel_notify).once.with(title: 'something')
UniformNotifier::Base.out_of_channel_notify('something')
Expand Down
20 changes: 16 additions & 4 deletions spec/uniform_notifier/bugsnag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ class Bugsnag
let(:notification_data) { { user: 'user', title: 'notify bugsnag', url: 'URL', body: 'something' } }

it 'should notify bugsnag' do
expect(Bugsnag).to receive(:notify).with(UniformNotifier::Exception.new(notification_data[:title]), grouping_hash: notification_data[:body], notification: notification_data)
expect(Bugsnag).to receive(:notify).with(
UniformNotifier::Exception.new(notification_data[:title]),
grouping_hash: notification_data[:body], notification: notification_data
)

UniformNotifier.bugsnag = true
UniformNotifier::BugsnagNotifier.out_of_channel_notify(notification_data)
end

it 'should notify bugsnag with option' do
expect(Bugsnag).to receive(:notify).with(UniformNotifier::Exception.new(notification_data[:title]), foo: :bar, grouping_hash: notification_data[:body], notification: notification_data)
expect(Bugsnag).to receive(:notify).with(
UniformNotifier::Exception.new(notification_data[:title]),
foo: :bar, grouping_hash: notification_data[:body], notification: notification_data
)

UniformNotifier.bugsnag = { foo: :bar }
UniformNotifier::BugsnagNotifier.out_of_channel_notify(notification_data)
Expand All @@ -33,14 +39,20 @@ class Bugsnag
let(:notification_data) { 'notify bugsnag' }

it 'should notify bugsnag' do
expect(Bugsnag).to receive(:notify).with(UniformNotifier::Exception.new('notify bugsnag'), grouping_hash: 'notify bugsnag', notification: { title: 'notify bugsnag' })
expect(Bugsnag).to receive(:notify).with(
UniformNotifier::Exception.new('notify bugsnag'),
grouping_hash: 'notify bugsnag', notification: { title: 'notify bugsnag' }
)

UniformNotifier.bugsnag = true
UniformNotifier::BugsnagNotifier.out_of_channel_notify(notification_data)
end

it 'should notify bugsnag with option' do
expect(Bugsnag).to receive(:notify).with(UniformNotifier::Exception.new('notify bugsnag'), foo: :bar, grouping_hash: 'notify bugsnag', notification: { title: 'notify bugsnag' })
expect(Bugsnag).to receive(:notify).with(
UniformNotifier::Exception.new('notify bugsnag'),
foo: :bar, grouping_hash: 'notify bugsnag', notification: { title: 'notify bugsnag' }
)

UniformNotifier.bugsnag = { foo: :bar }
UniformNotifier::BugsnagNotifier.out_of_channel_notify(notification_data)
Expand Down
30 changes: 25 additions & 5 deletions spec/uniform_notifier/growl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
expect(Growl).to receive(:new).with('localhost', 'uniform_notifier').and_return(growl)
expect(growl).to receive(:add_notification).with('uniform_notifier')
expect(growl).to receive(:password=).with(nil)
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'Uniform Notifier Growl has been turned on').ordered
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'notify growl without password').ordered
expect(growl).to receive(:notify).with(
'uniform_notifier',
'Uniform Notifier',
'Uniform Notifier Growl has been turned on'
)
.ordered
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'notify growl without password')
.ordered

UniformNotifier.growl = true
UniformNotifier::Growl.out_of_channel_notify(title: 'notify growl without password')
Expand All @@ -24,7 +30,12 @@
expect(Growl).to receive(:new).with('localhost', 'uniform_notifier').and_return(growl)
expect(growl).to receive(:add_notification).with('uniform_notifier')
expect(growl).to receive(:password=).with('123456')
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'Uniform Notifier Growl has been turned on').ordered
expect(growl).to receive(:notify).with(
'uniform_notifier',
'Uniform Notifier',
'Uniform Notifier Growl has been turned on'
)
.ordered
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'notify growl with password').ordered

UniformNotifier.growl = { password: '123456' }
Expand All @@ -36,7 +47,12 @@
expect(Growl).to receive(:new).with('10.10.156.17', 'uniform_notifier').and_return(growl)
expect(growl).to receive(:add_notification).with('uniform_notifier')
expect(growl).to receive(:password=).with('123456')
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'Uniform Notifier Growl has been turned on').ordered
expect(growl).to receive(:notify).with(
'uniform_notifier',
'Uniform Notifier',
'Uniform Notifier Growl has been turned on'
)
.ordered
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'notify growl with password').ordered

UniformNotifier.growl = { password: '123456', host: '10.10.156.17' }
Expand All @@ -48,7 +64,11 @@
expect(Growl).to receive(:new).with('localhost', 'uniform_notifier').and_return(growl)
expect(growl).to receive(:add_notification).with('uniform_notifier')
expect(growl).to receive(:password=).with('123456')
expect(growl).not_to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'Uniform Notifier Growl has been turned on')
expect(growl).not_to receive(:notify).with(
'uniform_notifier',
'Uniform Notifier',
'Uniform Notifier Growl has been turned on'
)
expect(growl).to receive(:notify).with('uniform_notifier', 'Uniform Notifier', 'notify growl with password')

UniformNotifier.growl = { password: '123456', quiet: true }
Expand Down
15 changes: 6 additions & 9 deletions spec/uniform_notifier/raise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,22 @@

it 'should raise error of the default class' do
UniformNotifier.raise = true
expect {
UniformNotifier::Raise.out_of_channel_notify(title: 'notification')
}.to raise_error(UniformNotifier::Exception, 'notification')
expect { UniformNotifier::Raise.out_of_channel_notify(title: 'notification') }.to raise_error(
UniformNotifier::Exception,
'notification'
)
end

it 'allows the user to override the default exception class' do
klass = Class.new(Exception)
UniformNotifier.raise = klass
expect {
UniformNotifier::Raise.out_of_channel_notify(title: 'notification')
}.to raise_error(klass, 'notification')
expect { UniformNotifier::Raise.out_of_channel_notify(title: 'notification') }.to raise_error(klass, 'notification')
end

it 'can be turned from on to off again' do
UniformNotifier.raise = true
UniformNotifier.raise = false

expect {
UniformNotifier::Raise.out_of_channel_notify(title: 'notification')
}.not_to raise_error
expect { UniformNotifier::Raise.out_of_channel_notify(title: 'notification') }.not_to raise_error
end
end
4 changes: 3 additions & 1 deletion spec/uniform_notifier/slack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
begin
UniformNotifier.slack = {}
rescue UniformNotifier::NotificationError

ensure
expect_any_instance_of(Slack::Notifier).to_not receive(:ping)
expect(UniformNotifier::Slack.out_of_channel_notify(title: 'notify slack')).to be_nil
Expand All @@ -34,7 +35,8 @@
end

it 'should allow username and channel config options' do
expect(Slack::Notifier).to receive(:new).with('http://some.slack.url', username: 'The Dude', channel: '#carpets').and_return(true)
expect(Slack::Notifier).to receive(:new).with('http://some.slack.url', username: 'The Dude', channel: '#carpets')
.and_return(true)
UniformNotifier.slack = { webhook_url: 'http://some.slack.url', username: 'The Dude', channel: '#carpets' }
expect(UniformNotifier::Slack.active?).to eq true
end
Expand Down
4 changes: 3 additions & 1 deletion spec/uniform_notifier/terminal_notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

it "should raise an exception when terminal-notifier gem isn't available" do
UniformNotifier.terminal_notifier = true
expect { UniformNotifier::TerminalNotifier.out_of_channel_notify(body: 'body', title: 'notify terminal') }.to raise_error(UniformNotifier::NotificationError, /terminal-notifier gem/)
expect {
UniformNotifier::TerminalNotifier.out_of_channel_notify(body: 'body', title: 'notify terminal')
}.to raise_error(UniformNotifier::NotificationError, /terminal-notifier gem/)
end

it 'should notify terminal-notifier when enabled' do
Expand Down
8 changes: 6 additions & 2 deletions spec/uniform_notifier/xmpp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
expect(message).to receive(:set_subject).with('Uniform Notifier').and_return(message)
expect(xmpp).to receive(:send).with(message)

UniformNotifier.xmpp = { account: 'from@gmail.com', password: '123456', receiver: 'to@gmail.com', show_online_status: false }
UniformNotifier.xmpp = {
account: 'from@gmail.com', password: '123456', receiver: 'to@gmail.com', show_online_status: false
}
UniformNotifier::Xmpp.out_of_channel_notify(title: 'notify xmpp')
end

Expand All @@ -46,7 +48,9 @@
expect(message).to receive(:set_subject).with('Uniform Notifier').and_return(message)
expect(xmpp).to receive(:send).with(message)

UniformNotifier.xmpp = { account: 'from@gmail.com', password: '123456', receiver: 'to@gmail.com', show_online_status: true }
UniformNotifier.xmpp = {
account: 'from@gmail.com', password: '123456', receiver: 'to@gmail.com', show_online_status: true
}
UniformNotifier::Xmpp.out_of_channel_notify(title: 'notify xmpp')
end
end

0 comments on commit 22e387e

Please sign in to comment.