octonode
Advanced tools
Comparing version 0.2.3 to 0.3.0
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var octonode; |
@@ -1,7 +0,9 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var auth, qs, request; | ||
var auth, qs, request, url; | ||
request = require('request'); | ||
url = require('url'); | ||
qs = require('querystring'); | ||
@@ -26,7 +28,13 @@ | ||
revoke: function(id, callback) { | ||
var options; | ||
if (this.mode === this.modes.cli) { | ||
return request({ | ||
url: "https://" + this.options.username + ":" + this.options.password + "@api.github.com/authorizations/" + id, | ||
method: 'DELETE' | ||
}, function(err, res, body) { | ||
options = { | ||
url: url.parse("https://api.github.com/authorizations/" + id), | ||
method: 'DELETE', | ||
headers: { | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
}; | ||
options.url.auth = "" + this.options.username + ":" + this.options.password; | ||
return request(options, function(err, res, body) { | ||
if ((err != null) || res.statusCode !== 204) { | ||
@@ -43,3 +51,3 @@ return callback(err || new Error(JSON.parse(body).message)); | ||
login: function(scopes, callback) { | ||
var uri; | ||
var options, uri; | ||
if (this.mode === this.modes.cli) { | ||
@@ -53,19 +61,26 @@ if (scopes instanceof Array) { | ||
} | ||
return request({ | ||
url: "https://" + this.options.username + ":" + this.options.password + "@api.github.com/authorizations", | ||
options = { | ||
url: url.parse("https://api.github.com/authorizations"), | ||
method: 'POST', | ||
body: scopes, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
}, function(err, res, body) { | ||
try { | ||
body = JSON.parse(body); | ||
} catch (err) { | ||
callback(new Error('Unable to parse body')); | ||
} | ||
if (res.statusCode === 401) { | ||
return callback(new Error(body.message)); | ||
}; | ||
options.url.auth = "" + this.options.username + ":" + this.options.password; | ||
return request(options, function(err, res, body) { | ||
if (err != null) { | ||
return callback(err); | ||
} else { | ||
return callback(null, body.id, body.token); | ||
try { | ||
body = JSON.parse(body); | ||
} catch (err) { | ||
callback(new Error('Unable to parse body')); | ||
} | ||
if (res.statusCode === 401) { | ||
return callback(new Error(body.message)); | ||
} else { | ||
return callback(null, body.id, body.token); | ||
} | ||
} | ||
@@ -88,3 +103,4 @@ }); | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
@@ -91,0 +107,0 @@ }, function(err, res, body) { |
@@ -1,4 +0,6 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var Client, Gist, Me, Org, Repo, Team, User, request; | ||
var Client, Gist, Me, Org, Pr, Repo, Search, Team, User, request, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
__slice = [].slice; | ||
@@ -19,2 +21,6 @@ request = require('request'); | ||
Pr = require('./pr'); | ||
Search = require('./search'); | ||
Client = (function() { | ||
@@ -24,2 +30,4 @@ | ||
this.token = token; | ||
this.limit = __bind(this.limit, this); | ||
} | ||
@@ -51,3 +59,11 @@ | ||
Client.prototype.query = function(path) { | ||
Client.prototype.pr = function(repo, number) { | ||
return new Pr(repo, number, this); | ||
}; | ||
Client.prototype.search = function() { | ||
return new Search(this); | ||
}; | ||
Client.prototype.query = function(path, page, per_page) { | ||
var uri; | ||
@@ -57,2 +73,8 @@ if (path == null) { | ||
} | ||
if (page == null) { | ||
page = null; | ||
} | ||
if (per_page == null) { | ||
per_page = null; | ||
} | ||
if (path[0] !== '/') { | ||
@@ -62,5 +84,13 @@ path = '/' + path; | ||
uri = "https://"; | ||
uri += typeof this.token === 'object' ? "" + this.token.username + ":" + this.token.password + "@" : ''; | ||
uri += "api.github.com" + path; | ||
return uri += typeof this.token === 'string' ? "?access_token=" + this.token : ''; | ||
uri += typeof this.token === 'object' && this.token.username ? "" + this.token.username + ":" + this.token.password + "@" : ''; | ||
uri += "api.github.com" + path + "?"; | ||
uri += typeof this.token === 'string' ? "access_token=" + this.token : ''; | ||
uri += typeof this.token === 'object' && this.token.id ? "client_id=" + this.token.id + "&client_secret=" + this.token.secret : ''; | ||
if (page != null) { | ||
uri += "&page=" + page; | ||
} | ||
if (per_page != null) { | ||
uri += "&per_page=" + per_page; | ||
} | ||
return uri; | ||
}; | ||
@@ -87,12 +117,12 @@ | ||
Client.prototype.get = function(path, headers, callback) { | ||
var _this = this; | ||
if (!callback || typeof headers === 'function') { | ||
callback = headers; | ||
headers = {}; | ||
} | ||
Client.prototype.get = function() { | ||
var callback, params, path, _i, | ||
_this = this; | ||
path = arguments[0], params = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), callback = arguments[_i++]; | ||
return request({ | ||
uri: this.query(path), | ||
uri: this.query.apply(this, [path].concat(__slice.call(params))), | ||
method: 'GET', | ||
headers: headers | ||
headers: { | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
}, function(err, res, body) { | ||
@@ -108,5 +138,2 @@ if (err) { | ||
var _this = this; | ||
if (content == null) { | ||
content = {}; | ||
} | ||
return request({ | ||
@@ -117,3 +144,4 @@ uri: this.query(path), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
@@ -130,5 +158,2 @@ }, function(err, res, body) { | ||
var _this = this; | ||
if (content == null) { | ||
content = {}; | ||
} | ||
return request({ | ||
@@ -139,3 +164,4 @@ uri: this.query(path), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
@@ -152,5 +178,2 @@ }, function(err, res, body) { | ||
var _this = this; | ||
if (content == null) { | ||
content = {}; | ||
} | ||
return request({ | ||
@@ -161,3 +184,4 @@ uri: this.query(path), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0' | ||
} | ||
@@ -172,2 +196,15 @@ }, function(err, res, body) { | ||
Client.prototype.limit = function(callback) { | ||
return this.get('/rate_limit', function(err, s, b) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (s !== 200) { | ||
return callback(new Error('Client rate_limit error')); | ||
} else { | ||
return callback(null, b.rate.remaining, b.rate.limit); | ||
} | ||
}); | ||
}; | ||
return Client; | ||
@@ -177,6 +214,12 @@ | ||
module.exports = function(token) { | ||
return new Client(token); | ||
module.exports = function() { | ||
var credentials, token; | ||
token = arguments[0], credentials = 2 <= arguments.length ? __slice.call(arguments, 1) : []; | ||
return (function(func, args, ctor) { | ||
ctor.prototype = func.prototype; | ||
var child = new ctor, result = func.apply(child, args); | ||
return Object(result) === result ? result : child; | ||
})(Client, [token].concat(__slice.call(credentials)), function(){}); | ||
}; | ||
}).call(this); |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
@@ -111,2 +111,15 @@ var Gist; | ||
Gist.prototype.fork = function(id) { | ||
return this.client.post("/gists/" + id + "/forks", {}, function(err, s, b) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 201) { | ||
return cb(new Error('Gist fork error')); | ||
} else { | ||
return cb(null, b); | ||
} | ||
}); | ||
}; | ||
Gist.prototype.star = function(id) { | ||
@@ -113,0 +126,0 @@ var _this = this; |
@@ -1,9 +0,5 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var Me, Org, User; | ||
var Me; | ||
User = require('./user'); | ||
Org = require('./org'); | ||
Me = (function() { | ||
@@ -221,3 +217,3 @@ | ||
Me.prototype.org = function(name) { | ||
return new Org(name, this.client); | ||
return this.client.org(name); | ||
}; | ||
@@ -238,2 +234,6 @@ | ||
Me.prototype.repo = function(name) { | ||
return this.client.repo(name); | ||
}; | ||
Me.prototype.repos = function(cbOrRepo, cb) { | ||
@@ -283,2 +283,6 @@ if (typeof cb === 'function' && typeof cbOrRepo === 'object') { | ||
Me.prototype.pr = function(repo, number) { | ||
return this.client.pr(repo, number); | ||
}; | ||
return Me; | ||
@@ -285,0 +289,0 @@ |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
@@ -25,2 +25,15 @@ var Org; | ||
Org.prototype.update = function(info, cb) { | ||
return this.client.post("/orgs/" + this.name, info, function(err, s, b) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 200) { | ||
return cb(new Error('Org update error')); | ||
} else { | ||
return cb(null, b); | ||
} | ||
}); | ||
}; | ||
Org.prototype.repos = function(cbOrRepo, cb) { | ||
@@ -27,0 +40,0 @@ if (typeof cb === 'function' && typeof cbOrRepo === 'object') { |
@@ -1,4 +0,5 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
var Repo; | ||
var Repo, | ||
__slice = [].slice; | ||
@@ -38,2 +39,15 @@ Repo = (function() { | ||
Repo.prototype.commit = function(sha, cb) { | ||
return this.client.get("/repos/" + this.name + "/commits/" + sha, function(err, s, b) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 200) { | ||
return cb(new Error("Repo commits error")); | ||
} else { | ||
return cb(null, b); | ||
} | ||
}); | ||
}; | ||
Repo.prototype.tags = function(cb) { | ||
@@ -198,3 +212,3 @@ return this.client.get("/repos/" + this.name + "/tags", function(err, s, b) { | ||
var _this = this; | ||
return this.client.del("/repos/" + this.name, function(err, s, b) { | ||
return this.client.del("/repos/" + this.name, {}, function(err, s, b) { | ||
if ((err != null) || s !== 204) { | ||
@@ -206,2 +220,54 @@ return _this.destroy(); | ||
Repo.prototype.pr = function(number) { | ||
return this.client.pr(this.name, number); | ||
}; | ||
Repo.prototype.prs = function(cbOrPr, cb) { | ||
if (typeof cb === 'function' && typeof cbOrPr === 'object') { | ||
return this.createPr(cbOrPr, cb); | ||
} else { | ||
cb = cbOrPr; | ||
return this.client.get("/repos/" + this.name + "/pulls", function(err, s, b) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 200) { | ||
return cb(new Error("Repo prs error")); | ||
} else { | ||
return cb(null, b); | ||
} | ||
}); | ||
} | ||
}; | ||
Repo.prototype.createPr = function(pr, cb) { | ||
return this.client.post("/repos/" + this.name + "/pulls", pr, function(err, s, b) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 201) { | ||
return cb(new Error("Repo createPr error")); | ||
} else { | ||
return cb(null, b); | ||
} | ||
}); | ||
}; | ||
Repo.prototype.stargazers = function() { | ||
var cb, page, params, per_page, _i; | ||
params = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), cb = arguments[_i++]; | ||
page = params[0] || 1; | ||
per_page = params[1] || 30; | ||
return this.client.get("/repos/" + this.name + "/stargazers", page, per_page, function(err, s, b, headers) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 200) { | ||
return cb(new Error("Repo stargazers error")); | ||
} else { | ||
return cb(null, b, headers); | ||
} | ||
}); | ||
}; | ||
return Repo; | ||
@@ -208,0 +274,0 @@ |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var Team; |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
// Generated by CoffeeScript 1.4.0 | ||
(function() { | ||
@@ -58,2 +58,22 @@ var User; | ||
User.prototype.events = function(events, cb) { | ||
if (!cb && typeof events === 'function') { | ||
cb = events; | ||
events = null; | ||
} else if (!Array.isArray(events)) { | ||
events = [events]; | ||
} | ||
return this.client.get("/users/" + this.login + "/events", function(err, s, b) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (s !== 200) { | ||
return cb(new Error('User events error')); | ||
} | ||
return cb(null, b.filter(function(event) { | ||
return events.indexOf(event.type) !== -1; | ||
})); | ||
}); | ||
}; | ||
return User; | ||
@@ -60,0 +80,0 @@ |
{ | ||
"name": "octonode", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"author": "Pavan Kumar Sunkara <pavan.sss1991@gmail.com> (http://pksunkara.github.com)", | ||
@@ -41,3 +41,3 @@ "description": "nodejs wrapper for github v3 api", | ||
"vows": "0.6.x", | ||
"coffee-script": "1.2.x" | ||
"coffee-script": "1.4.0" | ||
}, | ||
@@ -44,0 +44,0 @@ "engines": { |
128
README.md
@@ -23,4 +23,52 @@ # octonode | ||
var ghteam = client.team(37); | ||
var ghsearch = client.search(); | ||
``` | ||
### Build a client which accesses any public information | ||
```js | ||
var client = github.client(); | ||
client.get('/users/pksunkara', function (err, status, body) { | ||
console.log(body); //json object | ||
}); | ||
``` | ||
### Build a client from an access token | ||
```js | ||
var client = github.client('someaccesstoken'); | ||
client.get('/user', function (err, status, body) { | ||
console.log(body); //json object | ||
}); | ||
``` | ||
### Build a client from credentials | ||
```js | ||
var client = github.client({ | ||
username: 'pksunkara', | ||
password: 'password' | ||
}); | ||
client.get('/user', function (err, status, body) { | ||
console.log(body); //json object | ||
}); | ||
``` | ||
### Build a client from client keys | ||
```js | ||
var client = github.client({ | ||
id: 'abcdefghijklmno', | ||
secret: 'abcdefghijk' | ||
}); | ||
client.get('/user', function (err, status, body) { | ||
console.log(body); //json object | ||
}); | ||
``` | ||
__Many of the below use cases use parts of the above code__ | ||
@@ -91,35 +139,13 @@ | ||
### Build a client which accesses any public information | ||
## Rate Limiting | ||
```js | ||
var client = github.client(); | ||
You can also check your rate limit status by calling the following. | ||
client.get('/users/pksunkara', function (err, status, body) { | ||
console.log(body); //json object | ||
}); | ||
``` | ||
### Build a client from an access token | ||
```js | ||
var client = github.client('someaccesstoken'); | ||
client.get('/user', function (err, status, body) { | ||
console.log(body); //json object | ||
client.limit(function (err, left, max) { | ||
console.log(left); // 4999 | ||
console.log(max); // 5000 | ||
}); | ||
``` | ||
### Build a client from credentials | ||
```js | ||
var client = github.client({ | ||
username: 'pksunkara', | ||
password: 'password' | ||
}); | ||
client.get('/user', function (err, status, body) { | ||
console.log(body); //json object | ||
}); | ||
``` | ||
## API Callback Structure | ||
@@ -355,2 +381,10 @@ | ||
### Get users who starred a repository (GET /repos/pksunkara/hub/stargazers) | ||
``` | ||
ghrepo.stargazers(1, 100, callback); //array of users | ||
ghrepo.stargazers(10, callback); //array of users | ||
ghrepo.stargazers(callback); //array of users | ||
``` | ||
### Get the teams for a repository (GET /repos/pksunkara/hub/teams) | ||
@@ -376,2 +410,10 @@ | ||
### Update an organization (POST /orgs/flatiron) | ||
```js | ||
ghorg.update({ | ||
blog: 'https://blog.com' | ||
}, callback); // org | ||
``` | ||
### List organization repositories (GET /orgs/flatiron/repos) | ||
@@ -465,2 +507,8 @@ | ||
### Fork a gist (POST /gists/37/forks) | ||
```js | ||
ghgist.fork(37, callback); //gist | ||
``` | ||
### Star a gist (PUT /gists/37/star) | ||
@@ -538,2 +586,28 @@ | ||
## Github search api | ||
### Search issues | ||
```js | ||
ghsearch.issues('pksunkara/hub', 'open', 'git', callback); //array of issues | ||
``` | ||
### Search repositories | ||
```js | ||
ghsearch.repos('git', 'javascript', callback); //array of repositories | ||
``` | ||
### Search users | ||
```js | ||
ghsearch.users('git', callback); //array of users | ||
``` | ||
### Search emails | ||
```js | ||
ghsearch.emails('pavan.sss1991@gmail.com', callback); //user | ||
``` | ||
## Testing | ||
@@ -540,0 +614,0 @@ ``` |
61591
16
1413
750