summaryrefslogtreecommitdiff
path: root/lib/github/rate_limit.rb
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2017-04-11 22:33:54 -0300
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2017-04-24 16:17:52 -0300
commitfc42f3dffa364a360c76ff2785813d74f42016c7 (patch)
tree1bf7e3a340e910756ddde22bd4b8bdb1b6429048 /lib/github/rate_limit.rb
parent7030eb3286613be919e12daf95c43061b49aa5f5 (diff)
downloadgitlab-ce-fc42f3dffa364a360c76ff2785813d74f42016c7.tar.gz
Add basic client for the GitHub API
Diffstat (limited to 'lib/github/rate_limit.rb')
-rw-r--r--lib/github/rate_limit.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/github/rate_limit.rb b/lib/github/rate_limit.rb
new file mode 100644
index 00000000000..30b000f8a9a
--- /dev/null
+++ b/lib/github/rate_limit.rb
@@ -0,0 +1,41 @@
+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 ||= response.body.dig('rate', 'remaining').to_i
+ end
+
+ def reset_in
+ @reset ||= response.body.dig('rate', 'reset').to_i
+ end
+
+ private
+
+ def rate_limit_url
+ '/rate_limit'
+ end
+
+ def response
+ @response ||= connection.get(rate_limit_url)
+ end
+
+ # GitHub Rate Limit API returns 404 when the rate limit is disabled
+ def enabled?
+ response.status != 404
+ end
+ end
+end