Displaying articles with tag

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

# 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.

1 comment | Filed Under: Ruby | Tags:

Ruby interface to GitHub API

Posted by admin, Sat Apr 18 01:11:00 UTC 2009

Update: I have made major updates to this API, take a look at this new article.

I spent the last couple of hours baking a Ruby wrapper for the GitHub API v2.

Right now their API interface appears to be down, so I wanted to put this out there and open up for forking.

Here is an example of how the API works:

DSL flavor (work in progress)

include Octopi
connect "fcoury", "<<user-token>>" do |git|
  # the contents of the key whose title is "Local Server"
  puts git.keys.find { |k| k.title == "Local Server" }.key

  # prints current user name
  puts git.user.name

  # sets user name to Fernanda
  # and saves it on GitHub
  git.user.name = "Fernanda"
  git.user.save
end

API flavor

# initializes the API and authenticates the user
github = Octopi::Api.new('fcoury', '<<user-token>>')

# the contents of the key whose title is "Local Server"
puts github.keys.find { |k| k.title == "Local Server" }.key

# retrieves current user information and prints the name
user = github.user
puts user.name

# sets user name to Fernanda
# and saves it on GitHub
user.name = "Fernanda"
user.save

For it away here:
http://github.com/fcoury/octopi

0 comments | Filed Under: Ruby | Tags:

Clicky Web Analytics