-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
58 lines (40 loc) · 1.53 KB
/
weather.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
const config = require('./config');
const http = require('http');
const { API_KEY } = config;
const printData = (data, cityAndState) => {
try {
cleanData = JSON.parse(data);
if (!cleanData.current_observation || !cleanData.current_observation.temperature_string) {
console.log('ran inside if');
const message = ` Could not find the weather for ${cityAndState[0]}, ${cityAndState[1]}`;
const tempNotFoundError = new Error(message);
printError(tempNotFoundError);
return;
}
console.log(`The temperature in ${cityAndState[0]}, ${cityAndState[1]} is: ${cleanData.current_observation.temperature_string}`);
} catch(e) {
printError(e);
}
};
const printError = e => console.error('Error is:', e.message);
const getInfo = cityAndState => {
const city = cityAndState[0];
const state = cityAndState[1];
const URL = `http://api.wunderground.com/api/${API_KEY}/conditions/q/${state}/` +
`${city.replace(' ', '_')}.json`;
try {
const request = http.get(URL, response => {
if (response.statusCode !== 200) {
const message = `There was an error getting the weather, STATUSCODE: ${response.statusCode} | ` +
`${http.STATUS_CODES[response.statusCode]} `;
}
let dataString = '';
response.on('data', data => dataString += data);
response.on('end', () => printData(dataString, cityAndState));
}).on('error', printError);;
} catch(e) {
const urlError = new Error('URL parsing error');
printError(urlError);
}
}
module.exports = { getInfo };