Class: Contributions::GithubAPI

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

Class Method Summary collapse

Class Method Details

.forks(username) ⇒ Object

Public: Get just the user’s repositories that are forks.

Returns an Array.



10
11
12
13
14
# File 'lib/contributions/github_api.rb', line 10

def self.forks(username)
  forks = self.repos(username).select { |r| r["fork"] == true }
  repo_names = forks.map { |r| r["owner"]["login"] + '/' + r["name"] }
  repo_names.map { |r| self.parent(r) }
end

.name(username) ⇒ Object

Public: Get the name of the user.

username - github username.

Returns a String.



21
22
23
# File 'lib/contributions/github_api.rb', line 21

def self.name(username)
  self.user(username)["name"]
end

.parent(repository) ⇒ Object

Public: Get the name of the forked repository.

repository - a ‘username/repository_name’ string.

Returns a String.



30
31
32
33
34
# File 'lib/contributions/github_api.rb', line 30

def self.parent(repository)
  username, repo_name = repository.split('/')
  repo_info = self.repository(repository)
  repo_info["parent"]["owner"]["login"] + '/' + repo_name
end

.repos(username) ⇒ Object

Public: Get all the user’s repositories.

Returns an Array.



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

def self.repos(username)
  JSON.parse(open("https://api.github.com/users/#{username}/repos?per_page=100") { |f| f.read } )
end

.repository(repository) ⇒ Object

Internal: Get the repository info (all of it) from github.

repository - a ‘username/repository_name’ string.

Returns a Hash.



57
58
59
# File 'lib/contributions/github_api.rb', line 57

def self.repository(repository)
  JSON.parse(open("https://api.github.com/repos/#{repository}") { |f| f.read } )
end

.user(username) ⇒ Object

Internal: Get the user info (all of it) from github.

username - github username.

Returns a Hash.



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

def self.user(username)
  JSON.parse(open("https://api.github.com/users/#{username}") { |f| f.read } )
end