Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
Forecast
node v4.9.1
version:
1.0.0
endpoint
share
tweet
require("request"); // peer dependency var request = require('request-promise'); var _ = require('lodash'); var url = require('url'); var apiKey = process.env.OPENWEATHERMAP_KEY; function temperatureURL(city) { return url.format({ protocol: "http", host: "api.openweathermap.org", pathname: "data/2.5/forecast", query: {units: "imperial", mode: "json", q: city, APPID: apiKey} }); } async function threeDay(city) { if (!apiKey) { throw `<div> <h2>Whoops!</h2> <p>You need an API key to use this service. Sign up at <a href="http://home.openweathermap.org/users/sign_up"> openweathermap.org</a>, and grab your API key:<br /><br /> <img src="http://bit.ly/1GgoJZm" width="500" /></p> <p>Now, add <code>OPENWEATHERMAP_KEY</code> to your <a href="https://tonicdev.com/settings/environment"> environment settings</a>:<br /><br /> <img src="http://bit.ly/1kmRPfQ" width="500" /></p> </div>` } var response = await request(temperatureURL(city)); return _.map(JSON.parse(response)['list'], function(entry) { return entry["main"]["temp"]; }); }; module.exports = { threeDay: threeDay };
Exports a utility function to grab a 3 day weather forecast from openweathermap.org.
Loading…
no comments
sign in
to comment