Class: DHC::Throttle

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

Defined Under Namespace

Classes: OutOfQuota

Constant Summary collapse

CACHE_KEY =
'DHC/throttle/tracker/v1'

Instance Attribute Summary

Attributes inherited from Interceptor

#request

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Interceptor

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

Constructor Details

This class inherits a constructor from DHC::Interceptor

Class Method Details

.tracker(provider) ⇒ Object



13
14
15
# File 'lib/dhc/interceptors/throttle.rb', line 13

def tracker(provider)
  (Rails.cache.read(CACHE_KEY) || {})[provider] || {}
end

.tracker=(track) ⇒ Object



17
18
19
# File 'lib/dhc/interceptors/throttle.rb', line 17

def tracker=(track)
  Rails.cache.write(CACHE_KEY, (Rails.cache.read(CACHE_KEY) || {}).merge({ track[:provider] => track }))
end

Instance Method Details

#after_responseObject



27
28
29
30
31
32
33
34
35
# File 'lib/dhc/interceptors/throttle.rb', line 27

def after_response
  return unless track?
  self.class.tracker = {
    provider: options.dig(:provider),
    limit: limit,
    remaining: remaining,
    expires: expires
  }
end

#before_requestObject



22
23
24
25
# File 'lib/dhc/interceptors/throttle.rb', line 22

def before_request
  return unless options
  break! if break?
end