-
Notifications
You must be signed in to change notification settings - Fork 0
/
openweather.js
68 lines (63 loc) · 3.27 KB
/
openweather.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
openWeatherJS = function (apiKey) {
// Request the current weather information, either by city name or by ZIP code
this.requestCurrentWeather = function (jsonCityInfo, successCallback) {
//define callback function up here, which parses the response into a nice JSON
var internalCallback = function (response) {
var JSONresponse = JSON.parse(response);
//now feed it back to the callback
successCallback(JSONresponse);
}
//are we requesting with a ZIP code?
if (jsonCityInfo.zip != undefined) {
// Request with ZIP Code
var url = "https://api.openweathermap.org/data/2.5/weather?zip=" + jsonCityInfo.zip + "&appid=" + apiKey;
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
internalCallback(httpRequest.responseText);
}
httpRequest.open("GET", url, true); // true for asynchronous
httpRequest.send();
} else if (jsonCityInfo.q != undefined) {
//Request with city name and country code, divided by comma
var url = "https://api.openweathermap.org/data/2.5/weather?q=" + jsonCityInfo.q + "&appid=" + apiKey;
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
internalCallback(httpRequest.responseText);
}
httpRequest.open("GET", url, true); // true for asynchronous
httpRequest.send();
}
};
this.requestForecast5 = function (jsonCityInfo, successCallback) {
//define callback function up here, which parses the response into a nice JSON
var internalCallback = function (response) {
var JSONresponse = JSON.parse(response);
//now feed it back to the callback
successCallback(JSONresponse);
}
//are we requesting with a ZIP code?
if (jsonCityInfo.zip != undefined) {
// Request with ZIP Code
var url = "https://api.openweathermap.org/data/2.5/forecast?zip=" + jsonCityInfo.zip + "&appid=" + apiKey;
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
internalCallback(httpRequest.responseText);
}
httpRequest.open("GET", url, true); // true for asynchronous
httpRequest.send();
} else if (jsonCityInfo.q != undefined) {
//Request with city name and country code, divided by comma
var url = "https://api.openweathermap.org/data/2.5/forecast?q=" + jsonCityInfo.q + "&appid=" + apiKey;
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = () => {
if (httpRequest.readyState == 4 && httpRequest.status == 200)
internalCallback(httpRequest.responseText);
}
httpRequest.open("GET", url, true); // true for asynchronous
httpRequest.send();
}
}
}