forked from coolaj86/node-gist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (36 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var request = require('request').defaults({json: true})
var qs = require('querystring')
var _ = require('underscore')
function Gist(token) {
this.api = "https://api.github.com/"
this.token = token
}
Gist.prototype.create = function (content, cb) {
if (!content) return cb("must specify id")
var url = this.api + 'gists'
return this._request({url: url, json: content, method: "POST"}, cb)
};
Gist.prototype.gists = function (user, cb) {
if (typeof user === 'function') {
cb = user
user = false
}
var url = this.api + (user ? 'users/' + user + '/gists' : 'gists')
return this._request({url: url}, cb)
}
Gist.prototype.gist = function (id, cb) {
if (!id) return cb("must specify id")
var url = this.api + 'gists/' + id
return this._request({url: url}, cb)
}
Gist.prototype._request = function(options, cb) {
if (this.token) {
if (!options.headers) options.headers = {}
options.headers["Authorization"] = "token " + this.token
}
if (!cb) cb = function() {}
return request(options, cb)
}
module.exports = function(options) {
return new Gist(options)
}