Skip to content

Instantly share code, notes, and snippets.

@taiansu
Created January 31, 2012 04:53
Show Gist options
  • Save taiansu/1708909 to your computer and use it in GitHub Desktop.
Save taiansu/1708909 to your computer and use it in GitHub Desktop.
GetUserCollabraotrsRedesign
require "httparty"
class GitRepos
include HTTParty
base_uri "https://api.github.com"
headers 'Accept' => 'application/json'
format :json
def get_repos_by_user(user)
repos = self.class.get("/users/#{user}/repos")
end
#might call this by asynchronous or currency way.
def get_collaborators_by_repo(user, repo_name)
collaborators = self.class.get("/repos/#{user}/#{repo_name}/collaborators")
end
def list_collaborators_by_user(user, &block)
repo_hash = { }
#get all the repos via http
repos = get_repos_by_user(user)
#launch http get for each repo
repos.each do |repo|
puts "#{repo["name"]} => \n"
collaborators = get_collaborators_by_repo(user, repo["name"])
#store collaborators into hash for return
repo_hash[repo["name"]] = collaborators
#display each result
if block_given?
block.call(collaborators)
end
end
return repo_hash
end
end
#Run!
if __FILE__ == $0
git_repos = GitRepos.new
git_repos.list_collaborators_by_user("datamapper"){ |collaborators|
collaborators.each{ |collaborator| puts " #{collaborator["login"]}," }
puts #link break
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment