Class: GithubBackup::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/github_backup/api.rb

Constant Summary collapse

URL =
'https://api.github.com'

Instance Method Summary collapse

Constructor Details

#initialize(token, version = 'v3') ⇒ Api

Returns a new instance of Api.



10
11
12
13
# File 'lib/github_backup/api.rb', line 10

def initialize(token, version = 'v3')
  @token = token
  @version = version
end

Instance Method Details

#acceptObject



15
16
17
# File 'lib/github_backup/api.rb', line 15

def accept
  "application/vnd.github.#{@version}+json"
end

#get(url) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/github_backup/api.rb', line 19

def get(url)
  @response = HTTP[
    'User-Agent' => 'sleepinginsomniac - cloner',
    'Authorization' => "token #{@token}",
    'Accept' => accept
  ].get(URI.join(URL, url))
  JSON.parse(@response)
end

#nextObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/github_backup/api.rb', line 28

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