From 97286172bb2a658d842c5acbcc1c8a26073d587e Mon Sep 17 00:00:00 2001 From: Casey Dreier Date: Mon, 11 Jul 2011 23:13:19 -0400 Subject: [PATCH] Adding Github connection class and test. --- lib/github_backup.rb | 2 ++ lib/github_backup/github.rb | 30 ++++++++++++++++++++++++++++++ test/github_test.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/github_backup/github.rb create mode 100644 test/github_test.rb diff --git a/lib/github_backup.rb b/lib/github_backup.rb index b034774..92bdafd 100644 --- a/lib/github_backup.rb +++ b/lib/github_backup.rb @@ -3,6 +3,8 @@ require 'yaml' require 'github_backup/configuration' +require 'github_backup/repository' +require 'github_backup/github' module GithubBackup diff --git a/lib/github_backup/github.rb b/lib/github_backup/github.rb new file mode 100644 index 0000000..89390e3 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/github_test.rb b/test/github_test.rb new file mode 100644 index 0000000..18c968f --- /dev/null +++ b/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 \ No newline at end of file