Class: Github::API

Inherits:
Object
  • Object
show all
Defined in:
lib/gitget/github_api.rb

Overview

Service for all Github API call

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.configObject



16
17
18
19
20
21
# File 'lib/gitget/github_api.rb', line 16

def self.config
  return @config if @config

  @config = { username: ENV['GH_USERNAME'],
              token:    ENV['GH_TOKEN'] }
end

.config=(credentials) ⇒ Object



12
13
14
# File 'lib/gitget/github_api.rb', line 12

def self.config=(credentials)
  @config ? @config.update(credentials) : @config = credentials
end

.github_api_get(route) ⇒ Object



73
74
75
76
77
# File 'lib/gitget/github_api.rb', line 73

def self.github_api_get(route)
  url = GITHUB_API_URL + route
  response = github_api_wait_cache(url)
  JSON.parse(response.to_s)
end

.github_api_get_http(url) ⇒ Object



60
61
62
# File 'lib/gitget/github_api.rb', line 60

def self.github_api_get_http(url)
  HTTP.basic_auth(user: config[:username], pass: config[:token]).get(url)
end

.github_api_wait_cache(url) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/gitget/github_api.rb', line 64

def self.github_api_wait_cache(url)
  response = github_api_get_http(url)
  while response.headers['Status'].split(' ').first == '202'
    sleep(2)
    response = github_api_get_http(url)
  end
  response
end

.repo_info(owner, repo) ⇒ Object



28
29
30
31
# File 'lib/gitget/github_api.rb', line 28

def self.repo_info(owner, repo)
  route = '/repos/' + owner + '/' + repo
  github_api_get(route)
end

.repo_stat(full_name, stat) ⇒ Object



53
54
55
56
# File 'lib/gitget/github_api.rb', line 53

def self.repo_stat(full_name, stat)
  route = '/repos/' + full_name + '/stats/' + stat
  github_api_get(route)
end

.user_followers(username) ⇒ Object



33
34
35
36
# File 'lib/gitget/github_api.rb', line 33

def self.user_followers(username)
  route = '/users/' + username + '/followers'
  github_api_get(route)
end

.user_following(username) ⇒ Object



38
39
40
41
# File 'lib/gitget/github_api.rb', line 38

def self.user_following(username)
  route = '/users/' + username + '/following'
  github_api_get(route)
end

.user_info(username) ⇒ Object



23
24
25
26
# File 'lib/gitget/github_api.rb', line 23

def self.(username)
  route = '/users/' + username
  github_api_get(route)
end

.user_repos(username) ⇒ Object



48
49
50
51
# File 'lib/gitget/github_api.rb', line 48

def self.user_repos(username)
  route = '/users/' + username + '/repos'
  github_api_get(route)
end

.user_starred(username) ⇒ Object



43
44
45
46
# File 'lib/gitget/github_api.rb', line 43

def self.user_starred(username)
  route = '/users/' + username + '/starred'
  github_api_get(route)
end