-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
112 lines (93 loc) · 2.92 KB
/
index.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
const fetch = require('node-fetch');
const convert = require('xml-js');
const get = require('lodash.get');
const BeachConstants = require('./beachConstants');
DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
function getAllBeachesAllTime() {
return getFromApi('beaches/history/all.xml?v=1.0');
}
function getAllBeachesLatest() {
return getFromApi('beaches.xml?v=1.0');
}
function getAllBeachesForRange(startDate, endDate) {
if (!startDate || !endDate) {
throw 'Both a start date and end date are required.';
}
if (!DATE_REGEX.test(startDate) || !DATE_REGEX.test(endDate)) {
throw 'Dates must be formatted YYYY-MM-DD';
}
return getFromApi(`beaches/history.xml?v=1.0&from=${startDate}&to=${endDate}`);
}
function getSpecificBeachForRange(beachID, startDate, endDate) {
if (!beachID || !startDate || !endDate) {
throw 'The beach ID, start date, and end date are all required.';
}
if (!DATE_REGEX.test(startDate) || !DATE_REGEX.test(endDate)) {
throw 'Dates must be formatted YYYY-MM-DD';
}
return getFromApi(`beach/${beachID}/history.xml?v=1.0&from=${startDate}&to=${endDate}`);
}
function getSpecificBeachAllTime(beachID) {
if (!beachID) {
throw 'The beach ID is required.';
}
return getFromApi(`beach/${beachID}/history/all.xml?v=1.0`);
}
function getFromApi(path) {
return fetch(`http://app.toronto.ca/tpha/ws/${path}`)
.then(res => {
return res.text();
})
.then(res => {
const beachData = formatBeachData(res);
return beachData;
})
.catch(err => {
console.log({ err });
});
}
/*
// example usage, commented out so we don't fetch it when this module is included
const test = getSpecificBeachForRange(1, '2019-02-01', '2019-09-31').then(res => {
// console.log({ res });
res.forEach(item => {
// console.log({ item });
});
});
*/
const formatBeachData = (rawData) => {
const data = JSON.parse(
convert.xml2json(rawData, {
compact: false,
spaces: 4
})
);
const beaches = get(data, ['elements', 0, 'elements', 1, 'elements']);
return beaches
.map(beach => {
const beachData = get(beach, ['elements']);
const beachID = get(beach, ['attributes', 'beachId']);
return {
beachID: Number.parseInt(beachID),
name: get(BeachConstants, [beachID, 'name']),
map: get(BeachConstants, [beachID, 'map']),
sampleDate: get(beachData, [0, 'elements', 0, 'text']),
publishDate: get(beachData, [1, 'elements', 0, 'text']),
eColiCount: Number.parseInt(get(beachData, [2, 'elements', 0, 'text'])),
beachAdvisory: get(beachData, [3, 'elements', 0, 'text']),
beachState: get(beachData, [4, 'elements', 0, 'text'])
};
})
.filter(element => {
if (get(element, ['name'])) {
return element;
}
});
}
module.exports = {
getAllBeachesAllTime,
getAllBeachesLatest,
getAllBeachesForRange,
getSpecificBeachForRange,
getSpecificBeachAllTime
};