-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.js
139 lines (123 loc) · 4.21 KB
/
stop.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
// Dependencies
const request = require('request');
const readline = require('readline');
function Stop (Core) {
function list () {
const stops = Core.getAllStops();
const names = Object.keys(stops);
if (!names.length) {
console.log('');
console.log('You haven\'t saved any stops yet.');
console.log('');
} else {
console.log('');
console.log('----------------------------------------------------------------------');
names.forEach((name, i) => {
if (i) console.log('- - - - - - - - - - - - - - - - - - - - - - - -');
console.log('| ' + name + ' | ' + stops[name].name + ' (ID: ' + stops[name].id + ')');
});
console.log('----------------------------------------------------------------------');
console.log('');
}
}
function add (string) {
Core.getPlanKey()
.then(key => search(string, key))
.then(stop => Core.addStopOld(stop))
.catch(error => console.log(error));
}
function remove (string) {
Core.removeStop(string);
}
function search (input, key) {
return new Promise((resolve, reject) => {
const url = Core.buildStopSearchUrl(key, input);
console.log('');
console.log('Searching for ' + input + '..');
request.get(url, (error, response, body) => {
if (error) {
if (error.code === 'ENOTFOUND') {
reject('Sorry, but you don\'t seem to have an internet connection, so we can\'t proceed with this action at the moment.');
} else {
reject('Oops, something went wrong when we tried to find your stop. Sorry about that.');
}
return;
}
const stops = JSON.parse(body).StopLocation;
if (!stops.length) {
console.log('Could not find a stop with a name similar to ' + input);
} else if (stops.length === 1) {
console.log('Found only one result (' + stops[0].name + '), so I chose that for you. :)');
resolve(stops[0]);
} else {
chooseStop(stops).then(stop => resolve(stop));
}
});
});
function chooseStop (stops) {
return new Promise((resolve, reject) => {
console.log('');
console.log('We found the following stops with a name similar to "' + input + '"');
console.log('----------------------------------------------------------------------');
stops.forEach((stop, i) => {
if (i) console.log('- - - - - - - - - - - - - - - - - - - - - - - -');
console.log('| ' + (i+1) + ' | ' + stop.name);
});
console.log('----------------------------------------------------------------------');
console.log('');
askToChoose(stops).then((stop) => {
resolve(stop);
});
});
function askToChoose (stops) {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('Select one of the stops above using the number assigned to it: ');
rl.on('line', (answer) => {
const check = validateChoice(answer);
rl.close();
if (!check.valid) {
console.log('');
console.log(check.error);
console.log('');
return askToChoose(stops).then((stop) => resolve(stop));
}
resolve(stops[check.answer - 1]);
});
});
}
function validateChoice (answer) {
const num = parseInt(answer);
if (isNaN(num)) {
return {
valid: false,
error: 'Hey, that\'s not a number.. -_-'
};
}
if (num <= 0) {
return {
valid: false,
error: 'Yeah, you\'re going to have to use a number greater than 0. Duh?'
};
}
if (num > stops.length) {
return {
valid: false,
error: 'Uhm, that\'s more than the number of stops you can choose?'
};
}
return { valid: true, answer: num };
}
}
}
return {
add,
remove,
list,
search
}
}
module.exports = Stop;