2017-04-12 01:33:54 +00:00
|
|
|
module Github
|
|
|
|
class RateLimit
|
2017-04-24 22:26:07 +00:00
|
|
|
SAFE_REMAINING_REQUESTS = 100
|
|
|
|
SAFE_RESET_TIME = 500
|
2017-04-24 22:28:08 +00:00
|
|
|
RATE_LIMIT_URL = '/rate_limit'.freeze
|
2017-04-12 01:33:54 +00:00
|
|
|
|
|
|
|
attr_reader :connection
|
|
|
|
|
|
|
|
def initialize(connection)
|
|
|
|
@connection = connection
|
|
|
|
end
|
|
|
|
|
2017-04-24 22:49:17 +00:00
|
|
|
def get
|
|
|
|
response = connection.get(RATE_LIMIT_URL)
|
2017-04-12 01:33:54 +00:00
|
|
|
|
2017-04-24 22:49:17 +00:00
|
|
|
# GitHub Rate Limit API returns 404 when the rate limit is disabled
|
|
|
|
return false unless response.status != 404
|
2017-04-12 01:33:54 +00:00
|
|
|
|
2017-04-24 22:49:17 +00:00
|
|
|
body = Oj.load(response.body, class_cache: false, mode: :compat)
|
|
|
|
remaining = body.dig('rate', 'remaining').to_i
|
|
|
|
reset_in = body.dig('rate', 'reset').to_i
|
|
|
|
exceed = remaining <= SAFE_REMAINING_REQUESTS
|
2017-04-12 01:33:54 +00:00
|
|
|
|
2017-04-24 22:49:17 +00:00
|
|
|
[exceed, reset_in]
|
2017-04-12 01:33:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|