· 6 years ago · Mar 18, 2020, 09:06 PM
1// OpenWeatherMap API key: 20cb76773b348b6df21fe621537b86e5
2
3export class Weather {
4 constructor(apiKey, city) {
5 this._apiKey = apiKey;
6 this._city = city;
7 }
8
9 update() {
10 return this.updateJson().then(() => {
11 if (this.jsonData["cod"] === "404" || this.jsonData["cod"] === "400") {
12 this.status = "";
13 this.icon = "";
14 this.temperature = "";
15 return;
16 }
17
18 let status = this.jsonData['weather']['0']['description'];
19 this.status = status[0].toUpperCase() + status.slice(1);
20
21 this.icon = "http://openweathermap.org/img/w/" + this.jsonData['weather']['0']['icon'] + ".png";
22 this.temperature = this.jsonData['main']['temp'] - 273.15;
23 });
24 }
25
26 updateJson() {
27 let self = this;
28 const call = "http://api.openweathermap.org/data/2.5/weather?q=" + this._city + "&appid=" + this._apiKey + "&lang=nl";
29
30 return fetch(call)
31 .then((response) => response.json())
32 .then((json) => {
33 self._jsonData = json;
34 });
35 }
36
37 get jsonData() {
38 return this._jsonData;
39 }
40
41 get status() {
42 return this._status;
43 }
44
45 set status(value) {
46 this._status = value;
47 }
48
49 get icon() {
50 return this._icon;
51 }
52
53 set icon(value) {
54 this._icon = value;
55 }
56
57 get temperature() {
58 return this._temperature;
59 }
60
61 set temperature(value) {
62 this._temperature = Math.round(value);
63 }
64
65 get city() {
66 return this._city;
67 }
68
69 set city(value) {
70 this._city = value;
71 }
72}