/
me.coffee
224 lines (193 loc) · 6.95 KB
/
me.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#
# me.coffee: Github authenticated user class
#
# Copyright © 2011 Pavan Kumar Sunkara. All rights reserved
#
# Initiate class
class Me
constructor: (@client) ->
profile: (data) ->
Object.keys(data).forEach (e) =>
@[e] = data[e]
# Get a user
# '/user' GET
info: (cb) ->
@client.get '/user', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User info error')) else cb null, b
# Update user
# '/user' PATCH
update: (info, cb) ->
@client.post '/user', info, (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User update error')) else cb null, b
# Get emails of the user
# '/user/emails' GET
emails: (cbOrEmails, cb) ->
if cb? and typeof cbOrEmails isnt 'function'
@setEmails cbOrEmails, cb
else if !cb? and typeof cbOrEmails isnt 'function'
@deleteEmails cbOrEmails
else
cb = cbOrEmails
@client.get '/user/emails', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User emails error')) else cb null, b
# Set emails of the user
# '/user/emails' POST
setEmails: (emails, cb) ->
@client.post '/user/emails', emails, (err, s, b) ->
return cb(err) if err
if s isnt 201 then cb(new Error('User setEmails error')) else cb null, b
# Delete emails of the user
# '/user/emails' DELETE
deleteEmails: (emails) ->
@client.del '/user/emails', emails, (err, s, b) =>
@deleteEmails(emails) if err? or s isnt 204
# Get the followers of the user
# '/user/followers' GET
# TODO: page, user
followers: (cb) ->
@client.get '/user/followers', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User followers error')) else cb null, b
# Get the followings of the user
# '/user/following' GET
# TODO: page, user
following: (cbOrUser, cb) ->
if cb? and typeof cbOrUser isnt 'function'
@checkFollowing cbOrUser, cb
else
cb = cbOrUser
@client.get '/user/following', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User following error')) else cb null, b
# Check if you are following a user
# '/user/following/pksunkara' GET
checkFollowing: (user, cb) ->
@client.get "/user/following/#{user}", (err, s, b) ->
return cb(err) if err
cb null, s is 204
# Follow a user
# '/user/following/pksunkara' PUT
follow: (user) ->
@client.put "/user/following/#{user}", {}, (err, s, b) =>
@follow(user) if err? or s isnt 204
# Unfollow a user
# '/user/following/pksunkara' DELETE
unfollow: (user) ->
@client.del "/user/following/#{user}", {}, (err, s, b) =>
@unfollow(user) if err? or s isnt 204
# Get the starred repos for the user
# '/user/starred' GET
# TODO: page, user
starred: (cbOrUser, cb) ->
if cb? and typeof cbOrUser isnt 'function'
@checkStarred cbOrUser, cb
else
cb = cbOrUser
@client.get '/user/starred', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User starred error')) else cb null, b
# Check if you have starred a repository
# '/user/starred/pksunkara/octonode' GET
checkStarred: (repo, cb) ->
@client.get "/user/starred/#{repo}", (err, s, b) ->
return cb(err) if err
cb null, s is 204
# Star a repository
# '/user/starred/pksunkara/octonode' PUT
star: (repo) ->
@client.put "/user/starred/#{repo}", {}, (err, s, b) =>
@star(repo) if err? or s isnt 204
# Unstar a repository
# '/user/starred/pksunkara/octonode' DELETE
unstar: (repo) ->
@client.del "/user/starred/#{repo}", {}, (err, s, b) =>
@unstar(repo) if err? or s isnt 204
# Get the subscriptions of the user (what she watches)
# '/user/subscriptions' GET
# TODO: page, user
watched: (cb) ->
@client.get '/user/subscriptions', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User subscription error')) else cb null, b
# Get public keys of a user
# '/user/keys' GET
keys: (cbOrIdOrKey, cbOrKey, cb) ->
if !cb? and typeof cbOrIdOrKey is 'number' and typeof cbOrKey is 'function'
@getKey cbOrIdOrKey, cbOrKey
else if !cbOrKey? and !cb? and typeof cbOrIdOrKey is 'number'
@deleteKey cbOrIdOrKey
else if !cb? and typeof cbOrKey is 'function' and typeof cbOrIdOrKey is 'object'
@createKey cbOrIdOrKey, cbOrKey
else if typeof cb is 'function' and typeof cbOrIdOrKey is 'number' and typeof cbOrKey 'object'
@updateKey cbOrIdOrKey, cbOrKey, cb
else
cb = cbOrIdOrKey
@client.get '/user/keys', (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User keys error')) else cb null, b
# Get a single public key
# '/user/keys/1' GET
getKey: (id, cb) ->
@client.get "/user/keys/#{id}", (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User getKey error')) else cb null, b
# Create a public key
# '/user/keys' POST
createKey: (key, cb) ->
@client.post '/user/keys', key, (err, s, b) ->
return cb(err) if err
if s isnt 201 then cb(new Error('User createKey error')) else cb null, b
# Update a public key
# '/user/keys/1' PATCH
updateKey: (id, key, cb) ->
@client.post "/user/keys/#{id}", key, (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User updateKey error')) else cb null, b
# Delete a public key
# '/user/keys/1' DELETE
deleteKey: (id) ->
@client.del "/user/keys/#{id}", {}, (err, s, b) =>
@deleteKey(id) if err? or s isnt 204
# Get organization instance for client
org: (name) ->
@client.org name
# List your public and private organizations
# '/user/orgs' GET
orgs: (cb) ->
@client.get "/user/orgs", (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User orgs error')) else cb null, b
# Get repository instance for client
repo: (name, cb) ->
if typeof cb is 'function' and typeof nameOrRepo is 'object'
@createRepo nameOrRepo, cb
else
@client.repo name
# List your repositories
# '/user/repos' GET
# - page or query object, optional - params[0]
# - per_page, optional - params[1]
repos: (params..., cb) ->
@client.get "/user/repos", params..., (err, s, b) ->
return cb(err) if err
if s isnt 200 then cb(new Error('User repos error')) else cb null, b
# Create a repository
# '/user/repos' POST
createRepo: (repo, cb) ->
@client.post "/user/repos", repo, (err, s, b) ->
return cb(err) if err
if s isnt 201 then cb(new Error('User createRepo error')) else cb null, b
# Fork a repo
# '/repos/pksunkara/hub/forks' POST
fork: (repo, cb) ->
@client.post "/repos/#{repo}/forks", {}, (err, s, b) ->
return cb(err) if err
if s isnt 202 then cb(new Error('User fork error')) else cb null, b
# Get pull-request instance for client
pr: (repo, number) ->
@client.pr repo, number
# Export module
module.exports = Me