lib/take2.rb in take2-0.0.2 vs lib/take2.rb in take2-0.0.3
- old
+ new
@@ -21,21 +21,25 @@
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
module InstanceMethods
# Yields a block and retries on retriable errors n times.
# The raised error could be the defined retriable or it child.
#
# Example:
- # class KatorzaService
+ # class PizzaService
# include Take2
#
# number_of_retries 3
# retriable_errors Net::HTTPRetriableError, Net::HTTPServerError
# retriable_condition proc { |error| response_status(error.response) < 500 }
@@ -49,12 +53,13 @@
# # If not the original error re raised
# end
# 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
rescue => e
if config[:retriable].map {|klass| e.class <= klass }.any?
@@ -73,71 +78,72 @@
module ClassMethods
# Sets number of retries.
#
# Example:
- # class KatorzaService
+ # class PizzaService
# include Take2
# number_of_retries 3
# end
# Arguments:
- # num: Positive integer
+ # num: integer
def number_of_retries(num)
raise ArgumentError, 'Must be positive Integer' unless num.is_a?(Integer) && num.positive?
self.retries = num
end
# Sets list of errors on which the block will retry.
#
# Example:
- # class KatorzaService
+ # class PizzaService
# include Take2
# retriable_errors Net::HTTPRetriableError, Errno::ECONNRESET
# end
# Arguments:
# errors: List of retiable errors
def retriable_errors(*errors)
+ raise ArgumentError, 'All retriable errors must be StandardError decendants' unless errors.all? { |e| e <= StandardError }
self.retriable = errors
end
# Sets condition for retry attempt.
# If set, it MUST result to +false+ with number left retries greater that zero in order to retry.
#
# Example:
- # class KatorzaService
+ # class PizzaService
# include Take2
# retriable_condition proc { |error| error.response.status_code < 500 }
# end
# Arguments:
- # proc: Proc. The +proc+ called by default with the raised error argument
+ # proc: Proc. The proc called by default with the raised error argument
def retriable_condition(proc)
raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
self.retry_condition_proc = proc
end
# Defines a proc that is called *before* retry attempt.
#
# Example:
- # class KatorzaService
+ # class PizzaService
# include Take2
# on_retry proc { |error, tries| puts "Retrying.. #{tries} of #{self.class.retriable_configuration[:retries]}" }
# end
# Arguments:
- # proc: Proc. The +proc+ called by default with the raised error and number of left retries.
+ # proc: Proc. The proc called by default with the raised error and number of left retries.
def on_retry(proc)
raise ArgumentError, 'Must be callable' unless proc.respond_to?(:call)
self.retry_proc = proc
end
# Sets number of seconds to sleep before next retry.
#
# Example:
- # class KatorzaService
+ # class PizzaService
# include Take2
# sleep_before_retry 1.5
# end
# Arguments:
- # seconds: Positive number.
+ # seconds: number
def sleep_before_retry(seconds)
raise ArgumentError, 'Must be positive numer' unless (seconds.is_a?(Integer) || seconds.is_a?(Float)) && seconds.positive?
self.time_to_sleep = seconds
end
\ No newline at end of file