Created
April 29, 2016 13:18
-
-
Save knowthen/d29e416f19bc64e785561e61166c062e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
var request = require('request'); | |
/** | |
* Find the temperature at a location | |
* @param {String} location | |
* @param {function} callBack | |
* @return {Promise} | |
*/ | |
var getTemperatureByLatLong = function(latitude, longitude, callBack){ | |
var temp; | |
var def = Promise.defer(); | |
var APIKEY = 'YOURAPIKEYHERE'; | |
var url = 'http://api.openweathermap.org/data/2.5/weather?appid=' + APIKEY + '&units=imperial&lat=' + latitude + '&lon=' + longitude; | |
console.log(url); | |
var options = { | |
url: url, | |
json: true | |
}; | |
request(options, function(err, response, body){ | |
if(err){ | |
if(callBack){ | |
callBack(err); | |
} | |
def.reject(err); | |
} | |
else{ | |
if(body && body.main && body.main.temp){ | |
temp = body.main.temp; | |
} | |
if(callBack){ | |
callBack(null, temp); | |
} | |
def.resolve(temp); | |
} | |
}); | |
return def.promise; | |
} | |
module.exports = { | |
getTemperatureByLatLong: getTemperatureByLatLong | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment