Skip to content

Commit 5bcd9ec

Browse files
committed
added json format param for source=2 api
1 parent 7e98b43 commit 5bcd9ec

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ app.get('/', (req, res) => {
3636
const source = req.query.source ? Number(req.query.source) : 1;
3737

3838
if (source === 2) {
39-
return getWorldoMetersTable({ isCurl, emojis, minimal, top })
39+
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
4040
.then(result => {
4141
return res.send(result);
4242
}).catch(error => errorHandler(error, res));
@@ -107,7 +107,7 @@ app.get('/:country', (req, res) => {
107107
const { iso2 } = lookupObj;
108108

109109
if (source === 2) {
110-
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal })
110+
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
111111
.then(result => {
112112
return res.send(result);
113113
}).catch(error => errorHandler(error, res));

lib/api.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
/**
2+
* Data Source File
3+
* 1. Fetch data from source.
4+
* 2. Process data.
5+
* 3. Add to cache and return
6+
*/
7+
18
const NodeCache = require('node-cache');
29
const axios = require('axios');
310
const { countryNameMap } = require('./constants');
411

512
const myCache = new NodeCache({ stdTTL: 100, checkperiod: 600 });
613

14+
/**
15+
* John Hopkins Univ. data source API.
16+
* The data needs to be flattened.
17+
* @todo Move the data processing from `view` files to this `data` file.
18+
* @returns {Object} JHUDataObject
19+
*/
720
exports.getCoronaData = async () => {
821
const CORONA_ALL_KEY = 'coronaAll';
922
const cache = myCache.get(CORONA_ALL_KEY);
@@ -20,7 +33,12 @@ exports.getCoronaData = async () => {
2033
return result.data;
2134
};
2235

23-
/** Fetch Worldometers Data */
36+
/**
37+
* Fetch Worldometers Data.
38+
* As JHU data updates once a day, this was added.
39+
* This API scrapes data from `https://www.worldometers.info/coronavirus/`
40+
* and updates very frequenly.
41+
* */
2442
exports.getWorldoMetersData = async (countryCode = 'ALL') => {
2543
const key = `worldMetersData_${countryCode}`;
2644
const cache = myCache.get(key);
@@ -44,7 +62,8 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
4462
acc.critical += countryObj.critical;
4563
return acc;
4664
}, {
47-
countryName: 'World',
65+
country: 'World',
66+
countryCode: 'World',
4867
cases: 0,
4968
todayCases: 0,
5069
deaths: 0,
@@ -57,7 +76,6 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
5776
result.data.forEach(obj => obj.countryCode = countryNameMap[obj.country]);
5877
worldStats.casesPerOneMillion = (worldStats.cases / 7794).toFixed(2);
5978
let finalData = result.data;
60-
console.log(countryCode);
6179
if (countryCode && countryCode !== 'ALL') {
6280
finalData = finalData.filter(obj => obj.countryCode === countryCode);
6381
}

lib/worldoMeters.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ exports.getWorldoMetersTable = async ({
99
isCurl = true,
1010
emojis = false,
1111
minimal = false,
12-
top = 1000
12+
top = 1000,
13+
format,
1314
}) => {
1415
const secondColumnName = countryCode ? 'Country': 'World';
1516
const table = new Table({
@@ -18,6 +19,10 @@ exports.getWorldoMetersTable = async ({
1819
style: helpers.getTableStyles(minimal),
1920
});
2021
const { data, worldStats } = await api.getWorldoMetersData(countryCode);
22+
console.log(format);
23+
if (format === 'json') {
24+
return { data, worldStats };
25+
}
2126

2227
let rank = 1;
2328
data.some(cd => {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coronavirus-tracker-cli",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "track conronavirus cases from cli",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)