GitHub API v2 on Ruby, take 2
Posted by admin,
Sun Apr 19 12:50:00 UTC 2009
Major updates to octopi library (that is a Ruby interface for the GitHub API v2).
Gem installation
Yes! Now you can install octopi as a Gem:
$ sudo gem install fcoury-octopi --source http://gems.github.com
|
Progress
I am now focusing on having complete coverage of the API features that anyone can do as an anonymous user (not authenticated to GitHub, that is).
Once this part is done we’ll start working on features that requires authentication.
Some code snippets for your delight:
User API
Getting user information
u = User.find("fcoury")
puts "#{u.name} followed by #{u.followers.join(", ")},
following #{u.following.join(", ")}"
|
Followers and following, rendered as an User object collection
The bang version of followers and following creates one User object for each user login found and obviously is a lot more expensive and should be used with parsimony.
user.followers!.each do |u|
puts " - #{u.name} (#{u.login}) has
#{u.public_repo_count} repo(s)"
end
|
Searching users
users = User.find_all("silva")
puts "#{users.size} users found for 'silva':"
users.each do |u|
puts " - #{u.name}"
end
|
Repositories API
Search
repos = Repository.find_all("ruby", "git")
puts "#{repos.size} repository(ies) with 'ruby' and 'git':"
repos.each do |r|
puts " - #{r.name}"
end
|
Getting a repository
repo = Repository.find("fcoury", "octopi")
|
Getting a repository for a given user
If you have an User object, you can easily do:
repo = user.repository("octopi")
|
Repository information
puts "#{repo.name} - #{repo.description}
(by #{repo.owner}) - #{repo.url}"
|
Tags
tags = repo.tags.map {|t| t.name}.join(", ")
puts "Tags: #{tags}"
|
Commits API
Commits of a given repository:
fc = repo.commits.first
puts "First commit: "
puts "#{fc.id} - #{fc.message} - by #{fc.author['name']}"
|
Single commit information:
puts "Diff:"
fc.details.modified.each do |m|
puts "#{m['filename']} DIFF: #{m['diff']}"
end
|
Stay tuned for upcoming updates.
Filed Under: Ruby |
Tags: api github ruby