-
Notifications
You must be signed in to change notification settings - Fork 1
/
logic.js
74 lines (60 loc) · 2.08 KB
/
logic.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
if (typeof module !== 'undefined'){
var severityGifMap = require("./map.js");
var linesObj = require('./lines.js');
}
var logicObj = {
resultsObj: {},
makeTflUrl: function() {
var urlStart = 'https://api.tfl.gov.uk/Line/';
var urlEnd = '/Status?app_id=de9e1a2e&app_key=41bcfcc2d033bae16403b619c8ec1613';
return urlStart + logicObj.resultsObj.line + urlEnd;
},
makeGiphyUrl: function() {
var urlBase = "https://api.giphy.com/v1/gifs/search?api_key=03d284987c9444e8931acbc0601067d3&limit=30&offset=0&rating=G&lang=en&q=";
var searchTerm = severityGifMap[logicObj.resultsObj.status];
return urlBase + searchTerm;
},
apiCall: function(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var data = JSON.parse(xhr.responseText);
cb(data);
}
};
xhr.open("GET", url, true);
xhr.send();
},
tflCb: function(tflData) {
var lineStatus = tflData[0].lineStatuses[0].statusSeverityDescription;
logicObj.resultsObj.status = lineStatus;
},
giphyCb: function(giphyData) {
var randomNum = Math.floor(Math.random()*30);
var gifSrc = giphyData.data[randomNum].images.fixed_height.url;
logicObj.resultsObj.url = gifSrc;
},
waterfall: function(tasksArray, urlsArray, render) {
if (tasksArray.length > 0) {
var task = tasksArray[0];
var remainingTasks = tasksArray.slice(1);
var url = urlsArray[0]();
var remainingUrls = urlsArray.slice(1);
logicObj.apiCall(url, function(data) {
task(data);
logicObj.waterfall(remainingTasks, remainingUrls, render);
})
}
else {
render(logicObj.resultsObj);
}
},
init: function(line, lineName, render) {
logicObj.resultsObj.line = line;
logicObj.resultsObj.lineName = lineName;
logicObj.waterfall([logicObj.tflCb, logicObj.giphyCb], [logicObj.makeTflUrl, logicObj.makeGiphyUrl], render);
}
}
if (typeof module !== 'undefined'){
module.exports=logicObj;
}