Skip to content

Instantly share code, notes, and snippets.

@tesh254
Last active November 19, 2019 15:16
Show Gist options
  • Save tesh254/3d35bb74e18a6a256f72d36cf81cee42 to your computer and use it in GitHub Desktop.
Save tesh254/3d35bb74e18a6a256f72d36cf81cee42 to your computer and use it in GitHub Desktop.
const axios = require("axios");
class FetchWeather {
constructor(apiKey) {
this.apiKey = apiKey;
this.locations = ["nairobi"];
}
getWeatherByLocations() {
this.locations.map(location => {
const url =
`http://api.openweathermap.org/data/2.5/weather?q=${location}&units=imperial&appid=${this.apiKey}` ||
`http://api.openweathermap.org/data/2.5/weather?zip=${location}&units=imperial&appid=${this.apiKey}`;
axios
.get(url)
.then(async Res => {
console.log(
`It is ${Res.data.main.temp} degrees in ${Res.data.name}`
);
})
.catch(err => {
console.log(err.response.data);
});
});
}
async addWeatherToLocation(loc) {
// Check if loc is array
if (typeof loc === "object") {
this.locations = [...this.locations, ...loc];
}
// Check if type of string
else {
this.locations.push(loc);
}
this.getWeatherByLocations();
}
}
let apiKey = "<apiKey>";
const weather = new FetchWeather(apiKey);
weather.getWeatherByLocations();
weather.addWeatherToLocation(["kampala"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment