Skip to content

Instantly share code, notes, and snippets.

@croaky
Created April 27, 2012 02:45
Show Gist options
  • Save croaky/2505273 to your computer and use it in GitHub Desktop.
Save croaky/2505273 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
require 'json'
class Repo
def initialize(name)
@name = name
end
def forking_authors
forking_author_names.
collect {|name| Author.new(name) }.
sort_by {|author| author.score }.
reverse
end
private
def author
JSON.parse(open(repo_url).read)['repositories'].first['username']
end
def forking_author_names
JSON.parse(open(forks_url).read).map {|fork| fork['owner']['login'] }
end
def forks_url
"https://api.github.com/repos/#{author}/#{@name}/forks"
end
def repo_url
"https://api.github.com/legacy/repos/search/#{@name}"
end
end
class Author
attr_reader :score
def initialize(name)
@name = name
@score = original_repo_count * 3
@score += language_count('Ruby') * 2
@score += language_count('Javascript') * 2
@score += forked_repo_count * 1
end
def stat_types
%w(name public original forked ruby javascript score)
end
def stats
[
@name,
public_repo_count,
original_repo_count,
forked_repo_count,
language_count('Ruby'),
language_count('Javascript'),
score
]
end
private
def forked_repo_count
public_repo_count - original_repo_count
end
def github_profile
@github_profile ||= Nokogiri::HTML(open("https://github.com/#{@name}"))
end
def language_count(language)
github_profile.css('ul.repo-stats').select do |li|
li.text.include? language
end.count
end
def public_repo_count
github_profile.css('.public').count
end
def original_repo_count
github_profile.css('.source').count
end
end
ARGV.each do |query|
puts 'Fetching data...'
begin
puts '%-20s %-10s %-10s %-10s %-10s %-10s %-10s' % Author.stat_types
puts '=' * 80
Repo.new(query).forking_authors.each do |author|
puts '%-20s %-10s %-10s %-10s %-10s %-10s %-10s' % author.stats
end
rescue
puts 'No repo found'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment