Class: GithubPolyglot

Inherits:
Object
  • Object
show all
Defined in:
lib/github_polyglot.rb,
lib/github_polyglot/cli.rb,
lib/github_polyglot/svg.rb,
lib/github_polyglot/svg/language_stats.rb

Overview

Gets language usage stats for a GitHub user.

Defined Under Namespace

Classes: CLI, SVG

Constant Summary collapse

TOKEN_ENV_VAR =
'GITHUB_TOKEN'

Instance Method Summary collapse

Constructor Details

#initialize(username: nil, token: nil) ⇒ GithubPolyglot

Returns a new instance of GithubPolyglot.

Parameters:

  • username (String, Nil) (defaults to: nil)

    The username to look up.

  • token (String, Nil) (defaults to: nil)

    The GitHub token to use in requests.



13
14
15
16
17
# File 'lib/github_polyglot.rb', line 13

def initialize(username: nil, token: nil)
  @username = username
  @token = token
  @client = Octokit::Client.new(access_token: token)
end

Instance Method Details

#json(pretty: false) ⇒ Object

Gets the language stats for the user as JSON.



42
43
44
45
# File 'lib/github_polyglot.rb', line 42

def json(pretty: false)
  options = pretty ? { indent: '  ', space: ' ' } : { indent: '', space: '', array_nl: '', object_nl: '' }
  JSON.pretty_generate(languages, options)
end

#languagesObject

Gets language stats for the user.



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

def languages
  compiled = Hash.new(0)
  each_repo do |repo|
    next if repo[:fork]

    languages = repo_languages(repo.name)
    languages.to_h.each_pair do |language, size|
      compiled[language] += size
    end
  end
  compiled
end

Prints the languages



48
49
50
51
52
# File 'lib/github_polyglot.rb', line 48

def print
  languages.each_pair do |language, amount|
    puts "#{language}: #{amount}"
  end
end

#svgObject

Generates an SVG string for the languages



55
56
57
58
# File 'lib/github_polyglot.rb', line 55

def svg
  generator = SVG.new(languages)
  generator.generate
end

#usernameObject

Gets the username to use.



20
21
22
23
24
25
# File 'lib/github_polyglot.rb', line 20

def username
  return @username unless @username.nil?

  @username = token_username
  @username
end