Skip to content

Instantly share code, notes, and snippets.

@smtlaissezfaire
Forked from nakajima/ego_hub.rb
Created September 18, 2008 06:58
Show Gist options
  • Save smtlaissezfaire/11392 to your computer and use it in GitHub Desktop.
Save smtlaissezfaire/11392 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'hpricot'
require 'open-uri'
GITHUB = "http://github.com/"
FOLLOWER_COUNT_SELECTOR = '#repo_sub_menu li:nth-of-type(1)'
PROJECT_SELECTOR = 'li.project .title b a'
class String
def parse_int
scan(/\d+/).to_s.to_i
end
end
class Array
def invoke(method)
map { |i| i.send(method) }
end
def sum
inject(0) { |sum, i| sum + i }
end
end
class Page
def find(css)
path = GITHUB + page
begin
yield Hpricot(open(path)).search(css)
rescue OpenURI::HTTPError
puts "An error occurred while trying to access #{path}. Exiting..."
exit
end
end
end
class User < Page
def initialize(name)
@name = name
end
def projects
puts "finding #{@name}'s projects"
find(PROJECT_SELECTOR) do |result|
count = result.map { |p| Project.new(p.inner_html, @name) }
puts "(#{count.length} projects found)"
count
end
end
def total_followers
projects.invoke(:follower_count).sum
end
def page
@name
end
end
class Project < Page
def initialize(name, user)
@name, @user = name, user
end
def follower_count
find(FOLLOWER_COUNT_SELECTOR) do |result|
count = result.inner_html.parse_int
puts " #{count} followers for #{@name}"
count
end
end
def page
"#{@user}/#{@name}/info/feed"
end
end
print "Enter the username: "
user = User.new(gets.chomp)
puts "#{user.total_followers} people are following your projects."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment