Class: FaradayDynamicTimeout::Bucket Private

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday_dynamic_timeout/bucket.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Internal class for storing bucket configuration.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout:, limit: 0) ⇒ Bucket

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Bucket.

Parameters:

  • timeout (Float)

    The timeout in seconds.

  • limit (Integer) (defaults to: 0)

    The limit.



33
34
35
36
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 33

def initialize(timeout:, limit: 0)
  @timeout = timeout.to_f.round(3)
  @limit = limit.to_i
end

Instance Attribute Details

#limitObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



7
8
9
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 7

def limit
  @limit
end

#timeoutObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



7
8
9
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 7

def timeout
  @timeout
end

Class Method Details

.from_hashes(hashes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 10

def from_hashes(hashes)
  all_buckets = hashes.collect { |hash| new(timeout: fetch_indifferent_key(hash, :timeout), limit: fetch_indifferent_key(hash, :limit)) }
  grouped_buckets = all_buckets.select(&:valid?).group_by(&:timeout).values
  return [] if grouped_buckets.empty?

  unique_buckets = grouped_buckets.collect do |buckets|
    buckets.reduce do |merged, bucket|
      merged.nil? ? bucket : merged.merge(bucket)
    end
  end

  unique_buckets.sort_by(&:timeout)
end

Instance Method Details

#==(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 50

def ==(other)
  return false unless other.is_a?(self.class)

  timeout == other.timeout && limit == other.limit
end

#merge(bucket) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 56

def merge(bucket)
  combined_limit = if no_limit?
    limit
  elsif bucket.no_limit?
    bucket.limit
  else
    limit + bucket.limit
  end

  combined_timeout = [timeout, bucket.timeout].max

  self.class.new(timeout: combined_timeout, limit: combined_limit)
end

#no_limit?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return true if the bucket has no limit. A bucket has no limit if the limit is negative.

Returns:

  • (Boolean)

    True if the bucket has no limit.



40
41
42
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 40

def no_limit?
  limit < 0
end

#valid?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return true if the bucket is valid. A bucket is valid if the timeout is positive and the limit is non-zero.

Returns:

  • (Boolean)


46
47
48
# File 'lib/faraday_dynamic_timeout/bucket.rb', line 46

def valid?
  timeout.positive? && limit != 0
end