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

gosquared

Package Overview
Dependencies
Maintainers
3
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gosquared - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

config.js

178

lib/gosquared.js

@@ -1,52 +0,152 @@

var request = require('request');
var https = require('https'),
config = require('../config'),
h = require('./helpers'),
queryString = require('querystring'),
util = require('util'),
format = util.format
;
/*
See https://www.gosquared.com/developer for API documentation.
*/
var gosquared = function(defaults) {
this.opts = defaults;
this._setup();
var debugLevels = {
TRACE: 1,
NOTICE: 1<<1,
WARNING: 1<<2,
FATAL: 1<<3
};
gosquared.prototype._setup = function() {
var standardFunctions = ["aggregateStats","alertPreferences","campaigns","concurrents","engagement","events","expandUrl","fullDump","functions","geo","ignoredVisitors","notifications","organics","overview","pages","referrers","reportPreferences","sites","time","timeSeries","trends","visitors"];
for (var i = 0; i< standardFunctions.length; i++) {
gosquared.prototype[standardFunctions[i]] = this._defaultFunction.bind(this,standardFunctions[i]);
}
var messages = {
requestFailed: 'requestFailed',
responseEmpty: 'responseEmpty',
responseParseError: 'responseParseError',
responseInvalid: 'responseInvalid',
missingEventName: 'The event must have a name. No name was given.',
missingSiteToken: 'A site token must be given.'
};
gosquared.prototype._defaultFunction = function(fctn,opts,cb) {
if(typeof opts == 'function') {cb = opts; opts = {};}
opts = this._parse(opts,['site_token','api_key']);
console.log(opts);
this._get(fctn, opts, cb);
var errors = {
0: '',
1: messages.requestFailed,
2: messages.responseEmpty,
3: messages.responseParseError,
4: messages.responseInvalid,
5: messages.missingEventName,
6: messages.missingSiteToken,
7: messages.requestFailed
};
// helper function to set default parameters
gosquared.prototype._parse = function(opts,set,cb) {
for (var i = 0; i < set.length; i++) {
if (!opts[set[i]]) {
opts[set[i]] = this.opts[set[i]];
}
debugLevels.ALL = Math.pow(2, Object.keys(debugLevels).length) -1;
var GoSquared = module.exports = function(opts){
if(!opts.siteToken){
return this._debug(6);
}
return opts;
this.opts = h.extend(opts, {
requestTimeout: 2000,
version: 'latest',
debugLevel: 'FATAL'
});
this.config = config;
};
// actually gets the data
gosquared.prototype._get = function(fctn, params, cb) {
request({
url: 'https://api.gosquared.com/v2/'+fctn,
qs: params,
json: true
}, function(e,r,body) {
if (r.statusCode !== 200) {
// we've had an error, send the body back as the error
return cb(body);
}
cb(e,body);
GoSquared.prototype._exec = function(endpoint, path, params, cb){
var self = this;
var requestPath = path + '?' + queryString.stringify(params);
var requestOpts = {
host: endpoint.host,
port: endpoint.port,
method: endpoint.method,
path: requestPath
};
this._debug(0, 'TRACE', requestOpts);
var request = https.request(requestOpts, function(res){
h.bufferResponse(res, cb);
});
request.on('error', function(e){
self._debug(1, 'WARNING', e);
return cb(1);
});
request.end();
setTimeout(function(){
request.destroy();
}, this.opts.requestTimeout);
};
module.exports = function(opts) {
return new gosquared(opts);
};
GoSquared.prototype._validateResponse = function(responseData){
var err;
if(!responseData){
err = 2;
this._debug(err, 'WARNING');
return err;
}
var parsed = '';
try{
parsed = JSON.parse(responseData);
}
catch(e){
err = 3;
this._debug(err, 'WARNING', e);
return err;
}
if(!parsed){
err = 4;
this._debug(err, 'WARNING');
return err;
}
return parsed;
};
GoSquared.prototype._debug = function(code, level, extra){
var dLevel = this.opts.debugLevel || 'ALL';
if(!debugLevels[dLevel] & debugLevels[level]) return false;
var stream = console.log;
if(level > debugLevels['NOTICE']) stream = console.error;
// Errors are machine-parseable
stream(
format(
'[GoSquared][%s]:%s', level, JSON.stringify({message: errors[code], code: code, extra: extra})
)
);
};
GoSquared.prototype.storeEvent = function(name, params, done){
if(!name){
return this._debug(5, 'WARNING');
}
if(typeof params == 'function') done = params, params = {};
params._name = name;
params.a = this.opts.siteToken;
this._exec(this.config.endpoints.data, '/event', params, this._responseCompleted.bind(this, done));
};
GoSquared.prototype._responseCompleted = function(cb, err, responseData){
if(err){
this._debug(7, 'WARNING');
return cb(err);
}
var validated = this._validateResponse(responseData);
if(typeof validated != "object"){
return cb(validated);
}
cb(null, validated);
};
var standardFunctions = config.api.functions;
for (var i = 0; i< standardFunctions.length; i++) {
var func = standardFunctions[i];
GoSquared.prototype[func] = (function(func) {
return function(opts, cb){
if(typeof opts == 'function') {cb = opts; opts = {};}
var endpoint = this.config.endpoints.api;
opts.site_token = this.opts.siteToken;
opts.api_key = this.opts.apiKey;
this._exec(endpoint, '/'+this.opts.version+'/'+func, opts, this._responseCompleted.bind(this, cb));
};
})(func);
}
{
"name": "gosquared",
"version": "0.0.1",
"description": "Use the GoSquared API with ease in Node apps",
"version": "0.1.0",
"description": "GoSquared for your Node.JS application",
"main": "lib/gosquared.js",
"repository": "git@github.com:simontabor/node-gosquared.git",
"repository": "git@github.com:gosquared/node-gosquared.git",
"keywords": [

@@ -11,7 +11,19 @@ "gosquared",

],
"author": "Simon Tabor",
"license": "BSD",
"dependencies": {
"request": "~2.12.0"
"contributors": [
{
"name": "Geoff Wagstaff",
"email": "geoff@gosquared.com",
"url": "http://geoffwagstaff.com"
},
{
"name": "Simon Tabor",
"email": "simontabor@gosquared.com",
"url": "http://simontabor.com"
}
],
"license": "MIT",
"devDependencies": {
"mocha": "~1.8.1",
"should": "~1.2.1"
}
}
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