Class: Fbe::Middleware::Quota

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/fbe/middleware/quota.rb

Overview

Faraday Middleware that monitors GitHub API rate limits.

Instance Method Summary collapse

Constructor Details

#initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5) ⇒ Quota

Returns a new instance of Quota.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fbe/middleware/quota.rb', line 29

def initialize(app, loog: Loog::NULL, pause: 60, limit: 100, rate: 5)
  super(app)
  @requests = 0
  @app = app
  raise 'The "loog" cannot be nil' if loog.nil?
  @loog = loog
  raise 'The "pause" cannot be nil' if pause.nil?
  raise 'The "pause" must be a positive integer' unless pause.positive?
  @pause = pause
  raise 'The "limit" cannot be nil' if limit.nil?
  raise 'The "limit" must be a positive integer' unless limit.positive?
  @limit = limit
  raise 'The "rate" cannot be nil' if rate.nil?
  raise 'The "rate" must be a positive integer' unless rate.positive?
  @rate = rate
end

Instance Method Details

#call(env) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fbe/middleware/quota.rb', line 46

def call(env)
  @requests += 1
  response = @app.call(env)
  if out_of_limit?(env)
    @loog.info(
      "Too much GitHub API quota consumed, pausing for #{@pause} seconds"
    )
    sleep(@pause)
    @requests = 0
  end
  response
end