BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
275f00ee
Commit
275f00ee
authored
Apr 24, 2017
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring Github::RateLimit
parent
f73a0280
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
27 deletions
+14
-27
client.rb
lib/github/client.rb
+5
-3
rate_limit.rb
lib/github/rate_limit.rb
+9
-24
No files found.
lib/github/client.rb
View file @
275f00ee
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
r
ate_limit
.
reset_in
if
rate_limit
.
exceed?
exceed
,
reset_in
=
rate_limit
.
get
sleep
r
eset_in
if
exceed
Github
::
Response
.
new
(
connection
.
get
(
url
,
query
))
end
...
...
lib/github/rate_limit.rb
View file @
275f00ee
...
...
@@ -10,33 +10,18 @@ module Github
@connection
=
connection
end
def
exceed?
re
turn
false
unless
enabled?
def
get
re
sponse
=
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment