-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
332 lines (230 loc) · 7.17 KB
/
index.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
XMLHttpRequest = require('xhr2')
class HackerAPI
constructor: (token) ->
@token = token
@userId = null
@apiServer = "https://hackerapi.com/v1"
@sync = false
########## Auth Endpoint ##########
getToken: (username, password, callback) ->
req = {}
req.method = 'POST'
req.endpoint ='/auth/user'
req.callback = callback
req.payload = {
username: username,
password: password
}
return @makeRequest req
setToken: (token) ->
@token = token
########## Users Endpoint ##########
getCurrentUserInfo: (callback) ->
req = {}
req.endpoint = "/users/me"
req.callback = callback
return @makeRequest req
getUserInfo: (id, callback) ->
req = {}
req.endpoint = "/users/#{id}"
req.callback = callback
return @makeRequest req
getUserFiles: (id, callback) ->
req = {}
req.endpoint = "/users/#{id}/files"
req.callback = callback
return @makeRequest req
getUserResume: (id, callback) ->
req = {}
req.endpoint = "/users/#{id}/resume"
req.callback = callback
return @makeRequest req
updateUser: (id, payload, callback) ->
req = {}
req.method = 'PUT'
req.payload = payload
req.endpoint = "/users/#{id}"
req.callback = callback
return @makeRequest req
########## Teams Endpoint ##########
getTeamInfo: (id, callback) ->
req = {}
req.endpoint = "/teams/#{id}"
req.callback = callback
return @makeRequest req
joinTeam: (team_id, callback) ->
req = {}
req.method = "PUT"
req.endpoint = "/teams/#{team_id}"
req.callback = callback
return @makeRequest req
createTeam: (event_slug, callback) ->
req = {}
req.method = "POST"
req.endpoint = "/events/#{event_slug}/teams"
req.callback = callback
return @makeRequest req
leaveTeam: (id, callback) ->
req = {}
req.method = "DELETE"
req.endpoint = "/teams/#{id}"
req.callback = callback
return @makeRequest req
########## Pipeline Endpoint ##########
getPipelineInfo: (id, callback) ->
req = {}
req.endpoint = "/pipelines/#{id}"
req.callback = callback
return @makeRequest req
getPipelineClaims: (id, callback) ->
req = {}
req.endpoint = "/pipelines/#{id}/claims"
req.callback = callback
return @makeRequest req
getPipelineFields: (id, callback) ->
req = {}
req.endpoint = "/pipelines/#{id}/fields"
req.callback = callback
return @makeRequest req
getPipelineFieldById: (id, field_id, callback) ->
req = {}
req.endpoint = "/pipelines/#{id}/fields/#{field_id}"
req.callback = callback
return @makeRequest req
########## Claims Endpoint ##########
getClaimInfo: (id, callback) ->
req = {}
req.endpoint = "/claims/#{id}"
req.callback = callback
return @makeRequest req
########## Files Endpoint ##########
getFileInfo: (id, callback) ->
req = {}
req.endpoint = "/files/#{id}"
req.callback = callback
return @makeRequest req
getFileDownload: (id, callback) ->
req = {}
req.endpoint = "/files/#{id}/download"
req.callback = callback
return @makeRequest req
########## Institutions Endpoint ##########
getInstitutionInfo: (id, callback) ->
req = {}
req.endpoint = "/institutions/#{id}"
req.callback = callback
return @makeRequest req
createInstitution: (payload, callback) ->
if not payload.name?
throw "Institution name missing"
if not payload.institution_type?
throw "Institution type missing"
if not payload.country_code?
throw "Missing country code"
types = ['post-secondary', "high_school", "middle_school", "other"]
if payload.institution_type not in types
throw "Invalid institution type"
if payload.country_code.length != 2
throw "Invalid country code"
req = {}
req.method = 'POST'
req.payload = {
name : payload.name,
institution_type : payload.institution_type,
country_code : payload.country_code
}
req.endpoint = "/institutions"
req.callback = callback
return @makeRequest req
########## Events Endpoint ##########
getEventInfo: (slug, callback) ->
req = {}
req.endpoint = "/events/#{slug}"
req.callback = callback
return @makeRequest req
getEventSignups: (slug, callback) ->
req = {}
req.endpoint = "/events/#{slug}/signups"
req.callback = callback
return @makeRequest req
uploadFile: (evet_slug, payload, callback) ->
if not event_slug?
throw "Event slug missing"
if not payload.file?
throw "File missing"
if not payload.type?
throw "File type missing."
types = ['resume', "receipt", "form", "other"]
if payload.type not in types
throw "Invalid file type"
req = {}
req.method = 'POST'
req.payload = {
file : payload.file,
type : payload.type
}
req.endpoint = "/events/#{slug}/upload"
req.callback = callback
return @makeRequest req
########## Search Endpoint ##########
searchInstitutions: (query, callback) ->
req = {}
req.params = {
q: query
}
req.callback = callback
req.endpoint = "/search/institutions"
return @makeRequest req
makeRequest: ({endpoint, method, params, payload, callback} = {}) ->
method ?= 'GET'
params ?= {}
payload ?= null
self = @
if @token
params.token = @token
params = @serialize(params)
url = "#{@apiServer}#{endpoint}?#{params}"
xhr = new XMLHttpRequest
xhr.open(method, url, !@sync)
if method == 'POST' or method == 'PUT'
if payload
xhr.setRequestHeader("Content-type", "application/json")
xhr.send(JSON.stringify(payload))
else
xhr.send()
else
xhr.send()
if @sync
return @parseReponse(xhr.responseText)
xhr.onreadystatechange = () ->
if xhr.readyState == 4
json = self.parseReponse(xhr.responseText)
callback(json)
parseReponse: (data) ->
try
json = JSON.parse(data)
catch
json = {"success" : false, "message" : "Could not parse response JSON"}
return json
serialize: (obj) ->
str = []
for p of obj
param = encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])
str.push param
return str.join("&")
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MzM5MTUyOTgsImlkIjoyLCJldnQiOlsxXSwidHlwIjoidXNyIn0.USfHFAJ_AYw4hP-wAjiVSWiXbwxPwWLjzzC5oXhVCws"
api = new HackerAPI token
callback = console.log
# api.searchInstitutions("water", console.log)
# api.getCurrentUserInfo(callback)
# api.getUserInfo(1, callback)
# api.createInsitution({name:"Emery Collegiate Institute", institution_type:"high_school", country_code:"CA"}, callback)
# api.getInstitutionInfo(22593, callback)
# user_payload = {"address": {}}
# api.updateUser(2, user_payload, callback)
# api.createTeam('hackthenorth', callback)
# api.getTeamInfo('3468f7cba4', callback)
# api.joinTeam('3468f7cba4', callback)
# console.log api.leaveTeam('8ed19e0022', callback)
# TODOS
# - handle errors