This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
407 lines (374 loc) · 10.6 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
var find = require('lodash.find');
var isFunction = require('lodash.isfunction');
var isNull = require('lodash.isnull');
var isString = require('lodash.isstring');
var isUndefined = require('lodash.isundefined');
var request = require('request');
var ENDPOINT = 'http://api.captainteemo.com';
var PLATFORMS = [
{'short': 'na', 'full': 'North_America'},
{'short': 'br', 'full': 'Brasil'},
{'short': 'ru', 'full': 'Russia'},
{'short': 'euw', 'full': 'Europe_West'},
{'short': 'eun', 'full': 'Europe_East'},
{'short': 'tr', 'full': 'Turkey'},
{'short': 'las', 'full': 'Latin_America_South'},
{'short': 'lan', 'full': 'Latin_America_North'}
];
/**
* Create an error whose message begins with 'node-resteemo - '.
*
* @private
* @param {String} value The error message.
* @return {Error}
*/
function brandError(value) {
return new Error('node-resteemo - ' + value);
}
/**
* Converts a full platform string to its shorthand equivalent.
*
* @private
* @param {String} platform The platform to shorten.
* @return {String} The shorthand equivalent of `platform` if one exists, else
* `null`.
*/
function normalizePlatform(platform) {
var matchFromShortPlatform = find(PLATFORMS, {'short': platform});
if (!isUndefined(matchFromShortPlatform)) {
return platform;
}
var matchFromFullPlatform = find(PLATFORMS, {'full': platform});
if (!isUndefined(matchFromFullPlatform)) {
return matchFromFullPlatform['short'];
}
return null;
}
/**
* Parse JSON data from a request.
*
* @private
* @param {Object} req The request instance to handle.
* @param {Function} callback The callback, which is given two arguments:
* `(err, response)`, where `response` is a JSON Object from `req`.
*/
function responseHandler(req, callback) {
req.on('response', function(res) {
var response = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
try {
response = JSON.parse(response);
}
catch(e) {
var error = brandError('invalid json response');
return callback(error);
}
if (!response.success) {
var error = brandError('api failed');
return callback(error);
}
if (!isUndefined(response.data._success)) {
if (!response.data._success) {
var error = brandError('api failed at second success check');
return callback(error);
}
}
callback(null, response);
});
});
}
/**
* Establishes the user agent of all API calls.
*
* @param {String} refererString The referer string to use in API calls.
* @return {Object}
*/
module.exports = function(refererString) {
if (!isString(refererString)) {
var error = brandError('`refererString` not defined');
throw error;
}
/**
* Contructs the headers and options for the API request.
*
* @private
* @param {String} method The HTTP verb.
* @param {String} path The path to query from the API endpoint.
* @param {Function} callback The callback, which is passed to
* `responseHandler`.
*/
function prepareRequest(method, path, callback) {
if (!isFunction(callback)) {
var error = brandError('missing callback');
throw error;
}
var headers = {
'Accept' : 'application/json',
'User-Agent': refererString
};
var requestOptions = {
method : method,
uri : ENDPOINT + path,
headers: headers
};
var req = request(requestOptions);
responseHandler(req, callback);
req.end();
}
/**
* Begins the preparation for a GET request.
*
* @private
* @param {String} path The path to query from the API endpoint.
* @param {Function} callback The callback, which is passed to
* `prepareRequest`.
*/
function get(path, callback) {
prepareRequest('GET', path, callback);
}
/**
* Contructs the path to query.
*
* @private
* @param {Object} options
* @param {Function} callback
*/
function constructPath(options, callback) {
var shortPlatform = normalizePlatform(options.platform);
if (isNull(shortPlatform)) {
var error = brandError('invalid platform');
return callback(error);
}
if (isUndefined(options.path)) {
options.path = '';
} else {
options.path = '/' + options.path;
}
if (options.summoner) {
get('/player/' + shortPlatform + '/' + options.summoner + options.path + (options.season || ''), callback);
}
else if (options.tag || options.guid) {
get('/team/' + shortPlatform + options.path + '/' + (options.tag || options.guid) + (options.guid ? '/leagues' : ''), callback);
}
else {
get('/service-state/' + shortPlatform + options.path, callback);
}
}
var teemo = {};
/**
* Returns primarily ID-based data for String `summoner` on String `platform`.
* Account and summoner IDs are not unique across multiple platforms.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, profile)` where
* `profile` is the API response as an Object.
*/
teemo.player = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner
}, callback);
};
/**
* Returns observer metadata and information if String `summoner` on String
* `platform` is playing a valid game.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, profile)` where
* `game` is the API response as an Object.
*/
teemo.player.ingame = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'ingame'
}, callback);
};
/**
* Returns last 10 matches (order is random) for String `summoner` on String
* `platform`.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, games)` where `games`
* is the API response as an Object.
*/
teemo.player.recentGames = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'recent_games'
}, callback);
};
/**
* Returns lifetime influence point gains for String `summoner` on String
* `platform`.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, points)` where `points`
* is the API response as an Object.
*/
teemo.player.influencePoints = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'influence_points'
}, callback);
};
/**
* Returns runepages for String `summoner` on String `platform`.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, runes)` where `runes`
* is the API response as an Object.
*/
teemo.player.runes = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'runes'
}, callback);
};
/**
* Returns mastery pages for String `summoner` on String `platform`.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, pages)` where `pages`
* is the API response as an Object.
*/
teemo.player.mastery = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'mastery'
}, callback);
};
/**
* Returns Season 3 Leagues info.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, leagues)` where
* `leagues` is the API response as an Object.
*/
teemo.player.leagues = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'leagues'
}, callback);
};
/**
* Returns honor.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, honor)` where `honor`
* is the API response as an Object.
*/
teemo.player.honor = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'honor'
}, callback);
};
/**
* Returns ranked stats for String `summoner` in Number `season`.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Number} season
* @param {Function} callback Used as `callback(error, stats)` where `stats`
* is the API response as an Object.
*/
teemo.player.rankedStats = function(platform, summoner, season, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'ranked_stats/season/',
season: season
}, callback);
};
/**
* Returns all teams (and team match history) that `summoner` is a member of.
*
* @public
* @param {String} platform
* @param {String} summoner
* @param {Function} callback Used as `callback(error, stats)` where `teams`
* is the API response as an Object.
*/
teemo.player.teams = function(platform, summoner, callback) {
constructPath({
platform: platform,
summoner: summoner,
path: 'teams'
}, callback);
};
/**
* Returns team information and matches.
*
* @public
* @param {String} platform
* @param {String} tag
* @param {Function} callback Used as `callback(error, response)` where
* `response` is the API response as an Object.
*/
teemo.team = function(platform, tag, callback) {
constructPath({
platform: platform,
tag: tag,
path: 'tag'
}, callback);
};
/**
* Returns leagues for a given team GUID.
*
* @public
* @param {String} platform
* @param {String} guid
* @param {Function} callback Used as `callback(error, response)` where
* `response` is the API response as an Object.
*/
teemo.team.leagues = function(platform, guid, callback) {
constructPath({
platform: platform,
guid: guid,
path: 'guid'
}, callback);
};
/**
* Returns free week.
*
* @public
* @param {String} platform
* @param {Function} callback Used as `callback(error, response)` where
* `response` is the API response as an Object.
*/
teemo.freeWeek = function(platform, callback) {
constructPath({
platform: platform,
path: 'free-week'
}, callback);
};
return teemo;
};