Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for all documented methods in Gist v1 API. #15

Merged
merged 2 commits into from
Jul 28, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Note that the _authenticate_ method is synchronous because it only stores the cr
* Repository API: almost complete. Only _create_ and _delete_ repository is missing
* Issues API: only _getList_ is implemented
* Pulls API: only _getList_ is implemented
* Gist API: still missing
* Gist API: 100% v1 Gist API implemented. 0% v3 API
* Network API: still missing

## Running the Tests
Expand Down
15 changes: 15 additions & 0 deletions lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,19 @@ var GitHubApi = exports.GitHubApi = function(debug, proxy, http) {
return this.$apis['commit'];
};

/**
* Get the Gist API
*
* @return {GistApi} the gist API
*/
this.getGistApi = function()
{
if(!this.$apis['gist']) {
this.$apis['gist'] = new (require('./github/GistApi').GistApi)(this);
}

return this.$apis['gist'];
};


}).call(GitHubApi.prototype);
71 changes: 71 additions & 0 deletions lib/github/GistApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright 2011 Ajax.org B.V.
*
* This product includes software developed by
* Ajax.org B.V. (http://www.ajax.org/).
*
* Author: James Burkhart <[email protected]>
*/

var sys = require('sys');
var AbstractApi = require('./AbstractApi').AbstractApi;
var GistApi = exports.GistApi = function(api) {
this.$api = api;
};

sys.inherits(GistApi, AbstractApi);

(function() {
/**
* Get public gists for a user
* http://develop.github.com/p/gist.html
*
* @param {String} $username the user's username
*/
this.getUserGists = function(username, callback)
{
this.$api.get(
'gists/' + username,
null,
this.requestOptions,
this.$createListener(callback, 'gists')
);
};
/**
* Get metadata for a gist
* http://develop.github.com/p/gist.html
*
* @param {String} $gistId the id of the gist
*/
this.getMetadata = function(gistId, callback)
{
this.$api.get(
'' + gistId,
null,
this.requestOptions,
this.$createListener(callback, 'gists')
);

};
/**
* Get the content of a gist
* http://develop.github.com/p/gist.html
*
* @param {String} $gistId the id of the gist
* @param {String} $filename the filename of the gist
*/
this.getContent = function(gistId, filename, callback)
{
this.$api.get(
'' + gistId + '/' + filename,
null,
{hostname: 'raw.github.com', path: '/gist', format: ''},
this.$createListener(callback, null)
);

};

this.requestOptions = {hostname: 'gist.github.com', path: '/api/v1'};

}).call(GistApi.prototype);

51 changes: 51 additions & 0 deletions lib/github/GistApiTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2010 Ajax.org B.V.
*
* This product includes software developed by
* Ajax.org B.V. (http://www.ajax.org/).
*
* Author: James Burkhart <[email protected]>
*/

require('../../support/paths');

var assert = require('assert');
var GitHubApi = require('github').GitHubApi;

var test = module.exports = {

setUp: function() {
this.github = new GitHubApi(true);
this.gistApi = this.github.getGistApi();
},

"test: get user gists" : function(finished) {
this.gistApi.getUserGists('fourktest', function(err, gists) {
assert.equal(err, null);
assert.ok(gists.length);
assert.equal(gists[0].owner, 'fourktest');
assert.equal(gists[0].comments[0].user, 'fourktest');
finished();
});
},
"test: get metadata" : function(finished) {
this.gistApi.getMetadata('1091209', function(err, gists) {
assert.equal(err, null);
assert.ok(gists.length);
assert.equal(gists[0].owner, 'fourktest');
assert.equal(gists[0].comments[0].user, 'fourktest');
assert.equal(gists[0].repo, '1091209');
finished();
});
},
"test: get content" : function(finished) {
this.gistApi.getContent('374130', 'ports.sh', function(err, gist) {
assert.equal(err, null);
assert.ok(gist.length);
assert.equal(gist.length, 68);
finished();
});
}
};

!module.parent && require("asyncjs/test").testcase(module.exports).exec();
5 changes: 3 additions & 2 deletions lib/github/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ var Request = exports.Request = function(options) {
var port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443;

var headers = {
'Host':'github.com',
'Host':host,
"User-Agent": "NodeJS HTTP Client",
"Content-Length": "0"
};
Expand Down Expand Up @@ -192,6 +192,7 @@ var Request = exports.Request = function(options) {
this.$debug("get: "+ getQuery + " post " + postQuery);

var path = this.$options.path + "/" + this.$options.format + "/" + apiPath.replace(/\/*$/, "");
var path = path.replace('//','/');
if (getQuery)
path += "?" + getQuery;

Expand Down Expand Up @@ -243,7 +244,7 @@ var Request = exports.Request = function(options) {
*/
this.decodeResponse = function(response)
{
if(this.$options['format'] === "text") {
if(this.$options['format'] === "text" || this.$options['hostname'] === 'raw.github.com') {
return response;
}
else if(this.$options['format'] === "json")
Expand Down