-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
40 lines (35 loc) · 814 Bytes
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'link_header'
require 'json'
require 'uri'
require 'http'
module GithubBackup
class Api
URL = 'https://api.github.com'
def initialize(token, version = 'v3')
@token = token
@version = version
end
def accept
"application/vnd.github.#{@version}+json"
end
def get(url)
@response = HTTP[
'User-Agent' => 'sleepinginsomniac - cloner',
'Authorization' => "token #{@token}",
'Accept' => accept
].get(URI.join(URL, url))
JSON.parse(@response)
end
def next
links = LinkHeader.parse(@response.headers['Link']).links
next_link = links.find do |link|
Hash[link.attr_pairs]['rel'] == 'next'
end
if next_link
get(next_link.href)
else
false
end
end
end
end