Skip to content

Commit

Permalink
Updated Github.js. Dropped jQuery dependency and adds support for gists.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael committed Jul 19, 2012
1 parent 4850dc5 commit dfc5780
Showing 1 changed file with 140 additions and 70 deletions.
210 changes: 140 additions & 70 deletions _includes/vendor/github.js
@@ -1,4 +1,4 @@
// Github.js 0.6.1
// Github.js 0.6.2
// (c) 2012 Michael Aufreiter, Development Seed
// Github.js is freely distributable under the MIT license.
// For all details and documentation:
Expand All @@ -13,38 +13,30 @@
// Util
// =======

function headers() {
var headers = {}
if (options.auth === 'oauth' && !options.token) return { Accept: 'application/vnd.github.raw' };
if (options.auth === 'basic' && (!options.username || !options.password)) return { Accept: 'application/vnd.github.raw' };
return options.auth == 'oauth'
? { Authorization: 'token '+ options.token, Accept: 'application/vnd.github.raw' }
: { Authorization : 'Basic ' + Base64.encode(options.username + ':' + options.password), Accept: 'application/vnd.github.raw' }
}

function _request(method, path, data, cb) {
$.ajax({
type: method,
url: API_URL + path,
data: JSON.stringify(data),
dataType: 'json',
contentType: 'application/json',
success: function(res) { cb(null, res); },
error: function(err) { cb(err); },
headers : headers()
});
}

function _raw_request(method, path, data, cb) {
$.ajax({
type: method,
url: API_URL + path,
data: JSON.stringify(data),
contentType: 'application/json',
success: function(res) { cb(null, res); },
error: function(err) { cb(err); },
headers : headers()
});
function _request(method, path, data, cb, raw) {
var xhr = new XMLHttpRequest();
if (!raw) {xhr.dataType = "json"}
xhr.open(method, API_URL + path);
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300) {
cb(null, raw ? this.responseText : JSON.parse(this.responseText));
} else {
cb(this.status);
}
}
}
xhr.setRequestHeader('Accept','application/vnd.github.raw');
if (
(options.auth == 'oauth' && options.token) ||
(options.auth == 'basic' && options.username && options.password)
) {
xhr.setRequestHeader('Authorization',options.auth == 'oauth'
? 'token '+ options.token
: 'Basic ' + Base64.encode(options.username + ':' + options.password)
);
}
data ? xhr.send(JSON.stringify(data)) : xhr.send();
}

// User API
Expand Down Expand Up @@ -93,39 +85,13 @@
});
};

// List user organizations
// List all user gists
// This will return all public gists if user is not authenticated
// -------

this.orgs = function(cb) {
_request("GET", "/user/orgs", null, function(err, res) {
cb(err, res);
});
};

// Show user information
// -------

this.show = function(username, cb) {
_request("GET", "/users/"+username, null, function(err, res) {
cb(err, res);
});
}

// List user repositories
// -------

this.userRepos = function(username, cb) {
_request("GET", "/users/"+username+"/repos?type=all&per_page=100", null, function(err, res) {
cb(err, res);
});
};

// List organization repositories
// -------

this.orgRepos = function(orgname, cb) {
_request("GET", "/orgs/"+orgname+"/repos?type=all&per_page=100", null, function(err, res) {
cb(err, res);
this.userGists = function(username,cb) {
_request("GET", "/gists", null, function(err, res) {
cb(err,res);
});
};
};
Expand Down Expand Up @@ -182,7 +148,7 @@
// -------

this.getBlob = function(sha, cb) {
_raw_request("GET", repoPath + "/git/blobs/" + sha, null, cb);
_request("GET", repoPath + "/git/blobs/" + sha, null, cb, 'raw');
};

// For a given file path, get the corresponding sha (blob for files, tree for dirs)
Expand Down Expand Up @@ -357,13 +323,9 @@

this.write = function(branch, path, content, message, cb) {
updateTree(branch, function(err, latestCommit) {
if (err) return cb(err);
that.postBlob(content, function(err, blob) {
if (err) return cb(err);
that.updateTree(latestCommit, path, blob, function(err, tree) {
if (err) return cb(err);
that.commit(latestCommit, tree, message, function(err, commit) {
if (err) return cb(err);
that.updateHead(branch, commit, cb);
});
});
Expand All @@ -372,6 +334,110 @@
};
};

// Gists API
// =======

Github.Gist = function(options) {
var id = options.id;
var that = this;
var gistPath = "/gists/"+id;

// Read the gist
// --------

this.show = function(cb) {
_request("GET", gistPath, null, function(err,info) {
cb(err,info);
});
};

// Star the gist
// --------

this.star = function(cb) {
_request("PUT", gistPath+"/star", null, function(err,res) {
cb(err,res);
});
}

// Check if the Gist is starred
// --------

this.isStarred = function(cb) {
_request("GET", gistPath+"/star", null, function(err,res) {
cb(err,res);
});
};

// Unstar the gist
// --------

this.unstar = function(cb) {
_request("DELETE", gistPath+"/star", null, function(err,res) {
cb(err,res);
});
};

// Delete the gist
// --------

this.delete = function(cb) {
_request("DELETE", gistPath, null, function(err,res) {
cb(err,res);
});
};

// Fork a gist
// --------

this.fork = function(cb) {
_request("POST", gistPath+"/fork", null, function(err,res) {
cb(err,res);
});
};

// Update a gist with the new stuff
// --------

this.edit = function(cb,options) {
_request("PATCH", gistPath, options, function(err,res) {
cb(err,res);
});
};

// Update Gist Description
// --------

this.updateDescription = function(cb,description) {
that.edit(cb,{description: description});
};

// Rename a file in the gist
// --------

this.renameFile = function(oldName, newName, cb) {
var options = {files:{}};
options.files[oldName] = newName;
that.edit(cb, options);
};

// Delete a file in the gist
// --------

this.deleteFile = function(fileName, cb) {
var options = {files:{}};
options.files[fileName] = null;
that.edit(cb,options);
};

// Update a particular file in the gist
this.updateFile = function(filename, contents, cb) {
var options = {files:{}};
options.files[filename] = {content: contents};
that.edit(cb,options);
}
};

// Top Level API
// -------

Expand All @@ -382,5 +448,9 @@
this.getUser = function() {
return new Github.User();
};

this.getGist = function(id) {
return new Github.Gist({id: id});
};
};
}).call(this);
}).call(this);

0 comments on commit dfc5780

Please sign in to comment.