meerkat-api
Advanced tools
Comparing version 1.0.2 to 1.0.3
151
meerkat.js
var https = require('https'); | ||
var url = require('url'); | ||
// Detect whether we have an implementation of Promise. | ||
var PROMISES_IMPLEMENTED = (typeof Promise === 'function'); | ||
/** | ||
@@ -69,12 +72,13 @@ * A client wrapper for the Meerkat API. | ||
* | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getAllBroadcasts = function (callback) { | ||
Meerkat.prototype.getAllBroadcasts = function (opt_callback) { | ||
var targetUrl = Meerkat.RoutingMap.liveNow; | ||
this.issueGetRequest_('ALL_BROADCASTS', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('ALL_BROADCASTS', targetUrl, opt_callback); | ||
}; | ||
@@ -86,12 +90,13 @@ | ||
* | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getScheduledBroadcasts = function (callback) { | ||
Meerkat.prototype.getScheduledBroadcasts = function (opt_callback) { | ||
var targetUrl = Meerkat.RoutingMap.scheduledStreams; | ||
this.issueGetRequest_('SCHEDULED_BROADCASTS', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('SCHEDULED_BROADCASTS', targetUrl, opt_callback); | ||
}; | ||
@@ -104,6 +109,9 @@ | ||
* @param {string} broadcastId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getBroadcastSummary = function (broadcastId, callback) { | ||
Meerkat.prototype.getBroadcastSummary = function (broadcastId, opt_callback) { | ||
@@ -113,5 +121,3 @@ var urlTemplate = Meerkat.RoutingMap.streamSummaryTemplate; | ||
this.issueGetRequest_('BROADCAST_SUMMARY', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('BROADCAST_SUMMARY', targetUrl, opt_callback); | ||
}; | ||
@@ -124,6 +130,9 @@ | ||
* @param {string} broadcastId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getBroadcastActivities = function (broadcastId, callback) { | ||
Meerkat.prototype.getBroadcastActivities = function (broadcastId, opt_callback) { | ||
@@ -133,5 +142,3 @@ var urlTemplate = Meerkat.RoutingMap.broadcastActivities; | ||
this.issueGetRequest_('BROADCAST_ACTIVITIES', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('BROADCAST_ACTIVITIES', targetUrl, opt_callback); | ||
}; | ||
@@ -144,6 +151,9 @@ | ||
* @param {string} broadcastId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getBroadcastRestreams = function (broadcastId, callback) { | ||
Meerkat.prototype.getBroadcastRestreams = function (broadcastId, opt_callback) { | ||
@@ -153,5 +163,3 @@ var urlTemplate = Meerkat.RoutingMap.broadcastRestreams; | ||
this.issueGetRequest_('BROADCAST_RESTREAMS', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('BROADCAST_RESTREAMS', targetUrl, opt_callback); | ||
}; | ||
@@ -164,6 +172,9 @@ | ||
* @param {string} broadcastId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getBroadcastComments = function (broadcastId, callback) { | ||
Meerkat.prototype.getBroadcastComments = function (broadcastId, opt_callback) { | ||
@@ -173,5 +184,3 @@ var urlTemplate = Meerkat.RoutingMap.broadcastComments; | ||
this.issueGetRequest_('BROADCAST_COMMENTS', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('BROADCAST_COMMENTS', targetUrl, opt_callback); | ||
}; | ||
@@ -184,6 +193,9 @@ | ||
* @param {string} broadcastId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getBroadcastLikes = function (broadcastId, callback) { | ||
Meerkat.prototype.getBroadcastLikes = function (broadcastId, opt_callback) { | ||
@@ -193,5 +205,3 @@ var urlTemplate = Meerkat.RoutingMap.broadcastLikes; | ||
this.issueGetRequest_('BROADCAST_LIKES', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('BROADCAST_LIKES', targetUrl, opt_callback); | ||
}; | ||
@@ -204,6 +214,9 @@ | ||
* @param {string} broadcastId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getBroadcastWatchers = function (broadcastId, callback) { | ||
Meerkat.prototype.getBroadcastWatchers = function (broadcastId, opt_callback) { | ||
@@ -213,5 +226,3 @@ var urlTemplate = Meerkat.RoutingMap.broadcastWatchers; | ||
this.issueGetRequest_('BROADCAST_WATCHERS', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('BROADCAST_WATCHERS', targetUrl, opt_callback); | ||
}; | ||
@@ -224,6 +235,9 @@ | ||
* @param {string} userId | ||
* @param {!function(Error, Object)} callback A function that will be called when the operation | ||
* @param {!function(Error, Object)=} opt_callback A function that will be called when the operation | ||
* completes. | ||
* @return {undefined|!Promise<!Object,!Error>} If a callback is supplied, there is no return value. | ||
* If no callback is supplied, a promise is returned. The promise is resolved with the object | ||
* returned by the API, or is rejected with an error. | ||
*/ | ||
Meerkat.prototype.getUserDetails = function (userId, callback) { | ||
Meerkat.prototype.getUserDetails = function (userId, opt_callback) { | ||
@@ -233,5 +247,3 @@ var urlTemplate = Meerkat.RoutingMap.profile; | ||
this.issueGetRequest_('USER_DETAILS', targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
return this.doGetRequest_('USER_DETAILS', targetUrl, opt_callback); | ||
}; | ||
@@ -425,2 +437,51 @@ | ||
/** | ||
* Make the request in the appropriate manner, depending on whether a callback is supplied, or a | ||
* promise is required as a return value. | ||
* | ||
* @param {string} endpointName The name of the endpoint. Used in log messages. | ||
* @param {string} targetUrl The url of the endpoint, with any variables already baked in. | ||
* @param {function(Error, Object)=} callback A function that will be called when the operation | ||
* completes. If this is supplied, a promise will NOT be returned. | ||
* @return {undefined|!Promise<(!Object|string),!Error>} If a callback is supplied, there is no | ||
* return value. If no callback is supplied, the return value is a promise that resolves to the | ||
* response, or is rejected with an error. | ||
* @private | ||
*/ | ||
Meerkat.prototype.doGetRequest_ = function (endpointName, targetUrl, callback) { | ||
if (!callback && PROMISES_IMPLEMENTED) { | ||
return this.promiseGetRequest_(endpointName, targetUrl); | ||
} else { | ||
if (!callback) { | ||
throw new Error('Missing callback argument.' + | ||
'Promises are not available on this platform, so a callback is required'); | ||
} | ||
this.issueGetRequest_(endpointName, targetUrl, function (err, response, data) { | ||
callback.call(null, err, data); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Wrap issueGetRequest_ in a promise. | ||
* | ||
* @param {string} endpointName The name of the endpoint. Used in log messages. | ||
* @param {string} targetUrl The url of the endpoint, with any variables already baked in. | ||
* @return {!Promise<(!Object|string),!Error>} A promise that resolves to the response, or is | ||
* rejected with an error. | ||
* @private | ||
*/ | ||
Meerkat.prototype.promiseGetRequest_ = function (endpointName, targetUrl) { | ||
var self = this; | ||
return new Promise(function(resolve, reject) { | ||
self.issueGetRequest_(endpointName, targetUrl, function (err, response, data) { | ||
!err ? resolve(data) : reject(err); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* In order to keep the list of endpoints up to date, we have to check periodically with Meerkat. | ||
@@ -427,0 +488,0 @@ * Schedules a call for a specified number of milliseconds in the future. |
{ | ||
"name": "meerkat-api", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Node.js wrapper for Meerkat API", | ||
@@ -5,0 +5,0 @@ "main": "meerkat.js", |
116
README.md
@@ -10,6 +10,12 @@ # Node.js Wrapper for Meerkat API | ||
## Notes | ||
## Features | ||
* Periodically queries Meerkat's Routing Map and modifies the API endpoints accordingly. | ||
* You can apply for an API token here: http://developers.meerkatapp.co/api/ | ||
* There are two ways to use it: | ||
1. **Callbacks**: All methods take an optional callback function. The callback is called with | ||
two arguments: an error (or null if the operation succeeded) and an object containing the | ||
API data. | ||
2. **Promises**: If you don't supply a callback function, a Promise will be returned. If the | ||
operation completes successfully, the promise is resolved with the returned API data. If | ||
the operation fails, the promise is rejected with an error. | ||
* Periodically queries Meerkat's Routing Map and modifies the API endpoints if they change. | ||
@@ -23,22 +29,9 @@ ## Usage | ||
var meerkat = new Meerkat(API_TOKEN); | ||
// You can get an API token here: http://developers.meerkatapp.co/api/ | ||
/** | ||
* Clean up when exiting. | ||
* General requests. | ||
*/ | ||
process.on('SIGINT', shutdown); | ||
process.on('SIGTERM', shutdown); | ||
function shutdown () { | ||
// Don't accept any more requests. Finish handling the current requests. | ||
server.close(function () { | ||
meerkat.dispose(); | ||
}); | ||
} | ||
/** | ||
* General requests. | ||
*/ | ||
// Callback style: | ||
meerkat.getAllBroadcasts(function (err, broadcasts) { | ||
@@ -48,2 +41,11 @@ console.log(err || broadcasts); | ||
// Promise style: | ||
meerkat.getAllBroadcasts(). | ||
then(function (broadcasts) { | ||
console.log(broadcasts); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
// Callback style: | ||
meerkat.getScheduledBroadcasts(function (err, broadcasts) { | ||
@@ -53,3 +55,11 @@ console.log(err || broadcasts); | ||
// Promise style: | ||
meerkat.getScheduledBroadcasts(). | ||
then(function (broadcasts) { | ||
console.log(broadcasts); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
/** | ||
@@ -60,6 +70,13 @@ * Broadcast-specific requests. | ||
meerkat.getBroadcastSummary(broadcastId, function (err, broadcasts) { | ||
console.log(err || broadcasts); | ||
meerkat.getBroadcastSummary(broadcastId, function (err, summary) { | ||
console.log(err || summary); | ||
}); | ||
meerkat.getBroadcastSummary(broadcastId). | ||
then(function (summary) { | ||
console.log(summary); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
meerkat.getBroadcastActivities(broadcastId, function (err, activities) { | ||
@@ -69,2 +86,9 @@ console.log(err || activities); | ||
meerkat.getBroadcastActivities(broadcastId). | ||
then(function (activities) { | ||
console.log(activities); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
meerkat.getBroadcastRestreams(broadcastId, function (err, restreams) { | ||
@@ -74,2 +98,9 @@ console.log(err || restreams); | ||
meerkat.getBroadcastRestreams(broadcastId). | ||
then(function (restreams) { | ||
console.log(restreams); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
meerkat.getBroadcastComments(broadcastId, function (err, comments) { | ||
@@ -79,2 +110,9 @@ console.log(err || comments); | ||
meerkat.getBroadcastComments(broadcastId). | ||
then(function (comments) { | ||
console.log(comments); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
meerkat.getBroadcastLikes(broadcastId, function (err, likes) { | ||
@@ -84,2 +122,9 @@ console.log(err || likes); | ||
meerkat.getBroadcastLikes(broadcastId). | ||
then(function (likes) { | ||
console.log(likes); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
meerkat.getBroadcastWatchers(broadcastId, function (err, watchers) { | ||
@@ -89,3 +134,10 @@ console.log(err || watchers); | ||
meerkat.getBroadcastWatchers(broadcastId). | ||
then(function (watchers) { | ||
console.log(watchers); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
/** | ||
@@ -99,2 +151,24 @@ * User-specific requests. | ||
}); | ||
meerkat.getUserDetails(userId). | ||
then(function (userDetails) { | ||
console.log(userDetails); | ||
}, function (err) { | ||
console.log(err); | ||
}); | ||
/** | ||
* Don't forget to clean up when exiting. | ||
*/ | ||
process.on('SIGINT', shutdown); | ||
process.on('SIGTERM', shutdown); | ||
function shutdown () { | ||
// Don't accept any more requests. Finish handling the current requests. | ||
server.close(function () { | ||
meerkat.dispose(); | ||
}); | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25268
488
164