feat: async method/conversions

This commit is contained in:
Mike 2024-01-18 00:07:38 -05:00
parent 693f5a3560
commit 5af94fa28b

View file

@ -3,80 +3,94 @@ class WeatherComponent {
this.apiKey = apiKey; this.apiKey = apiKey;
} }
async getWeatherReport(city) { currentWeatherData = undefined;
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${this.apiKey}`;
async getWeatherReport(zipcode) {
const url = `https://api.openweathermap.org/data/2.5/weather?zip=${zipcode}&appid=${this.apiKey}`;
let response = await fetch(url); let response = await fetch(url);
if (response.status !== 200) {
return;
}
let data = await response.json(); let data = await response.json();
console.log(data);
console.log( // Set current weather data
this.convertToFarenheit(data.main.temp), if (data) {
this.convertToCelsius(data.main.temp), this.setCurrentWeather(data);
); this.parseWeatherData();
this.setReport(data); }
}
parseWeatherData() {
const self = this;
let data = {
time: this.currentWeatherData.dt,
weather: {
main: this.currentWeatherData.weather[0].main,
description: this.currentWeatherData.weather[0].description,
},
temp: {
current: this.currentWeatherData.main.temp,
feelsLike: this.currentWeatherData.main.feels_like,
minTemp: this.currentWeatherData.main.temp_min,
maxTemp: this.currentWeatherData.main.temp_max,
},
wind: this.currentWeatherData.wind,
sunrise: this.currentWeatherData.sys.sunrise,
sunset: this.currentWeatherData.sys.sunset,
city: this.currentWeatherData.name,
convertTemp(conversionType) {
this[conversionType] = {};
const method =
conversionType === 'F'
? self.convertToFarenheit
: self.convertToCelsius;
const tempKeys = Object.keys(this.temp);
tempKeys.forEach((element) => {
this[conversionType][element] = method(this.temp[element]);
});
},
};
if (data) this.parsedWeatherData = data;
}
async getWeather(zip) {
try {
await this.getWeatherReport(zip);
return (
this.parsedWeatherData || {
error: 'Sorry, unable to fetch current weather at this time. :(',
}
);
} catch (error) {
console.log(error.message);
return { error: 'issue occured while fetching weather data!' };
}
} }
// async printWeather() {
// let self = this;
// let wait = new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve(this.getData);
// }, 5000);
// }).then(() => {
// console.log(self.getData());
// });
// }
//
convertToCelsius(temp) { convertToCelsius(temp) {
return temp - 273.15; return Math.round(temp - 273.15);
} }
convertToFarenheit(temp) { convertToFarenheit(temp) {
return (temp - 273.15) * 1.8 + 32; return Math.round((temp - 273.15) * 1.8 + 32);
} }
setReport(data) { setCurrentWeather(data) {
this.data = data; this.currentWeatherData = data;
}
getReport() {
return this.data;
} }
} }
const api = 'bd5d23eea5751c12b0ef75344e3df932'; const api = 'bd5d23eea5751c12b0ef75344e3df932';
const weather = new WeatherComponent(api); const weather = new WeatherComponent(api);
weather.getWeatherReport('Washington DC'); weather.getWeather(20716).then((data) => console.log(data));
// weather.printWeather(); weather.getWeather(22030).then((data) => {
/* data.convertTemp('C');
{ console.log(data);
coord: { lon: -94.4335, lat: 33.4501 }, data.convertTemp('F');
weather: [ { id: 800, main: 'Clear', description: 'clear sky', icon: '01n' } ], console.log(data);
base: 'stations', });
main: {
temp: 264.97,
feels_like: 261.85,
temp_min: 262.75,
temp_max: 266.19,
pressure: 1031,
humidity: 60,
sea_level: 1031,
grnd_level: 1017
},
visibility: 10000,
wind: { speed: 1.61, deg: 299, gust: 2.97 },
clouds: { all: 0 },
dt: 1705456294,
sys: {
type: 2,
id: 2011572,
country: 'US',
sunrise: 1705411289,
sunset: 1705447968
},
timezone: -21600,
id: 4675805,
name: 'Bowie',
cod: 200
}
*/