Skip to content

Commit

Permalink
Adding Github connection class and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydreier committed Jul 12, 2011
1 parent 6a5d7a4 commit 9728617
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/github_backup.rb
Expand Up @@ -3,6 +3,8 @@

require 'yaml'
require 'github_backup/configuration'
require 'github_backup/repository'
require 'github_backup/github'

module GithubBackup

Expand Down
30 changes: 30 additions & 0 deletions lib/github_backup/github.rb
@@ -0,0 +1,30 @@
require 'rest_client'
require 'json'

module GithubBackup
class Github

class << self
def api_url
'https://api.github.com'
end
end

attr_accessor :connection, :username, :password

def initialize(username, password)
self.username = username
self.password = password

self.connection = RestClient::Resource.new Github.api_url, :user => username, :password => password
end

# Return a list of the
def repositories
JSON::parse(connection["orgs/#{username}/repos"].get).collect do |repo_hash|
Repository.new(:ssh_url => repo_hash['ssh_url'], :private => repo_hash['private'])
end
end

end
end
26 changes: 26 additions & 0 deletions test/github_test.rb
@@ -0,0 +1,26 @@
require 'test_helper'
require 'fakeweb'

class GithubBackup::GithubTest < Test::Unit::TestCase

def setup
FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:get, GithubBackup::Github.api_url, :body => "Unauthorized", :status => ["401", "Unauthorized"])
FakeWeb.register_uri(:get, 'https://johnnytest:sOOperSecret@api.github.com/orgs/johnnytest/repos', :body => organization_json, :content_type => "application/json")
end

def teardown
FakeWeb.allow_net_connect = true
end

def test_repositories
github = GithubBackup::Github.new('johnnytest', 'sOOperSecret')
assert github.repositories.respond_to?(:size)
assert github.repositories.size > 0
end

def organization_json
@org_contents ||= File.open(File.join(File.expand_path(File.dirname(__FILE__)), 'fixtures', 'repositories_listing.json')).read
end

end

0 comments on commit 9728617

Please sign in to comment.