Skip to content

Instantly share code, notes, and snippets.

@spejman
Created January 17, 2009 23:17
Show Gist options
  • Save spejman/48486 to your computer and use it in GitHub Desktop.
Save spejman/48486 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'net/http'
class GitHub
def initialize(username)
@username = username
end
def data
url = "http://github.com/api/v1/json/#{username}"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
return result
end
def username
@username
end
def repositories
repos = []
data['user']['repositories'].each { |key, value| repos << key['name'] }
return repos
end
def name
data['user']['name']
end
end
if ARGV.size == 1
GitHub.new(ARGV[0]).repositories.each do |repo|
puts "[#{repo}]"
if !File.exist?(repo)
puts "=> Cloning"
system "git clone -q git@github.com:#{ARGV[0]}/#{repo}.git"
else
puts "=> Pulling & Pushing"
system "cd #{repo} && git pull && git push"
end
end
else
puts "=> Please provide your GitHub username"
end
require 'test/unit'
require 'github'
class TestGitHub < Test::Unit::TestCase
def setup
@github = GitHub.new('fesplugas')
end
def test_should_return_name
assert_equal "Francesc Esplugas", @github.name
end
def test_should_return_repositories_list
assert @github.repositories.include?('typus')
end
def test_error
@github = GitHub.new('fesplugues')
assert_raise JSON::ParserError do
@github.data
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment