From 079d0e738e5632255defb8b5b32dcf848f4a22bc Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 21 Nov 2017 10:11:35 -0500 Subject: [PATCH] promises, and such --- app-promise.js | 47 ++++++++++++++++++++++++++++++++++++++++++ app.js | 1 - credentials.js | 7 +++++++ package-lock.json | 35 +++++++++++++++++++++++++++++++ package.json | 1 + playground/promise.js | 36 ++++++++++++++++++++++++++++++++ playground/promise2.js | 29 ++++++++++++++++++++++++++ 7 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 app-promise.js create mode 100644 credentials.js create mode 100644 playground/promise.js create mode 100644 playground/promise2.js diff --git a/app-promise.js b/app-promise.js new file mode 100644 index 0000000..10eef26 --- /dev/null +++ b/app-promise.js @@ -0,0 +1,47 @@ +const yargs = require('yargs'); +const axios = require('axios'); + +const credentials = require("./credentials") + +const argv = yargs + .options({ + a: { + demand: true, + alias: 'address', + describe: "Address to fetch weather for.", + string: true + } + }) + .help() + .alias('help', 'h') + .argv; + +var encodedAddress = encodeURIComponent(argv.address); +var geocodeUrl = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}&key=${credentials.GOOGLE_API_KEY}`; + +axios.get(geocodeUrl).then((response) => { + if (response.data.status === "ZERO_RESULTS") { + throw new Error('Unable to find that Address.') + } else if (response.data.status === 'REQUEST_DENIED'){ + throw new Error(`API Request Denied: ${response.data.error_message}`); + } + var lat = response.data.results[0].geometry.location.lat; + var lng = response.data.results[0].geometry.location.lng; + console.log(response.data.results[0].formatted_address); + var weatherUrl = `https://api.darksky.net/forecast/${API_KEY}/${lat},${lng}`; + return axios.get(weatherUrl); +}).then((response) => { + var temperature = response.data.currently.temperature; + var apparentTemperature = response.data.currently.apparentTemperature; + console.log(`It's currently: ${temperature}. It feels like ${apparentTemperature}`); +}) +.catch((e) => { + if (e.code === 'ENOTFOUND') { + console.log("Unable to connect to API servers"); + } else { + console.log(e.message); + } +}) + + +console.log("Powered By https://darksky.net/poweredby/"); diff --git a/app.js b/app.js index 0c41d91..49f94f2 100644 --- a/app.js +++ b/app.js @@ -35,4 +35,3 @@ geocode.geocodeAddress(argv.address, (errorMessage, results) => { console.log("Powered By https://darksky.net/poweredby/"); -// ae8dd4c9c3a690289fd7cfe125f50e84 diff --git a/credentials.js b/credentials.js new file mode 100644 index 0000000..4ef7534 --- /dev/null +++ b/credentials.js @@ -0,0 +1,7 @@ +var GOOGLE_API_KEY = process.env.GOOGLE_API_KEY; +var DARKSKY_API_KEY = process.env.DARKSKY_API_KEY; + +module.exports = { + GOOGLE_API_KEY, + DARKSKY_API_KEY +} diff --git a/package-lock.json b/package-lock.json index 6896cd3..4120f0c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,6 +45,14 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.6.0.tgz", "integrity": "sha1-g+9cqGCysy5KDe7e6MdxudtXRx4=" }, + "axios": { + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.13.1.tgz", + "integrity": "sha1-Pmer/kMzvJ0tX+b70TtGlOr8jfg=", + "requires": { + "follow-redirects": "0.0.7" + } + }, "bcrypt-pbkdf": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz", @@ -136,6 +144,14 @@ "assert-plus": "1.0.0" } }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "requires": { + "ms": "2.0.0" + } + }, "decamelize": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", @@ -192,6 +208,15 @@ "pinkie-promise": "2.0.1" } }, + "follow-redirects": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-0.0.7.tgz", + "integrity": "sha1-NLkLqyqRGqNHVx2pDyK9NuzYqRk=", + "requires": { + "debug": "2.6.9", + "stream-consume": "0.1.0" + } + }, "forever-agent": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", @@ -381,6 +406,11 @@ "mime-db": "1.30.0" } }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, "normalize-package-data": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", @@ -583,6 +613,11 @@ "tweetnacl": "0.14.5" } }, + "stream-consume": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/stream-consume/-/stream-consume-0.1.0.tgz", + "integrity": "sha1-pB6tGm1ggc63n2WwYZAbbY89HQ8=" + }, "string-width": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", diff --git a/package.json b/package.json index eea3135..eca3131 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "axios": "^0.13.1", "request": "^2.83.0", "yargs": "^4.8.1" } diff --git a/playground/promise.js b/playground/promise.js new file mode 100644 index 0000000..e9170e6 --- /dev/null +++ b/playground/promise.js @@ -0,0 +1,36 @@ +var asyncAdd = (a, b) => { + return new Promise((resolve, reject) => { + setTimeout(() => { + if (typeof a === 'number' && typeof b === 'number'){ + resolve(a + b); + } else { + reject("Arguments must be numbers"); + } + }, 500); + + }) +} +// +// +// var somePromise = new Promise((resolve, reject) => { +// setTimeout(() => { +// // resolve("Hey, It worked!"); +// reject("FAILED BRUH"); +// }, 500); +// }); +// +// somePromise.then((message) => { +// console.log("Success:", message); +// }, +// (errorMessage) => { +// console.log("Error:", errorMessage); +// }) + + +asyncAdd(1, 2).then((res) => { + return asyncAdd(res, 33); +}).then((res) => { + console.log("Should be 36", res); +}).catch((errorMessage) => { + console.log(errorMessage); +}) diff --git a/playground/promise2.js b/playground/promise2.js new file mode 100644 index 0000000..3d98824 --- /dev/null +++ b/playground/promise2.js @@ -0,0 +1,29 @@ +const request = require('request') + +var geocodeAddress = (address) => { + return new Promise((resolve, reject) => { + var encodedAddress = encodeURIComponent(address); + request({ + url: `https://maps.googleapis.com/maps/api/geocode/json?address=${encodedAddress}`, + json: true + }, (error, response, body) => { + if (error) { + reject("Unable to connect to Google Servers."); + } else if (body.status === "ZERO_RESULTS"){ + reject("Unable to find that Address."); + } else if (body.status === "OK"){ + resolve({ + address: body.results[0].formatted_address, + latitude: body.results[0].geometry.location.lat, + longitude: body.results[0].geometry.location.lng + }); + } + }); + }) +}; + +geocodeAddress(19146).then((location) => { + console.log(JSON.stringify(location, null, 2)); +}, (errorMessage) => { + console.log(errorMessage); +})