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;
}
async getWeatherReport(city) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${this.apiKey}`;
currentWeatherData = undefined;
async getWeatherReport(zipcode) {
const url = `https://api.openweathermap.org/data/2.5/weather?zip=${zipcode}&appid=${this.apiKey}`;
let response = await fetch(url);
if (response.status !== 200) {
return;
}
let data = await response.json();
console.log(data);
console.log(
this.convertToFarenheit(data.main.temp),
this.convertToCelsius(data.main.temp),
);
this.setReport(data);
// Set current weather data
if (data) {
this.setCurrentWeather(data);
this.parseWeatherData();
}
}
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) {
return temp - 273.15;
return Math.round(temp - 273.15);
}
convertToFarenheit(temp) {
return (temp - 273.15) * 1.8 + 32;
return Math.round((temp - 273.15) * 1.8 + 32);
}
setReport(data) {
this.data = data;
}
getReport() {
return this.data;
setCurrentWeather(data) {
this.currentWeatherData = data;
}
}
const api = 'bd5d23eea5751c12b0ef75344e3df932';
const weather = new WeatherComponent(api);
weather.getWeatherReport('Washington DC');
// weather.printWeather();
/*
{
coord: { lon: -94.4335, lat: 33.4501 },
weather: [ { id: 800, main: 'Clear', description: 'clear sky', icon: '01n' } ],
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
}
*/
weather.getWeather(20716).then((data) => console.log(data));
weather.getWeather(22030).then((data) => {
data.convertTemp('C');
console.log(data);
data.convertTemp('F');
console.log(data);
});