forecastioplus
Version:
fetches forecast data from forcast.io api
36 lines (29 loc) • 689 B
JavaScript
var https = require('https');
function Forecast(key){
this.key = key;
}
Forecast.prototype.fetch = function(cord, callback){
// Build path
var domain = "https://api.forecast.io/forecast/";
domain += this.key + "/";
domain += cord.lat + "," + cord.long;
// Make request
https.get(domain, function(res){
var body = "";
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
if ( res.statusCode === 200 ){
var forcastdata = JSON.parse(body);
callback(forcastdata);
} else {
callback({
'error': `${res.statusCode} - ${res.statusMessage}`
});
}
});
});
}
// export constructor function
exports.Forecast = Forecast;