Class: Userl

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

Overview

The main userl driver

Class Method Summary collapse

Class Method Details

.start(user) ⇒ Object

Get the most used languages of a GitHub user

Example:

>> Userl.start("marcosero")
=> repos and languages infos

Arguments:

user: (String)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/userl.rb', line 16

def self.start(user)
  all_languages = Hash.new
  starred_repos = 0
  forked_repos = 0

  done = false
  t = Thread.new {
        print "Analyzing #{user}'s repos"
        while (!done) do
          print '.'
          sleep 0.5
        end
  }
  t.run

  # Get all repos
  # GET /repos/:owner/repos
  repos_string = URI.parse("https://api.github.com/users/#{user}/repos").read
  repos = JSON.parse(repos_string)

  repos_rows = []

  repos.each do |repository|
    starred_repos += repository["watchers"]
    forked_repos += repository["forks"]
    repos_rows << [repository["name"], repository["watchers"], repository["forks"], repository["language"]]

    # Detect Languages
    # GET /repos/:owner/:repo/languages
    languages_string = URI.parse("https://api.github.com/repos/#{user}/#{repository['name']}/languages").read
    repo_languages = JSON.parse(languages_string)
    all_languages = all_languages.merge(repo_languages){|key, oldval, newval| newval + oldval}
  end

  # prepare repos table
  repos_table = Terminal::Table.new :title => "#{user}'s repositories", :headings => ['Name', 'Stars', 'Forks', 'language'], :rows => repos_rows, :style => {:width => 70} do |t|
    t << :separator
    t.add_row ['', starred_repos, forked_repos]
  end

  # prepare languages table
  languages_rows = []
  all_languages.each do |key, value|
    languages_rows << [key, value]
  end
  languages_rows = languages_rows.sort_by {|k,v| -v}
  languages_table = Terminal::Table.new :title => "#{user}'s languages", :headings => ['Language', 'Bytes'], :rows => languages_rows, :style => {:width => 70}

  done = true
  puts ''
  puts repos_table
  puts languages_table
end