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:

Mimicking MySQL's AES_ENCRYPT and AES_DECRYPT in pure Ruby

Posted by admin, Tue Mar 24 11:27:00 UTC 2009

I had a problem where I needed to interact with a legacy database that relies on MySQL’s AES_ENCRYPT and AES_DECRYPT functions for data encryption for a couple of fields.

After searching “the intertnets” for it, no ready-made solution was found. So, I started with MySQL documentation on the functions:

AES_ENCRYPT() and AES_DECRYPT() allow encryption and decryption of data using the official AES (Advanced Encryption Standard) gorithm, previously known as “Rijndael.” Encoding with a 128-bit key length is used, but you can extend it up to 256 bits by modifying the source. We chose 128 bits because it is much faster and it is secure enough for most purposes.

AES_ENCRYPT() encrypts a string and returns a binary string. AES_DECRYPT() decrypts the encrypted string and returns the original string. The input arguments may be any length. If either argument is NULL, the result of this function is also NULL.

Because AES is a block-level algorithm, padding is used to encode uneven length strings and so the result string length may be calculated using this formula:

16 × (trunc(string_length / 16) + 1)

If AESDECRYPT() detects invalid data or incorrect padding, it returns NULL. However, it is possible for AESDECRYPT() to return a non-NULL value (possibly garbage) if the input data or the key is invalid.

However, after doing a lot of experimentation (as pointed out on this two posts), I downloaded MySQL code and, even though C is not my thing, I figured something out (with the help of this other blog post):

“The algorithm just creates a 16 byte buffer set to all zero, then loops through all the characters of the string you provide and does an assignment with bitwise OR between the two values. If we iterate until we hit the end of the 16 byte buffer, we just start over from the beginning doing ^=. For strings shorter than 16 characters, we stop at the end of the string.”

And here’s the relevant snippet of MySQL’s source file my_aes.c:

bzero((char*) rkey,AES_KEY_LENGTH/8);      /* Set initial key  */

for (ptr= rkey, sptr= key; sptr < key_end; ptr++,sptr++)
{
  if (ptr == rkey_end)
    ptr= rkey;  /*  Just loop over tmp_key until we used all key */
  *ptr^= (uint8) *sptr;
}

So, I finally came up with the method below, that replicates the same behavior, in Ruby (kudos to Rob Biedenharn, from Ruby Forum, who helped me to refactor this):

def mysql_key(key)
   final_key = "\0" * 16
   key.length.times do |i|
     final_key[i%16] ^= key[i]
   end
   final_key
end

Once you have the correct key, everything becomes a lot simpler. I have used OpenSSL::Cipher::AES128.new("ECB") from openssl built-in library and things worked just fine.

If you ever need to replicate the behavior of those functions on your own Ruby code, here’s a permanent link for a Gist with complete source code:

http://gist.github.com/84093

Hope it helps ;)

934 comments | Filed Under: Ruby | Tags:

Clicky Web Analytics