Class: OSS::Contributions

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Contributions

Returns a new instance of Contributions.



7
8
9
10
11
12
# File 'lib/oss-contributions.rb', line 7

def initialize(opts={})
  @access_token = nil
  if opts[:access_token]
    @access_token = opts[:access_token]
  end
end

Instance Method Details

#add_access_token(path) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/oss-contributions.rb', line 35

def add_access_token(path)
  if @access_token
    delim = path =~ /\?/ ? "&" : "?"
    "#{path}#{delim}access_token=#{@access_token}"
  else
    path
  end
end

#api(path) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/oss-contributions.rb', line 18

def api(path)
  obj = JSON.parse conn.get(add_access_token path).body
  if obj.is_a?(Hash)
    h = Hashie::Mash.new obj
    if h.message && h.message =~ /API rate limit exceeded/
      raise "API rate limit exceeded"
    elsif h.message
      raise h.message
    end
    h
  elsif obj.is_a?(Array)
    obj.map { |o| Hashie::Mash.new(o) }
  else
    obj
  end
end

#connObject



14
15
16
# File 'lib/oss-contributions.rb', line 14

def conn
  @conn ||= Faraday.new 'https://api.github.com'
end

#is_contributor?(user, owner, repo, page = 1) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/oss-contributions.rb', line 53

def is_contributor?(user,owner,repo,page=1)
  contributors = api("/repos/#{owner}/#{repo}/contributors?page=#{page}")
  answered = contributors.any? do |contributor|
    contributor. == user
  end
  if answered || contributors.empty?
    return answered
  else
    return is_contributor?(user,owner,repo,page+1)
  end
end

#list(user) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/oss-contributions.rb', line 65

def list(user)
  the_list = repos(user).reduce(published: [], contributed: []) do |l, r|
    repo_ok = true
    repo_ok = false if r.private
    type = r.fork ? :contributed : :published
    if r.fork
      r = repo(user, r.name).source
      repo_ok = false unless is_contributor?(user, r.owner., r.name)
    end
    if repo_ok
      l[type] << Hashie::Mash.new({
        name: r.name,
        owner: r.owner.,
        repo: "#{r.owner.}/#{r.name}",
        stars: r.stargazers_count,
        commits: "#{r.html_url}/commits?author=#{user}"
      })
    else
      puts "skippping #{r.name}"
    end
    l
  end
  the_list[ :published   ] = the_list[ :published   ].sort_by(&:stars).reverse
  the_list[ :contributed ] = the_list[ :contributed ].sort_by(&:stars).reverse
  Hashie::Mash.new(the_list)
end

#repo(owner, repo) ⇒ Object



49
50
51
# File 'lib/oss-contributions.rb', line 49

def repo(owner, repo)
  api "/repos/#{owner}/#{repo}"
end

#repos(user) ⇒ Object



44
45
46
47
# File 'lib/oss-contributions.rb', line 44

def repos(user)
  # TODO get multiple pages
  api "/users/#{user}/repos?per_page=100"
end