Sha256: ceac500dd3ae613a8d36423f2930244fe93fa03c0c1a1d9ff2eaec10e248b6e2

Contents?: true

Size: 1.72 KB

Versions: 6

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require "timeout"

module HTTPX
  class Timeout
    CONNECT_TIMEOUT = 60
    OPERATION_TIMEOUT = 60

    def self.new(opts = {})
      return opts if opts.is_a?(Timeout)

      super
    end

    attr_reader :connect_timeout, :operation_timeout, :total_timeout

    def initialize(connect_timeout: CONNECT_TIMEOUT,
                   operation_timeout: OPERATION_TIMEOUT,
                   total_timeout: nil,
                   loop_timeout: nil)
      @connect_timeout = connect_timeout
      @operation_timeout = operation_timeout
      @total_timeout = total_timeout

      return unless loop_timeout

      warn ":loop_timeout is deprecated, use :operation_timeout instead"
      @operation_timeout = loop_timeout
    end

    def ==(other)
      if other.is_a?(Timeout)
        @connect_timeout == other.instance_variable_get(:@connect_timeout) &&
          @operation_timeout == other.instance_variable_get(:@operation_timeout) &&
          @total_timeout == other.instance_variable_get(:@total_timeout)
      else
        super
      end
    end

    def merge(other)
      case other
      when Hash
        timeout = Timeout.new(other)
        merge(timeout)
      when Timeout
        connect_timeout = other.instance_variable_get(:@connect_timeout) || @connect_timeout
        operation_timeout = other.instance_variable_get(:@operation_timeout) || @operation_timeout
        total_timeout = other.instance_variable_get(:@total_timeout) || @total_timeout
        Timeout.new(connect_timeout: connect_timeout,
                    operation_timeout: operation_timeout,
                    total_timeout: total_timeout)
      else
        raise ArgumentError, "can't merge with #{other.class}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
httpx-0.6.3 lib/httpx/timeout.rb
httpx-0.6.2 lib/httpx/timeout.rb
httpx-0.6.1 lib/httpx/timeout.rb
httpx-0.6.0 lib/httpx/timeout.rb
httpx-0.5.1 lib/httpx/timeout.rb
httpx-0.5.0 lib/httpx/timeout.rb