/
gisty.coffee
59 lines (44 loc) · 1.67 KB
/
gisty.coffee
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
###
node-gisty 0.0.1
(c) 2011 Alexey Simonenko, Serenity LLC.
For all details and documentation:
http://github.com/meritt/node-gisty
###
xhr = require 'request'
class Gisty
api = 'https://api.github.com'
constructor: (options = {}) ->
@setOptions options
setOptions: (@options) ->
@options.username = @options.login if @options.login? and not @options.username?
@
fetch: (id, fn) ->
throw new Error 'You should specify gist id for fetch information' unless id?
@scheme = '/gists/' + id
request @, fn
comments: (id, fn) ->
throw new Error 'You should specify gist id for fetch comments' unless id?
@scheme = '/gists/' + id + '/comments'
request @, fn
all: (fn) ->
@scheme = if @options.username? then '/users/' + @options.username + '/gists' else '/gists'
request @, fn
public: (fn) ->
@scheme = '/gists/public'
request @, fn
starred: (fn) ->
@scheme = '/gists/starred'
request @, fn
request = (self, fn = ->) ->
options = url: api + self.scheme
if self.options.token?
options['headers'] = Authorization: "token #{self.options.token}"
else if self.options.login? and self.options.password?
options['headers'] = Authorization: "Basic " + new Buffer(self.options.login + ':' + self.options.password).toString('base64')
xhr options, (error, request, body) ->
body = JSON.parse body
error = body if request.statusCode not in [200, 201, 204]
limit = if request.headers? then parseInt(request.headers['x-ratelimit-remaining']) > 0 else true
error = {message: 'Your limit is over'} if not error and not limit
fn.call self, error, body
exports.Gisty = Gisty