gitlab-org--gitlab-foss/lib/github/rate_limit.rb

28 lines
714 B
Ruby
Raw Normal View History

2017-04-11 21:33:54 -04:00
module Github
class RateLimit
2017-04-24 18:26:07 -04:00
SAFE_REMAINING_REQUESTS = 100
SAFE_RESET_TIME = 500
2017-04-24 18:28:08 -04:00
RATE_LIMIT_URL = '/rate_limit'.freeze
2017-04-11 21:33:54 -04:00
attr_reader :connection
def initialize(connection)
@connection = connection
end
2017-04-24 18:49:17 -04:00
def get
response = connection.get(RATE_LIMIT_URL)
2017-04-11 21:33:54 -04:00
2017-04-24 18:49:17 -04:00
# GitHub Rate Limit API returns 404 when the rate limit is disabled
return false unless response.status != 404
2017-04-11 21:33:54 -04:00
2017-04-24 18:49:17 -04: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-11 21:33:54 -04:00
2017-04-24 18:49:17 -04:00
[exceed, reset_in]
2017-04-11 21:33:54 -04:00
end
end
end