var express = require("express");
function tonicExpress(anExport)
{
var mount = express();
var app = express();
// "mount" is our root app, and it mounts "app" at your notebook path
// which is available in the TONIC_MOUNT_PATH environment variable
mount.use(process.env.TONIC_MOUNT_PATH || "", app);
if (anExport) {
anExport.tonicEndpoint = mount;
}
// overwrite .listen since it is not needed
app.listen = function(){}
// return the express instance for use by the caller
return app;
}
module.exports = tonicExpress
var R = require("ramda");
var cities = ["New York", "San Francisco"];
var forecast = require("notebook")("tonic/forecast/2.0.0");
var promises = R.map(forecast.threeDay, cities);
R.zipObj(cities, await Promise.all(promises));
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
};
You probably played the game of telephone as a kid, but it's kind of boring as an adult. But what if we get the computer to play for us instead?
First, let's require some utilities from other notebooks, and define a few of our own.