From b1d4636682919e3184f5a94b3415a84a888975d2 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 20 Oct 2015 17:19:58 -0300 Subject: [PATCH] fix request url when still no notificationId received --- lib/api.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/api.js b/lib/api.js index fd6fe96f..b59f1ee5 100644 --- a/lib/api.js +++ b/lib/api.js @@ -88,16 +88,15 @@ API.prototype._fetchLatestNotifications = function(interval, cb) { cb = cb || function() {}; - var url = '/v1/notifications/'; - if (self.lastNotificationId) { - url += '?notificationId=' + self.lastNotificationId; - } else { - url += '?timeSpan=' + (interval + 2); // Give a couple of seconds for network latency + var opts = { + lastNotificationId: self.lastNotificationId, + }; + + if (!self.lastNotificationId) { + opts.timeSpan = interval + 2; // Give a couple of seconds for network latency } - self.getNotifications({ - lastNotificationId: self.lastNotificationId - }, function(err, notifications) { + self.getNotifications(opts, function(err, notifications) { if (err) { log.warn('Error receiving notifications.'); log.debug(err); @@ -1060,14 +1059,21 @@ API.prototype._processCustomData = function(result) { * * @param {object} opts * @param {String} lastNotificationId (optional) - The ID of the last received notification + * @param {String} timeSpan (optional) - A time window on which to look for notifications (in seconds) * @returns {Callback} cb - Returns error or an array of notifications */ API.prototype.getNotifications = function(opts, cb) { $.checkState(this.credentials); var self = this; + opts = opts || {}; + var url = '/v1/notifications/'; - if (opts.lastNotificationId) url += '?notificationId=' + opts.lastNotificationId; + if (opts.lastNotificationId) { + url += '?notificationId=' + opts.lastNotificationId; + } else if (opts.timeSpan) { + url += '?timeSpan=' + opts.timeSpan; + } self._doGetRequest(url, function(err, result) { if (err) return cb(err);