Comparing version 0.0.1 to 0.1.0
@@ -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" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8534
0
10
0
238
0
2
5
3
- Removedrequest@~2.12.0
- Removedrequest@2.12.0(transitive)