Class: Redd::Utilities::RateLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/redd/utilities/rate_limiter.rb

Overview

Manages rate limiting by sleeping.

Instance Method Summary collapse

Constructor Details

#initialize(gap) ⇒ RateLimiter


7
8
9
10
# File 'lib/redd/utilities/rate_limiter.rb', line 7

def initialize(gap)
  @gap = gap
  @last_request_time = Time.now - gap
end

Instance Method Details

#after_limitObject


12
13
14
15
16
17
18
# File 'lib/redd/utilities/rate_limiter.rb', line 12

def after_limit
  sleep_time = (@last_request_time + @gap) - Time.now
  sleep(sleep_time) if sleep_time > 0.01
  response = yield
  @last_request_time += @gap
  response
end