summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2017-04-24 19:49:17 -0300
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2017-04-24 19:49:17 -0300
commit275f00ee88cb9ef6ae3ae7879498a0ed4f448681 (patch)
tree6718fd49e31f1f72cec720236815e8cacf0c452f
parentf73a0280c96f76b1b19200990ab07adc732dbb92 (diff)
downloadgitlab-ce-275f00ee88cb9ef6ae3ae7879498a0ed4f448681.tar.gz
Refactoring Github::RateLimit
-rw-r--r--lib/github/client.rb8
-rw-r--r--lib/github/rate_limit.rb33
2 files changed, 14 insertions, 27 deletions
diff --git a/lib/github/client.rb b/lib/github/client.rb
index 95536cae57f..bf1b2b0a523 100644
--- a/lib/github/client.rb
+++ b/lib/github/client.rb
@@ -1,17 +1,19 @@
module Github
class Client
- attr_reader :connection
+ attr_reader :connection, :rate_limit
def initialize(options)
@connection = Faraday.new(url: options.fetch(:url)) do |faraday|
faraday.authorization 'token', options.fetch(:token)
faraday.adapter :net_http
end
+
+ @rate_limit = RateLimit.new(connection)
end
def get(url, query = {})
- rate_limit = RateLimit.new(connection)
- sleep rate_limit.reset_in if rate_limit.exceed?
+ exceed, reset_in = rate_limit.get
+ sleep reset_in if exceed
Github::Response.new(connection.get(url, query))
end
diff --git a/lib/github/rate_limit.rb b/lib/github/rate_limit.rb
index ab2b9c707d8..884693d093c 100644
--- a/lib/github/rate_limit.rb
+++ b/lib/github/rate_limit.rb
@@ -10,33 +10,18 @@ module Github
@connection = connection
end
- def exceed?
- return false unless enabled?
+ def get
+ response = connection.get(RATE_LIMIT_URL)
- 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
+ # GitHub Rate Limit API returns 404 when the rate limit is disabled
+ return false unless response.status != 404
- private
-
- def response
- connection.get(RATE_LIMIT_URL)
- end
-
- def body
- @body ||= Oj.load(response.body, class_cache: false, mode: :compat)
- end
+ 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
- # GitHub Rate Limit API returns 404 when the rate limit is disabled
- def enabled?
- response.status != 404
+ [exceed, reset_in]
end
end
end