gitlab-org--gitlab-foss/lib/github/rate_limit.rb
2017-04-24 16:17:52 -03:00

45 lines
836 B
Ruby

module Github
class RateLimit
SAFE_REMAINING_REQUESTS = 100.freeze
SAFE_RESET_TIME = 500.freeze
attr_reader :connection
def initialize(connection)
@connection = connection
end
def exceed?
return false unless enabled?
remaining <= SAFE_REMAINING_REQUESTS
end
def remaining
@remaining ||= body.dig('rate', 'remaining').to_i
end
def reset_in
@reset ||= body.dig('rate', 'reset').to_i
end
private
def rate_limit_url
'/rate_limit'
end
def response
connection.get(rate_limit_url)
end
def body
@body ||= Oj.load(response.body, class_cache: false, mode: :compat)
end
# GitHub Rate Limit API returns 404 when the rate limit is disabled
def enabled?
response.status != 404
end
end
end