forked from fac-14/Week3TakingAIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
140 lines (131 loc) Β· 4.81 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
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
var apiFunctions = {
// generic API request function
apiRequest: function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
// check response to request is good
if (xhr.readyState == 4 && xhr.status == 200) {
// parse response into object
var parsedObj = JSON.parse(xhr.responseText);
// run callback function after response received
return callback(parsedObj);
}
// Display error message if API request is unsuccessful
else if (xhr.readyState == 4 && xhr.status != 200){
document.getElementById("error").textContent = 'Unable to load data :(';
}
};
xhr.open("GET", url, true);
xhr.send();
},
// get tube line status using API request
getLineStatus: function(lineName) {
// store url for TfL API request
var line = document.getElementById('tube-lines');
var value = line.options[line.selectedIndex].value;
var url =
"https://api.tfl.gov.uk/line/" + value + "/status?details=false";
// make API request to TfL
apiFunctions.apiRequest(url, function(parsedObj) {
parsedObj.forEach(function(line) {
// line status variable
var lineStatus = line.lineStatuses[0].statusSeverityDescription;
// // puts line status text into DOM
console.log(url);
document.getElementById("line-status-text").innerHTML =
lineStatus + " on<br>" + lineName;
// append emoji text
apiFunctions.getEmoji(lineStatus);
// get appropriate gif
return apiFunctions.getGif(lineStatus);
// }
});
});
},
getGif: function(statusDescription) {
// parse TfL status description into sentimentObject key
var sentiment =
apiFunctions.sentimentObject[statusDescription.split(" ").join("")];
// create url for GIPHY API request with sentiment search query
var url =
"https://api.giphy.com/v1/gifs/random?&api_key=dc6zaTOxFJmzC&tag=" +
sentiment;
// make API request to GIPHY
apiFunctions.apiRequest(url, function(parsedObj) {
// Remove error message upon successful API request
document.getElementById("error").textContent = "";
document.getElementById("giphy-gif").src =
parsedObj.data.images.fixed_height.url;
document.getElementById("giphy-gif").classList.remove("hidden-img");
document.getElementById("giphy-gif").classList.add("reveal-img");
});
},
getEmoji: function(statusDescription) {
var sentiment =
apiFunctions.emojiObject[statusDescription.split(" ").join("")];
document.getElementById("line-status-text").innerHTML += sentiment;
},
// convert TfL status descriptions into search terms for GIPHY
sentimentObject: {
GoodService: "happy-dancing",
MinorDelays: "shrugging",
ReducedService: "shrugging",
PlannedClosure: "sighing",
PartClosure: "sighing",
SevereDelays: "screaming",
Suspended: "impatient",
PartSuspended: "impatient",
BusService: "bus",
SpecialService: "nah"
},
// emojis to append to line status
emojiObject: {
GoodService: " πππ",
MinorDelays: " πππ",
ReducedService: " π€¨π€¨π€¨",
PlannedClosure: " π©π«π",
PartClosure: " π©π«π",
SevereDelays: " π¬π‘π",
Suspended: " π π€¬π",
PartSuspended: " π π€¬π",
BusService: " πππ",
SpecialService: " π€π€π€"
},
// colors for logo and submit button changing
logoColorObject: {
bakerloolist: "rgba(174, 97, 24, 1.0)",
centrallist: "rgba(228, 31, 31, 1.0)",
circlelist: "rgba(248, 212, 45, 1.0)",
districtlist: "rgba(0, 165, 117, 1.0)",
handclist: "rgba(232, 153, 168, 1.0)",
jubileelist: "rgba(143, 152, 158, 1.0)",
metrolist: "rgba(137, 50, 103, 1.0)",
northernlist: "rgba(0, 0, 0, 1.0)",
piccadillylist: "rgba(4, 80, 161, 1.0)",
victorialist: "rgba(0, 159, 224, 1.0)",
wandclist: "rgba(112, 195, 206, 1.0)",
dlrlist: "rgba(0, 187, 180, 1.0)",
overgroundlist: "rgba(248, 108, 0, 1.0)",
tflraillist: "rgba(4, 80, 161, 1.0)"
},
// colors for changing background
bgColorObject: {
bakerloolist: "rgba(174, 97, 24, 0.1)",
centrallist: "rgba(228, 31, 31, 0.1)",
circlelist: "rgba(248, 212, 45, 0.1)",
districtlist: "rgba(0, 165, 117, 0.1)",
handclist: "rgba(232, 153, 168, 0.1)",
jubileelist: "rgba(143, 152, 158, 0.1)",
metrolist: "rgba(137, 50, 103, 0.1)",
northernlist: "rgba(0, 0, 0, 0.1)",
piccadillylist: "rgba(4, 80, 161, 0.1)",
victorialist: "rgba(0, 159, 224, 0.1)",
wandclist: "rgba(112, 195, 206, 0.1)",
dlrlist: "rgba(0, 187, 180, 0.1)",
overgroundlist: "rgba(248, 108, 0, 0.1)",
tflraillist: "rgba(4, 80, 161, 0.1)"
}
};
if (typeof module !== "undefined") {
module.exports = apiFunctions;
}