From b09deeab20710ca5bf19218ed360ca3c8dd70913 Mon Sep 17 00:00:00 2001 From: sharpshark28 Date: Fri, 1 Jul 2016 19:19:38 -0500 Subject: [PATCH] First commit, 1.0 --- README.md | 13 ++++++++++++- app.js | 43 +++++++++++++++++++++++++++++++++++++++++++ package.json | 11 +++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app.js create mode 100644 package.json diff --git a/README.md b/README.md index 033b0c9..f7abda9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ -# can-i-disc-golf-today +# Can I Disc Golf Today? A Webtask ready api ready to tell you if the weather looks good for Disc Golfing this weekend. + +[Premade example](https://webtask.it.auth0.com/api/run/wt-sharpshark28-gmail_com-0/discGolfWeekend?webtask_no_cache=1) + +## How do I generate my own? +This tool is built on Node/npm/[Webtask](https://webtask.io). Clone this repo locally and within it run: + +``` +wt create app.js --secret OPEN_WEATHER_KEY={Your Key} +``` + +Where `{Your Key}` is an API key you generate from [OpenWeatherMap](https://openweathermap.org/). diff --git a/app.js b/app.js new file mode 100644 index 0000000..59a5e25 --- /dev/null +++ b/app.js @@ -0,0 +1,43 @@ +'use latest'; +require('isomorphic-fetch'); + +module.exports = function(ctx, done) { + // Params + let appid = ctx.secrets.OPEN_WEATHER_KEY; + let where = ctx.data.where || 'Austin,US'; + let tempRange = [ctx.data.min || 50, ctx.data.max || 100]; //Imperial (F) + let windMaxSpeed = ctx.data.wind || 50; + // URL built from above params + let weatherCastURL = `http://api.openweathermap.org/data/2.5/forecast/daily?mode=json&units=imperial&country=US&cnt=7&q=${where}&APPID=${appid}`; + // Weekend days offset from today + let today = new Date().getDay(); + let saturday = 6 - today; + let sunday = 7 - today; + // Function to decide if each day's weather is appropriate for disc golfing + let analyze = days => days.map((day, i) => { + // Poor weather conditions and relevant user message + let notClear = day.weather[0].main !== 'Clear' + ? `Its calling for ${day.weather[0].description}.` : ''; + let tooCold = day.temp.day < tempRange[0] + ? `Brr... Its only ${Math.round(day.temp.day)}F.`: ''; + let tooHot = day.temp.day >= tempRange[1] + ? `Phew! Its going to be ${Math.round(day.temp.day)}F.` : ''; + let tooWindy = day.speed > windMaxSpeed + ? `May be too windy with wind speeds of ${day.speed}mph.` : ''; + let details = notClear || tooHot || tooCold || tooWindy || 'Go Disc Golfing!'; + // Build object to return for each day + return { + day: ['Saturday', 'Sunday'][i], + greatWeather: details === 'Go Disc Golfing!', + details + }; + }); + + // Fetch weather + fetch(weatherCastURL) + .then(r => r.json()) + .then(r => [r.list[saturday], r.list[sunday]]) // We only care about the weekends + .then(r => analyze(r)) // Map over each day to determine if they're good for disc golf + .then(r => done(null, r)) // Return content to the user + .catch(e => done(null, e.error)); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..e324e18 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "can-i-disc-golf-this-weekend", + "version": "1.0.0", + "description": "A Webtask ready api ready to tell you if the weather looks good for Disc Golfing this weekend.", + "main": "app.js", + "author": "Joe Wroten ", + "license": "ISC", + "dependencies": { + "isomorphic-fetch": "^2.2.1" + } +}