Sha256: 34d85ce3b098827762775d72dcf3695dd7614f20cf8d16867412700ec3a327f1

Contents?: true

Size: 924 Bytes

Versions: 2

Compression:

Stored size: 924 Bytes

Contents

require 'racli/handlers/base'

module Racli
  module Handlers
    class RetryHandler < Base
      DEFAULT_MAX_RETRY_COUNTER = 3

      attr_accessor :retry_counter, :default_max_retry_counter

      def initialize(cli)
        @retry_counter = 0
        @retry_max_counter = DEFAULT_MAX_RETRY_COUNTER
        super
      end

      def call(status, headers, body, original_args)
        if ((300...400) === status.to_i) && headers['Location']
          unless within_max_retry_count?
            STDERR.puts 'Too many redirection!'
            throw :abort
          end
          @retry_counter += 1
          original_args[:method] = 'GET'
          original_args[:path]   = headers['Location']
          cli.call(**original_args)
          throw :abort
        end
        [status, headers, body]
      end

      def within_max_retry_count?
        @retry_counter < DEFAULT_MAX_RETRY_COUNTER
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
racli-0.1.1 lib/racli/handlers/retry_handler.rb
racli-0.1.0 lib/racli/handlers/retry_handler.rb