Module: Ghstars

Defined in:
lib/ghstars.rb,
lib/ghstars/cli.rb,
lib/ghstars/version.rb

Defined Under Namespace

Classes: CLI, Error, GitHubAccessTokenError, GitHubBadCredentialsError, InvalidOwnerError

Constant Summary collapse

VERSION =
"0.1.6"
@@client =
nil

Class Method Summary collapse

Class Method Details

.check_github_credentialsObject



16
17
18
19
20
21
22
23
# File 'lib/ghstars.rb', line 16

def self.check_github_credentials
  begin
    @@client.user
  rescue Octokit::Unauthorized => error_message
    puts error_message
    raise ::Ghstars::GitHubBadCredentialsError, "Bad GitHub credentials."
  end
end

.check_github_token_definedObject



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

def self.check_github_token_defined
  raise ::Ghstars::GitHubAccessTokenError, "Please define GITHUB_TOKEN environment variable." unless ENV['GITHUB_TOKEN']
end

.get_repos(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ghstars.rb', line 45

def self.get_repos(options)
  if ::Ghstars.github_access_available?
    query = options[:query]
    repos = @@client.repos({}, query: query)
      .reject{|r| r[:stargazers_count] == 0}
      .reject{|r| r[:fork]}
      .sort_by{|r| r[:stargazers_count]}
      .reverse
      repos
  end
end

.github_access_available?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ghstars.rb', line 25

def self.github_access_available?
  begin
    ::Ghstars.check_github_token_defined
  rescue ::Ghstars::GitHubAccessTokenError => error_message
    puts error_message
    exit
  end

  @@client = Octokit::Client.new(:access_token => ENV['GITHUB_TOKEN'])
  @@client.auto_paginate = true
  
  begin
    ::Ghstars.check_github_credentials
  rescue ::Ghstars::GitHubBadCredentialsError => error_message
    puts error_message
    exit
  end
  true
end