Skip to content

Commit

Permalink
Cleanup Rubocop config, fix offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Aug 28, 2024
1 parent fdd9306 commit 9594753
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 75 deletions.
62 changes: 48 additions & 14 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,77 @@
---
require:
- rubocop-performance
- rubocop-rails
- rubocop-rake
- rubocop-rspec

AllCops:
NewCops: enable
SuggestExtensions: false
TargetRubyVersion: 3.0
Exclude:
- bin/*
- lib/mailgun-rails.rb

Gemspec/RequireMFA:
Enabled: false

#########
# STYLE #
#########

Style/Documentation:
Enabled: false

Layout/EmptyLinesAroundModuleBody:
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma

Style/BlockDelimiters:
AllowedPatterns: ['expect']

##########
# LAYOUT #
##########

Layout/LineLength:
Max: 125

Layout/EmptyLines:
Enabled: false

Layout/EmptyLineBetweenDefs:
Enabled: false

Layout/EmptyLinesAroundClassBody:
Enabled: false

Layout/EmptyLineBetweenDefs:
Layout/EmptyLinesAroundBlockBody:
Enabled: false

Layout/EmptyLines:
Layout/EmptyLinesAroundModuleBody:
Enabled: false

Layout/IndentationConsistency:
EnforcedStyle: indented_internal_methods

Style/NumericPredicate:
EnforcedStyle: comparison
Layout/FirstHashElementIndentation:
EnforcedStyle: consistent

Metrics/BlockLength:
Exclude:
- spec/**/*
#########
# RSPEC #
#########

Layout/LineLength:
Exclude:
- spec/**/*
RSpec/NotToNot:
EnforcedStyle: to_not

RSpec/NestedGroups:
Enabled: false

RSpec/MultipleExpectations:
Max: 3

Naming/FileName:
RSpec/AnyInstance:
Exclude:
- lib/mailgun-rails.rb
- spec/mailgun_rails/web_hook_processor_spec.rb
10 changes: 8 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ gem 'rake'
gem 'rspec'
gem 'rubocop'
gem 'rubocop-performance'
gem 'rubocop-rails'
gem 'rubocop-rake'
gem 'rubocop-rspec'
gem 'simplecov'

# Fix:
# warning: ostruct was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
# warning: logger was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add logger to your Gemfile or gemspec.
# warning: ostruct was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0
# Add ostruct to your Gemfile or gemspec.
#
# warning: logger was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0
# Add logger to your Gemfile or gemspec.
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.4.0')
gem 'logger'
gem 'ostruct'
Expand Down
2 changes: 1 addition & 1 deletion lib/mailgun/web_hook/authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def secure_compare(a, b)

res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
res.zero?
end
# rubocop:enable Naming/MethodParameterName, Layout/CommentIndentation

Expand Down
2 changes: 1 addition & 1 deletion lib/mailgun/web_hook/event_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Mailgun
module WebHook
class EventDecorator < OpenStruct
class EventDecorator < OpenStruct # rubocop:disable Style/OpenStructUse

def delivery_status
self['delivery-status']
Expand Down
2 changes: 1 addition & 1 deletion lib/mailgun/web_hook/message_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def event_type
event.event
end

def method_missing(method, *args)
def method_missing(method, *args) # rubocop:disable Style/MissingRespondToMissing
event.send(method, *args)
end

Expand Down
54 changes: 26 additions & 28 deletions spec/mailgun/web_hook/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@

RSpec.describe Mailgun::WebHook::Processor do
let(:params) { {} }
let(:processor_class) { Mailgun::WebHook::Processor }
let(:processor_class) { described_class }
let(:processor) { processor_class.new(params) }

describe '#run!' do
context 'when handler methods are present' do
let(:params) { load_mailgun_event(:clicked)[:'event-data'] }

it 'passes event payload to the handler' do
expect(processor).to receive(:handle_clicked)
allow(processor).to receive(:handle_clicked)
processor.run!
expect(processor).to have_received(:handle_clicked)
end
end

context 'but no valid handler methods are present' do
context 'when no valid handler methods are present' do
let(:params) { nil }

it 'keeps calm and carries on' do
processor.run!
expect { processor.run! }.to_not raise_error
end
end

Expand All @@ -32,15 +34,15 @@
context 'with handler method as public' do
let(:callback_host_class) do
Class.new do
def handle_clicked
def handle_clicked(_payload)
'ok'
end
end
end

it 'passes event payload to the handler' do
expect(callback_host).to receive(:handle_clicked).and_return('ok')
processor.run!
response = processor.run!
expect(response).to eq 'ok'
end
end

Expand All @@ -49,15 +51,15 @@ def handle_clicked
Class.new do
protected

def handle_clicked
def handle_clicked(_payload)
'ok'
end
end
end

it 'passes event payload to the handler' do
expect(callback_host).to receive(:handle_clicked).and_return('ok')
processor.run!
response = processor.run!
expect(response).to eq 'ok'
end
end

Expand All @@ -66,50 +68,46 @@ def handle_clicked
Class.new do
private

def handle_clicked
def handle_clicked(_payload)
'ok'
end
end
end

it 'passes event payload to the handler' do
expect(callback_host).to receive(:handle_clicked).and_return('ok')
processor.run!
response = processor.run!
expect(response).to eq 'ok'
end
end

context 'with unhandled event' do
let(:callback_host_class) { Class.new }

context 'and default missing handler behaviour' do
context 'with default missing handler behaviour' do
before { processor.on_unhandled_mailgun_events = :log }

it 'logs an error' do
processor.on_unhandled_mailgun_events = :log
logger = double
expect(logger).to receive(:error)
expect(Rails).to receive(:logger).and_return(logger)
allow(Rails).to receive(:logger).and_return(logger)
allow(logger).to receive(:error)

expect do
processor.run!
end.to_not raise_error
expect { processor.run! }.to_not raise_error
expect(logger).to have_received(:error)
end
end

context 'and ignore missing handler behaviour' do
context 'with ignore missing handler behaviour' do
it 'keeps calm and carries on' do
processor.on_unhandled_mailgun_events = :ignore

expect do
processor.run!
end.to_not raise_error
expect { processor.run! }.to_not raise_error
end
end

context 'and raise_exception missing handler behaviour' do
context 'with raise_exception missing handler behaviour' do
it 'raises an error' do
processor.on_unhandled_mailgun_events = :raise_exception
expect do
processor.run!
end.to raise_error(MailgunRails::Errors::MissingEventHandler)
expect { processor.run! }.to raise_error(MailgunRails::Errors::MissingEventHandler)
end
end
end
Expand Down
Loading

0 comments on commit 9594753

Please sign in to comment.