policy should be required

This commit is contained in:
Dmytro Zakharov 2018-01-22 19:26:42 +01:00
parent 2d5542f256
commit 517dc5c007
2 changed files with 9 additions and 9 deletions

View File

@ -3,7 +3,7 @@ module IdempotentRequest
def initialize(app, config = {}) def initialize(app, config = {})
@app = app @app = app
@config = config @config = config
@decider = config[:decider] @policy = config.fetch(:policy)
end end
def call(env) def call(env)
@ -20,15 +20,15 @@ module IdempotentRequest
private private
attr_reader :app, :env, :config, :request, :decider attr_reader :app, :env, :config, :request, :policy
def process? def process?
!request.key.to_s.empty? && should_be_idempotent? !request.key.to_s.empty? && should_be_idempotent?
end end
def should_be_idempotent? def should_be_idempotent?
return false unless decider return false unless policy
decider.new(request).should? policy.new(request).should?
end end
def set_request(env) def set_request(env)

View File

@ -9,13 +9,13 @@ RSpec.describe IdempotentRequest::Middleware do
) )
end end
let(:storage) { @memory_storage ||= IdempotentRequest::MemoryStorage.new } let(:storage) { @memory_storage ||= IdempotentRequest::MemoryStorage.new }
let(:decider) do let(:policy) do
class_double('IdempotentRequest::Decider', new: double(should?: true)) class_double('IdempotentRequest::policy', new: double(should?: true))
end end
let(:middleware) do let(:middleware) do
described_class.new(app, described_class.new(app,
decider: decider, policy: policy,
storage: storage, storage: storage,
header_key: 'X-Qonto-Idempotency-Key' header_key: 'X-Qonto-Idempotency-Key'
) )
@ -45,8 +45,8 @@ RSpec.describe IdempotentRequest::Middleware do
end end
context 'when should not be idempotent' do context 'when should not be idempotent' do
let(:decider) do let(:policy) do
class_double('IdempotentRequest::Decider', new: double(should?: false)) class_double('IdempotentRequest::policy', new: double(should?: false))
end end
it 'should not read storage' do it 'should not read storage' do