Class: LHC::Throttle

Inherits:
Interceptor show all
Defined in:
lib/lhc/interceptors/throttle.rb

Defined Under Namespace

Classes: OutOfQuota

Class Attribute Summary collapse

Attributes inherited from Interceptor

#request

Instance Method Summary collapse

Methods inherited from Interceptor

#after_request, #all_interceptor_classes, #before_raw_request, #before_response, dup, #initialize, #response

Constructor Details

This class inherits a constructor from LHC::Interceptor

Class Attribute Details

.trackObject

Returns the value of attribute track.



10
11
12
# File 'lib/lhc/interceptors/throttle.rb', line 10

def track
  @track
end

Instance Method Details

#after_responseObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lhc/interceptors/throttle.rb', line 23

def after_response
  options = response.request.options.dig(:throttle)
  return unless throttle?(options)

  self.class.track ||= {}
  self.class.track[options.dig(:provider)] = {
    limit: limit(options: options[:limit], response: response),
    remaining: remaining(options: options[:remaining], response: response),
    expires: expires(options: options[:expires], response: response)
  }
end

#before_requestObject



13
14
15
16
17
18
19
20
21
# File 'lib/lhc/interceptors/throttle.rb', line 13

def before_request
  options = request.options.dig(:throttle)
  return unless options

  break_options = options.dig(:break)
  return unless break_options

  break_when_quota_reached! if break_options.match?('%')
end