forked from CorndelWithSoftwire/busboard-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsoleRunner.js
46 lines (39 loc) · 1.38 KB
/
consoleRunner.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
import { createInterface } from 'readline';
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
export default class ConsoleRunner {
constructor(departureBoardsService) {
this.departureBoardsService = departureBoardsService;
}
handleError(reason) {
console.error('\n' + reason);
this.runForever();
}
promptForPostcode() {
return new Promise((resolve, reject) =>
readline.question('\nEnter your postcode: ', resolve)
);
}
displayArrivals(stopPoint, arrivals) {
console.log(`\nDeparture board for ${stopPoint.commonName}:`);
arrivals.forEach(arrival =>
console.log(` ${Math.round(arrival.timeToStation / 60)} minutes: ${arrival.lineName} to ${arrival.destinationName}`)
);
}
displayDepartureBoards(departureBoards) {
departureBoards.forEach(board => {
this.displayArrivals(board.stopPoint, board.arrivals);
});
}
runForever() {
this.promptForPostcode()
.then(postcode => this.departureBoardsService.getDepartureBoardsForPostcode(postcode.replace(/\s/g, ''), 2, 5))
.then(departureBoards => {
this.displayDepartureBoards(departureBoards);
this.runForever();
})
.catch(reason => this.handleError(reason));
}
}