Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

forecastio

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

forecastio - npm Package Compare versions

Comparing version 0.2.0 to 1.0.0

99

index.js

@@ -1,8 +0,11 @@

var request = require('request');
var qs = require('querystring');
var util = require('util');
var request = require("request-promise");
var util = require("util");
defaultRequestOptions = {};
allowedRequestOptions = ['timeout'];
var DEFAULT_REQUEST_OPTIONS = {
json: true,
simple: false,
resolveWithFullResponse: true
};
var ALLOWED_REQUEST_OPTIONS = ["timeout"];

@@ -12,8 +15,8 @@ function ForecastIo(apiKey, requestOptions) {

this.apiKey = apiKey;
this.baseUrl = 'https://api.forecast.io/forecast/' + this.apiKey + '/';
this.baseUrl = "https://api.forecast.io/forecast/" + this.apiKey + "/";
}
ForecastIo.prototype.forecast = function(latitude, longitude, options, callback) {
if (typeof options === 'function') {
callback = options;
ForecastIo.prototype.forecast = function(latitude, longitude, options, optionalCallback) {
if (typeof options === "function") {
optionalCallback = options;
options = {};

@@ -23,11 +26,8 @@ }

var url = this.buildUrl(latitude, longitude);
this.makeRequest(url, options, function(err, data) {
if (err) return callback(err);
return callback(null, data);
});
return this.makeRequest(url, options, optionalCallback);
};
ForecastIo.prototype.timeMachine = function(latitude, longitude, time, options, callback) {
if (typeof options === 'function') {
callback = options;
ForecastIo.prototype.timeMachine = function(latitude, longitude, time, options, optionalCallback) {
if (typeof options === "function") {
optionalCallback = options;
options = {};

@@ -37,6 +37,3 @@ }

var url = this.buildUrl(latitude, longitude, time);
this.makeRequest(url, options, function(err, data) {
if (err) return callback(err);
return callback(null, data);
});
return this.makeRequest(url, options, optionalCallback);
};

@@ -46,5 +43,5 @@

if (typeof userOptions === "undefined")
return defaultRequestOptions;
var options = {};
allowedRequestOptions.forEach(function(optionName) {
return DEFAULT_REQUEST_OPTIONS;
var options = cloneObject(DEFAULT_REQUEST_OPTIONS);
ALLOWED_REQUEST_OPTIONS.forEach(function(optionName) {
if (userOptions[optionName])

@@ -57,5 +54,5 @@ options[optionName] = userOptions[optionName];

ForecastIo.prototype.buildUrl = function(latitude, longitude, time) {
var url = this.baseUrl + latitude + ',' + longitude;
if (typeof time !== 'undefined') {
url += ',' + time
var url = this.baseUrl + latitude + "," + longitude;
if (typeof time !== "undefined") {
url += "," + time;
}

@@ -66,21 +63,25 @@

ForecastIo.prototype.makeRequest = function(url, queryString, callback) {
ForecastIo.prototype.makeRequest = function(url, queryString, optionalCallback) {
var requestOptions = this.requestOptions;
requestOptions.uri = url;
requestOptions.qs = queryString;
request.get(requestOptions, function(err, res, body) {
if (err) return callback(new ForecastIoError(err));
if (res.statusCode !== 200) {
return callback(new ForecastIoAPIError(res.request.uri.href, res.statusCode, body));
}
var data;
try {
data = JSON.parse(body);
} catch (e) {
return callback(e);
}
var promise = request(requestOptions)
.then(function(response) {
if (response.statusCode != 200) {
throw new ForecastIoAPIError(url, response.statusCode, response.body);
}
callback(null, data);
});
return response.body;
});
if (typeof(optionalCallback) !== "undefined") {
promise
.then(function(data) {
optionalCallback(null, data)
})
.catch(optionalCallback);
} else {
return promise;
}
};

@@ -91,7 +92,2 @@

function ForecastIoAPIError(url, statusCode, body) {
// Try to parse error response's body, since it's most probably JSON
try {
body = JSON.parse(body)
} catch(e) {}
this.response = {

@@ -102,6 +98,6 @@ statusCode: statusCode,

this.message = this._formatErrorMessage(body);
this.name = 'ForecastIoAPIError';
this.name = "ForecastIoAPIError";
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
this.request = 'GET ' + url;
this.request = "GET " + url;
}

@@ -125,12 +121,7 @@ util.inherits(ForecastIoAPIError, Error);

/** Represents generic errors. Like timeouts, networking issues etc. */
function ForecastIoError(cause) {
this.name = "ForecastIoError";
this.message = cause.message;
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
function cloneObject(obj) {
return JSON.parse(JSON.stringify(obj));
}
util.inherits(ForecastIoError, Error);
module.exports = ForecastIo;
{
"name": "forecastio",
"version": "0.2.0",
"version": "1.0.0",
"description": "A node.js wrapper for Forecast.io API",
"main": "index.js",
"engines": {
"node" : ">=0.10 <0.12"
"node": ">=0.10 <0.12"
},

@@ -28,3 +28,3 @@ "scripts": {

"dependencies": {
"request": "^2.40.0"
"request-promise": "^2.0.0"
},

@@ -31,0 +31,0 @@ "devDependencies": {

@@ -5,2 +5,3 @@ forecastio

[![Build Status](https://travis-ci.org/soplakanets/node-forecastio.svg?branch=master)](https://travis-ci.org/soplakanets/node-forecastio) [![Dependency Status](https://david-dm.org/soplakanets/node-forecastio.png)](https://david-dm.org/soplakanets/node-forecastio)
![Badge](https://img.shields.io/badge/has%20nifty%20badges-yes-green.svg)

@@ -26,4 +27,3 @@ A node.js client library for [Forecast.io API](https://developer.forecast.io).

var forecastIo = new ForecastIo('<apiKey>');
forecastIo.forecast('51.506', '-0.127', function(err, data) {
if (err) throw err;
forecastIo.forecast('51.506', '-0.127').then(function(data) {
console.log(JSON.stringify(data, null, 2));

@@ -39,4 +39,3 @@ });

// What was the weather like in London on January 1st 2008?
forecastIo.timeMachine('51.506', '-0.127', '2008-01-01T00:00:01Z', function(err, data) {
if (err) throw err;
forecastIo.timeMachine('51.506', '-0.127', '2008-01-01T00:00:01Z').then(function(data) {
console.log(JSON.stringify(data, null, 2));

@@ -58,4 +57,3 @@ });

};
forecastIo.forecast('49.844', '24.028', options, function(err, data) {
if (err) throw err;
forecastIo.forecast('49.844', '24.028', options).then(function(data) {
console.log(JSON.stringify(data, null, 2));

@@ -72,3 +70,3 @@ });

};
forecastIo.timeMachine('49.844', '24.028', '2008-01-01T00:00:01Z', options, function(err, data) {
forecastIo.timeMachine('49.844', '24.028', '2008-01-01T00:00:01Z', options).then(function(data) {
if (err) throw err;

@@ -106,2 +104,5 @@ console.log(JSON.stringify(data, null, 2));

#### 1.0.0
- Promises (For compatibility callbacks are still supported).
#### 0.2.0

@@ -108,0 +109,0 @@ - Added `timeout` option

@@ -14,16 +14,14 @@ var assert = require('assert');

describe('#forecast()', function() {
it('should return forecast for latitude and longitude', function(done) {
forecastIo.forecast('49.844', '24.028', function(err, data) {
if (err) throw err;
assert.ok(data);
assert.ok(data.currently);
assert.ok(data.hourly);
assert.ok(data.daily);
assert.ok(data.flags);
done();
});
it('should return forecast for latitude and longitude', function() {
return forecastIo.forecast('49.844', '24.028')
.then(function(data) {
assert.ok(data);
assert.ok(data.currently);
assert.ok(data.hourly);
assert.ok(data.daily);
assert.ok(data.flags);
})
});
it('should handle options if specified', function(done) {
it('should handle options if specified', function() {
var options = {

@@ -33,52 +31,68 @@ units: 'si',

};
forecastIo.forecast('49.844', '24.028', options, function(err, data) {
if (err) throw err;
return forecastIo.forecast('49.844', '24.028', options)
.then(function(data) {
assert.equal('si', data.flags.units);
assert.equal('si', data.flags.units);
assert.equal(undefined, data.currently);
assert.equal(undefined, data.hourly);
assert.equal(undefined, data.daily);
done();
});
assert.equal(undefined, data.currently);
assert.equal(undefined, data.hourly);
assert.equal(undefined, data.daily);
})
});
it('should return informative error objects', function(done) {
forecastIo.forecast('foo', 'bar', {foo: 'bar'}, function(err, data) {
assert.ok(err);
assert.ok(err.request);
assert.ok(err.response);
assert.ok(err.response.body);
assert.equal(400, err.response.statusCode);
done();
});
it('should return informative error objects', function() {
return forecastIo.forecast('foo', 'bar', {foo: 'bar'})
.then(function() {
assert.fail(1, 2, 'This request should have failed');
})
.catch(function(err) {
assert.ok(err.request);
assert.ok(err.response);
assert.ok(err.response.body);
assert.equal(400, err.response.statusCode);
})
});
it('should handle timeouts', function(done) {
it('should handle timeouts', function() {
var opts = {timeout: 1}; // Any request with timeout of 1 ms will surely fail
var instance = new ForecastIo(process.env.FORECASTIO_API_KEY, opts);
instance.forecast('49.844', '24.028', function(err, _) {
return instance.forecast('49.844', '24.028')
.then(function() {
assert.fail(1, 2, 'This request should have failed');
})
.catch(function(err) {
assert.ok(err);
assert.equal("Error: ETIMEDOUT", err.message);
})
})
it('should support optional callbacks for success cases', function(done) {
forecastIo.forecast('49.844', '24.028', function(err, data) {
if (err) return done(err);
assert.ok(data);
done()
})
})
it('should support oldschool callbacks for error cases', function(done) {
forecastIo.forecast('foo', 'bar', {foo: 'bar'}, function(err) {
assert.ok(err);
assert.equal("ETIMEDOUT", err.message);
done();
});
});
done()
})
})
})
describe('#timeMachine()', function() {
it('should return forecast for latitude and longitude', function(done) {
forecastIo.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z', function(err, data) {
if (err) throw err;
assert.ok(data);
assert.ok(data.currently);
assert.ok(data.hourly);
assert.ok(data.daily);
assert.ok(data.flags);
done();
});
it('should return forecast for latitude and longitude', function() {
return forecastIo.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z')
.then(function(data) {
assert.ok(data);
assert.ok(data.currently);
assert.ok(data.hourly);
assert.ok(data.daily);
assert.ok(data.flags);
})
});
it('should handle options if specified', function(done) {
it('should handle options if specified', function() {
var options = {

@@ -88,35 +102,52 @@ units: 'si',

};
forecastIo.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z', options, function(err, data) {
if (err) throw err;
return forecastIo.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z', options)
.then(function(data) {
assert.equal('si', data.flags.units);
assert.equal('si', data.flags.units);
assert.equal(undefined, data.currently);
assert.equal(undefined, data.daily);
done();
});
assert.equal(undefined, data.currently);
assert.equal(undefined, data.daily);
})
});
it('should return informative error objects', function(done) {
forecastIo.timeMachine('49.844', '24.028', 'blabla', {foo: 'bar'}, function(err, data) {
assert.ok(err);
assert.ok(err.request);
assert.ok(err.response);
assert.ok(err.response.body);
assert.equal(400, err.response.statusCode);
done();
});
it('should return informative error objects', function() {
return forecastIo.timeMachine('49.844', '24.028', 'blabla', {foo: 'bar'})
.then(function() {
assert.fail(1, 2, 'This request should have failed');
})
.catch(function(err) {
assert.ok(err.request);
assert.ok(err.response);
assert.ok(err.response.body);
assert.equal(400, err.response.statusCode);
})
});
it('should handle timeouts', function(done) {
it('should handle timeouts', function() {
var opts = {timeout: 1}; // Any request with timeout of 1 ms will surely fail
var instance = new ForecastIo(process.env.FORECASTIO_API_KEY, opts);
instance.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z', function(err, _) {
return instance.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z')
.then(function() {
assert.fail(1, 2, 'This request should have failed');
})
.catch(function(err) {
assert.equal("Error: ETIMEDOUT", err.message);
});
});
it('should support optional callbacks for success cases', function(done) {
forecastIo.timeMachine('49.844', '24.028', '2004-07-14T00:00:01Z', function(err, data) {
if (err) return done(err);
assert.ok(data);
done()
})
})
it('should support oldschool callbacks for error cases', function(done) {
forecastIo.timeMachine('foo', 'bar', 'foobar', function(err) {
assert.ok(err);
assert.equal("ETIMEDOUT", err.message);
done();
});
});
done()
})
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc