Last active
January 4, 2019 05:32
-
-
Save victorkurauchi/020cac80aecb638b9ae7258bc541d283 to your computer and use it in GitHub Desktop.
weather.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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