Skip to content

Commit

Permalink
Merge pull request #7 from restaurant-cheetah/enable-to-set-configura…
Browse files Browse the repository at this point in the history
…tions-on-method-call

Enable to set configurations on method call
Closes #1
  • Loading branch information
EvNomad authored Sep 24, 2018
2 parents a9ef823 + 6025263 commit 830a36f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class KratosService
end
end

# Pass custom options per method call
# The class defaults will not be overwritten
def kill_baldur
call_api_with_retry(retries: 2, retriable: [IOError], retry_proc: proc {}, retry_condition_proc: proc {}, time_to_sleep: 1.11) do
# Some logic that might raise..
end
end

end

KratosService.new.call_boy
Expand Down
7 changes: 6 additions & 1 deletion lib/take2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def self.reset(options = {})
@configuration = Configuration.new(options)
end

def self.local_defaults(options)
configuration.validate_options(options)
end

def self.configure
yield(configuration) if block_given?
end
Expand Down Expand Up @@ -51,8 +55,9 @@ module InstanceMethods
# end
#
# end
def call_api_with_retry
def call_api_with_retry(options = {})
config = self.class.retriable_configuration
config.merge! Take2.local_defaults(options) unless options.empty?
tries ||= config[:retries]
begin
yield
Expand Down
30 changes: 19 additions & 11 deletions lib/take2/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ def initialize(options = {})
@retry_condition_proc = proc { false }
@time_to_sleep = 3
# Overwriting the defaults
options.each do |k, v|
validate_options(options, &setter)
end

def to_hash
CONFIG_ATTRS.each_with_object({}) do |key, hash|
hash[key] = public_send(key)
end
end

def [](value)
self.public_send(value)
end

def validate_options(options, &setter)
options.each do |k, v|
raise ArgumentError, "#{k} is not a valid configuration" unless CONFIG_ATTRS.include?(k)
case k
when :retries
Expand All @@ -29,18 +43,12 @@ def initialize(options = {})
when :retry_proc, :retry_condition_proc
raise ArgumentError, "#{k} must be Proc" unless v.is_a?(Proc)
end
instance_variable_set(:"@#{k}", v)
end
end

def to_hash
CONFIG_ATTRS.each_with_object({}) do |key, hash|
hash[key] = public_send(key)
end
setter.call(k, v) if block_given?
end
end

def [](value)
self.public_send(value)
def setter
proc { |key, value| instance_variable_set(:"@#{key}", value) }
end

end
Expand Down
44 changes: 44 additions & 0 deletions spec/take2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,50 @@ def wrath_the_gods_with(error)

end

context 'with custom options' do

let(:retriable_error) { Net::HTTPRetriableError.new 'Release the Kraken...many times!!', nil }
let(:new_retriable_error) { IOError.new 'You shall not PASS!' }

before(:each) { @tries = 0 }

it 'overwrites the :retries' do
expect do
object.call_api_with_retry(retries: 3) { wrath_the_gods_with retriable_error } rescue nil
end.to change { @tries }.from(0).to(4)
end

it 'overwrites the :retry_proc' do
new_proc = proc { 1**1 }
expect(new_proc).to receive(:call).exactly(klass.retriable_configuration[:retries])
object.call_api_with_retry(retry_proc: new_proc) { wrath_the_gods_with retriable_error } rescue nil
end

it 'overwrites the :retry_condition_proc' do
new_proc = proc { true }
expect(new_proc).to receive(:call).exactly(klass.retriable_configuration[:retries])
object.call_api_with_retry(retry_condition_proc: new_proc) { wrath_the_gods_with retriable_error } rescue nil
end

it 'overwrites the :time_to_sleep' do
allow_any_instance_of(Object).to receive(:sleep).with(1.66)
object.call_api_with_retry(time_to_sleep: 1.66) { wrath_the_gods_with retriable_error } rescue nil
end

it 'overwrites the :retriable' do
expect do
object.call_api_with_retry(retriable: [new_retriable_error]) { wrath_the_gods_with retriable_error } rescue nil
end.to change { @tries }.from(0).to(1)
end

it 'raises ArgumentError if there are invalid keys' do
expect do
object.call_api_with_retry(invalid_key: :nope) { wrath_the_gods_with retriable_error }
end.to raise_error ArgumentError
end

end

end

end

0 comments on commit 830a36f

Please sign in to comment.