Skip to content

Instantly share code, notes, and snippets.

@victorkurauchi
Last active January 4, 2019 05:32
Show Gist options
  • Save victorkurauchi/020cac80aecb638b9ae7258bc541d283 to your computer and use it in GitHub Desktop.
Save victorkurauchi/020cac80aecb638b9ae7258bc541d283 to your computer and use it in GitHub Desktop.
weather.js
const axios = require('axios');
const groupBy = require('lodash/groupBy');
class Weather {
constructor() {
this.endpoint = 'https://www.metaweather.com/api';
}
groupByDates(weather) {
return groupBy(weather, (item) => item.applicable_date);
}
getApp() {
return { app: 'Weather', description: 'Work easily with the weather', date: new Date().toString() };
}
async getCity(city) {
const response = await axios.get(`${this.endpoint}/location/search/?query=${city}`);
return response.data;
}
async getWeather(cityId) {
const response = await axios.get(`${this.endpoint}/location/${cityId}`)
return response.data;
}
async getWeatherByCity(city) {
const information = await this.getCity(city);
const { woeid } = information[0];
const { consolidated_weather, sources } = await this.getWeather(woeid);
const dates = this.groupByDates(consolidated_weather);
const result = {
dates,
sources,
};
return result;
}
}
module.exports = Weather;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment