-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdk.js
664 lines (540 loc) · 15.8 KB
/
sdk.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/// NB: This original SDK has been superceded by the file pearson_sdk.js
// Pearson Top Ten Travel API wrapper. V0.9
// Base Url for API call http://api.pearson.com/v2/
/// BASE OBJECT
function Pearson(apiKey) {
'use strict';
if (typeof apiKey === 'undefined') {
this.base = 'http://api.pearson.com/v2/'
this.apiKey = ""
} else {
this.base = 'http://api.pearson.com/v2/';
this.apiKey = "&apikey=" + apiKey;
return this;
}
};
/////// BASE OBJECT END
// Simple 'call the api from the returned url I provide method'
Pearson.prototype.collect = function(url) {
'use strict';
var fullUrl;
var key;
var base = "http://api.pearson.com";
key = this.apiKey;
if (key !== "" || "undefined") {
fullUrl = base + url + "?apikey=" + key;
console.log(fullUrl);
console.log("defined");
} else {
fullUrl = base + url
console.log(fullUrl);
console.log("undefined");
};
};
///// Dictionaries and methods
Pearson.prototype.dictionaries = function() {
'use strict';
this.api = "dictionaries";
this.url = this.base + this.api + "/";
this.entries = function() {
'use strict';
this.endpoint = "entries";
return this;
};
this.addDatasets = function() {
// If whitespace is a problem - add .replace(/\s+/g,"") to args to remove. Hopefully.
// This resets the url to nil before repopulating.
this.url = this.base + this.api + "/";
this.datasets = "";
var datasets;
var args = Array.prototype.slice.call(arguments);
this.datasets = args.join(",");
console.log(this.datasets);
this.url = this.url + this.datasets + '/';
return this;
};
this.addSearch = function (searchTerm ) {
var searchString = '';
var terms = "&search=" + encodeURIComponent(searchTerm);
searchString = searchString + terms;
this.searchTerm = searchString.substr(1);
return this;
};
this.headword = function (searchTerm) {
var searchString = '';
var terms = "&headword=" + encodeURIComponent(searchTerm);
searchString = searchString + terms;
this.headword = searchString.substr(1);
return this;
};
this.audio = function (audio) {
'use strict'
this.audio = audio;
return this;
};
this.images = function (images) {
'use strict'
this.images = images;
return this;
};
return this;
};
// dictionary allow empty attribute WITHOUT = method. ////WIP
function toQueryString(bits) {
var obj = {audio: bits.audio, images: bits.images }
var parts = [];
for (var i in obj) {
if (obj.hasOwnProperty(i) && (typeof obj[i] !== "undefined" )) {
console.log(encodeURIComponent(i));
console.log(encodeURIComponent(obj[i]));
parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
}
else
parts.push(encodeURIComponent(i))
}
return parts.join("&");
};
///// Travel ////// listDatasets id attr needs fixing and full url test needed.
Pearson.prototype.travel = function() {
'use strict';
this.api = "travel";
this.url = this.base + this.api + "/"
this.addDatasets = function() {
// If whitespace is a problem - add .replace(/\s+/g,"") to args to remove. Hopefully.
// This resets the url to nil before repopulating.
this.url = this.base + this.api + "/";
this.datasets = "";
var datasets;
var args = Array.prototype.slice.call(arguments);
this.datasets = args.join(",");
console.log(this.datasets);
this.url = this.url + this.datasets + '/';
return this;
};
this.topten = function() {
'use strict';
this.endpoint = "topten";
return this;
};
this.streetsmart = function() {
'use strict';
this.endpoint = "streetsmart";
return this;
};
this.aroundTown = function() {
'use strict';
this.endpoint = "around_town";
return this;
};
this.places = function() {
'use strict';
this.endpoint = "places";
};
this.addSearch = function (searchTerm ) {
var searchString = '';
var terms = "&search=" + encodeURIComponent(searchTerm);
searchString = searchString + terms;
this.searchTerm = searchString.substr(1);
return this;
};
// Add lat and long to query string
this.addPosition = function (lat, lon, dist) {
'use strict';
var pos;
if (typeof lat === 'undefined') {
console.log('latitude is not defined');
}
if (typeof lon === 'undefined') {
console.log('longitude is not defined');
}
if (typeof dist === 'undefined') {
console.log('Please enter a value for the search radius');
}
var pos = "&lat=" + lat + "&lon=" + lon + "&dist=" + dist;
this.position = pos;
return this;
};
return this;
};
//// Food and Drink
Pearson.prototype.foodanddrink = function() {
'use strict';
this.api = "foodanddrink";
this.endpoint = "recipes";
this.url = this.base + this.api + "/"
return this;
};
//List all datasets for an api
function grab(url) {
$.ajax({
url: url,
type: 'GET',
timeout: 1000, // feel free to mod this
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
async: false,
success: function (data) {
result = data;
},
error: function (x, t, m) {
if (t === 'timeout') {
console.warn("Hmm, request timed out");
} else {
console.warn(t);
}
}
});
return result;
}
Pearson.prototype.listDatasets = function () {
'use strict';
this.endpoint = 'datasets';
this.addParams({
limit: 25,
offset:0
});
var call = this.buildUrl();
var datasetsList = grab(call);
var results = datasetsList.results;
var len = results.length;
var out = [];
for (var i = 0; i < len; i++) {
out.push({
description: results[i].description,
id: results[i].name
});
}
this.addParams({
limit: 25,
offset:25
});
grab(call);
var results = datasetsList.results;
for (var i = 0; i < len; i++) {
out.push({
description: results[i].description,
id: results[i].name
});
}
this.addParams({
limit: 25,
offset:50
});
this.endpoint = 'datasets';
var call = this.buildUrl();
var datasetsList = grab(call);
var results = datasetsList.results;
var len = results.length;
for (var i = 0; i < len; i++) {
out.push({
description: results[i].description,
id: results[i].name
});
}
this.addParams({
limit: 25,
offset:75
});
this.endpoint = 'datasets';
var call = this.buildUrl();
var datasetsList = grab(call);
var results = datasetsList.results;
var len = results.length;
for (var i = 0; i < len; i++) {
out.push({
description: results[i].description,
id: results[i].name
});
}
return out;
};
// Get a document by UID, if you know it! ///RETURN Article - object independant? //Test with food.
Pearson.prototype.getById = function (id) {
'use strict';
var results;
if (typeof this.endpoint == 'undefined') {
this.endpoint = 'topten';
var z = this.url + this.endpoint + '/' + id + "?" + this.apiKey.substr(1);
results = grab(z);
return results;
} else {
var z = this.url + this.endpoint + '/' + id + "?" + this.apiKey.substr(1);
results = grab(z);
return results;
}
};
// Add url params that limit results. /// To be homologised.
Pearson.prototype.addParams = function (params) {
'use strict';
var url = '';
var params = params || {
offset: 0
};
for (var key in params) {
if (params.hasOwnProperty(key)) {
var value = params[key];
url += '&' + key + '=' + value;
}
this.params = url.substr(1);
}
return this;
};
Pearson.prototype.offset = function() {
//Hmm split out?
};
// Construct Url for the API call
Pearson.prototype.buildUrl = function () {
'use strict';
var url = this.url + this.endpoint + '?';
if (typeof this.params !== 'undefined') {
url = url + this.params;
} else {
url
};
if (typeof this.searchTerm !== 'undefined' && typeof this.params !== 'undefined') {
url = url + "&" + this.searchTerm;
} else {
url
};
if (typeof this.searchTerm !== 'undefined' && typeof this.params == 'undefined') {
url = url + this.searchTerm;
} else {
url
};
//dictionary mods // not finished
if (typeof this.headword !== 'undefined') {
url = url + this.headword
} else {
url
};//
if (typeof this.position !== 'undefined') {
url = url + this.position;
} else {
url
};
url = url + this.apiKey;
if (url.indexOf("undefined") != -1 || url.indexOf("NaN") != -1) {
throw {
name: 'Api or endpoint undefined',
message: 'Please select an API and an endpoint to work with'
};
} else {
return url;
}
};
function buildIt(objec) {
'use strict';
var url = objec.url + objec.endpoint + '?';
if (typeof objec.params !== 'undefined') {
url = url + objec.params;
} else {
url
};
if (typeof objec.searchTerm !== 'undefined' && typeof objec.params !== 'undefined') {
url = url + "&" + objec.searchTerm;
} else {
url
};
if (typeof objec.searchTerm !== 'undefined' && typeof objec.params == 'undefined') {
url = url + objec.searchTerm;
} else {
url
};
if (typeof objec.position !== 'undefined') {
url = url + objec.position;
} else {
url
};
url = url + objec.apiKey;
//Redacted from version where apikey was mandatory
// if (url.indexOf("undefined") != -1 || url.indexOf("NaN") != -1) {
// throw {
// name: 'Api or endpoint undefined',
// message: 'Please select an API and an endpoint to work with'
// }
// } else {
return url;
//}
};
// Debug: async callback - log data and result - look for timing errors.
Pearson.prototype.fetch = function() {
'use strict';
var ApiObject = this;
var result = null;
var url = buildIt(ApiObject);
$.ajax({
url: url,
type: 'GET',
timeout: 1000, // feel free to mod this
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
async: false,
success: function (data) {
console.log(data);
result = data;
},
error: function (x, t, m) {
if (t === 'timeout') {
console.warn("Hmm, request timed out");
} else {
console.warn(t);
}
}
});
var origin = [url];
result.origin = origin[0];
var origObj = [ApiObject];
result.components = origObj[0];
result = new AjaxContent(result);
return result;
}
/////////////////////////////////////////////////////
//// RESULTS time ////////
///////////////////////////////////////////////////
function AjaxContent(jsondata) {
'use strict';
// $.extend(this, new Pearson(jsondata.components.apiKey));
this.json = jsondata
return this;
}
AjaxContent.prototype.raw = function () {
'use strict';
return this.json;
};
AjaxContent.prototype.getUrl = function () {
'use strict';
var results = this.json.results;
var apiKey = this.json.components.apiKey
var len = results.length;
var out = [];
for (var i = 0; i < len; i++) {
out.push({
url: "http://api.pearson.com" + results[i].url + "?" + apiKey
});
}
return out;
};
AjaxContent.prototype.introText = function () {
'use strict';
var results = this.json.results;
var len = results.length;
var out = [];
for (var i = 0; i < len; i++) {
if (typeof results[i].intro !== 'undefined') {
out.push({
title: results[i].title,
text: results[i].intro.text
});
} else {
out.push({
title: results[i].title,
text: 'No introductory text'
});
}
}
return out;
};
// The Next Method - Default returns then
AjaxContent.prototype.nextSet = function () {
'use strict';
var count = this.json.count;
var offset = this.json.offset;
var limit = this.json.limit;
var nextOffset = offset + count;
var nextGroup = this.json.components.addParams({
offset: nextOffset
});
var nextSet = $.fetch(nextGroup);
// var returnedSet = new AjaxContent(nextSet);
// return returnedSet;
return nextSet;
};
// Get a specified url from a selection of results returend
AjaxContent.prototype.getResultUrl = function (num) {
var results = this.json.results;
var i = num;
url = "http://api.pearson.com" + results[i].url + "?" + this.json.components.apiKey;
return url;
}
AjaxContent.prototype.getArticleObject = function (num) {
var results = this.json.results;
var i = num;
url = "http://api.pearson.com" + results[i].url + "?" + this.json.components.apiKey;
$.ajax({
url: url,
type: 'GET',
timeout: 1000, // feel free to mod this
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
async: false,
success: function (data) {
console.log(data);
result = data;
},
error: function (x, t, m) {
if (t === 'timeout') {
console.warn("Hmm, request timed out");
} else {
console.warn(t);
}
}
});
}
/// FoodandDrink Methods
AjaxContent.prototype.getRecipeNames = function(){
var results = this.json.results;
var len = results.length;
var out = [];
for (var i = 0; i < len; i++) {
if (typeof results[i].summary.title !== 'undefined') {
out.push({
recipeName: results[i].summary.title,
id: results[i].id
});
} else {
out.push({
recipeName: "No recipeName found",
id: 'No ID'
});
}
}
return out;
}
AjaxContent.prototype.getRecipeFromName = function(num){ // Where num is the recipe index in the results array
var rId = this.json.results[num].id;
var indRecipe = this.getRecipeById(rId);
return indRecipe;
}
AjaxContent.prototype.getRecipeById = function(id) {
var apiKey = this.json.components.apiKey;
var url = this.json.components.url + this.json.components.endpoint + '/' + id + '?' + apiKey.substr(1);
console.log(url);
$.ajax({
url: url,
type: 'GET',
timeout: 1000, // feel free to mod this
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
async: false,
success: function (data) {
console.log(data);
result = data;
},
error: function (x, t, m) {
if (t === 'timeout') {
console.warn("Hmm, request timed out");
} else {
console.warn(t);
}
}
});
return result;
}