From 1d6ada3e91ed7ba87782dc85b18edc458651f4da Mon Sep 17 00:00:00 2001 From: Jason Williams Date: Sun, 25 Aug 2019 15:16:02 -0600 Subject: [PATCH] Video 37 --- weather-app/app.js | 35 ++++++----------------------------- weather-app/utils/forecast.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 29 deletions(-) create mode 100644 weather-app/utils/forecast.js diff --git a/weather-app/app.js b/weather-app/app.js index b1a812f..5c52f2b 100644 --- a/weather-app/app.js +++ b/weather-app/app.js @@ -1,35 +1,12 @@ -const request = require('request') const geocode = require('./utils/geocode') - -const url = 'https://api.darksky.net/forecast/0466bccb953c0b9daeb091a529da2c0d/37.8267,-122.4233?units=si' - -// request({ url: url, json: true }, (error, response) => { -// if (error) { -// console.log('Unable to connect to weather service!') -// } else if (response.body.error) { -// console.log('Unable to find location') -// } else { -// console.log(response.body.daily.data[0].summary + ' It is currently ' + response.body.currently.temperature + ' degrees out. There is a ' + response.body.currently.precipProbability + '% chance of rain.') -// } -// }) - -// const geocodeURL = 'https://api.mapbox.com/geocoding/v5/mapbox.places/philadelphia.json?access_token=pk.eyJ1IjoiamF5d2xsIiwiYSI6ImYyZjkyMGNlOTgyNGQ5ZWM5ZDExN2FjNGYwMDZmNTQ4In0.y3lv5ddmGhVNSL7wfMpB0g&limit=1' - -// request({ url: geocodeURL, json: true }, (error, response) => { -// if (error) { -// console.log('Unable to connect to location services!') -// } else if (response.body.features.length === 0) { -// console.log ('Unable to find location. Try another search.') -// } else { -// const latitude = response.body.features[0].center[1] -// const longitude = response.body.features[0].center[0] -// console.log(latitude, longitude) -// } -// }) - - +const forecast = require('./utils/forecast') geocode('Boston', (error, data) => { console.log('Error', error) console.log('Data', data) }) + +forecast(-75.7088, 44.1545, (error, data) => { + console.log('Error', error) + console.log('Data', data) +}) diff --git a/weather-app/utils/forecast.js b/weather-app/utils/forecast.js new file mode 100644 index 0000000..e1d5f1e --- /dev/null +++ b/weather-app/utils/forecast.js @@ -0,0 +1,17 @@ +const request = require('request') + +const forecast = (latitude, longitude, callback) => { + const url = 'https://api.darksky.net/forecast/0466bccb953c0b9daeb091a529da2c0d/' + latitude + ',' + longitude + '?units=si' + + request({ url: url, json: true }, (error, response) => { + if (error) { + callback('Unable to connect to weather service!', undefined) + } else if (response.body.error) { + callback('Unable to find location', undefined) + } else { + callback(undefined, response.body.daily.data[0].summary + ' It is currently ' + response.body.currently.temperature + ' degrees out. There is a ' + response.body.currently.precipProbability + '% chance of rain.') + } + }) +} + +module.exports = forecast