Module: OTR

Defined in:
lib/otr.rb

Constant Summary collapse

VERSION =
"0.1.3"
USER_AGENT =
"otr.jupo.org"

Class Method Summary collapse

Class Method Details

.get(options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/otr.rb', line 48

def self.get(options)
  repos = self.get_bb(options[:bitbucket_username] || options[:username])
  self.get_gh(options[:github_username] || options[:username]).each do |repo|
    name = repo["name"]
    repos[name] ||= {"forks" => 0, "watchers" => 0, "urls" => []}
    repos[name]["urls"] << repo["url"]
    %w[watchers forks].each { |k| repos[name][k] += repo[k] }
    %w[fork name].each { |k| repos[name][k] ||= repo[k] }
  end
  repos.values
end

.get_bb(username) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/otr.rb', line 15

def self.get_bb(username)
  repos = {}
  url = "https://api.bitbucket.org/1.0/users/#{username}/"
  self.get_json(url)["repositories"].map { |repo|
    Thread.new repo do |repo|
      name = repo["slug"]
      url = "https://bitbucket.org/#{username}/#{name}/descendants"
      html = RestClient.get(url).split("<span class=\"value\">")
      repos[name] = {
        "name" => name,
        "watchers" => html[4].to_i,
        "forks" => html[3].to_i,
        "fork" => repo["is_fork"],
        "urls" => ["https://bitbucket.org/#{username}/#{name}"]
      }
    end
  }.each { |thread| thread.join }
  repos
end

.get_gh(username) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/otr.rb', line 35

def self.get_gh(username)
  page = 0
  all_repos = []
  while true
    page += 1
    url = "https://api.github.com/users/#{username}/repos?page=#{page}"
    repos = self.get_json(url)
    break if repos.count == 0
    all_repos += repos
  end
  all_repos
end

.get_json(url) ⇒ Object



11
12
13
# File 'lib/otr.rb', line 11

def self.get_json(url)
  JSON.parse(RestClient.get(url, :user_agent => USER_AGENT))
end