diff --git a/lib/index.js b/lib/index.js index 38e0124..2a483aa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,9 +11,10 @@ exports['Librato'] = require('./librato'); exports['Lytics'] = require('./lytics'); exports['Mixpanel'] = require('./mixpanel'); exports['Outbound'] = require('./outbound'); -exports['Preact'] = require('./preact'); +exports['Preact'] = require('./preact'); +exports['Snappy'] = require('./snappy'); exports['trak.io'] = require('./trakio'); exports['USERcycle'] = require('./usercycle'); exports['Vero'] = require('./vero'); exports['Webhooks'] = require('./webhooks'); -exports['Woopra'] = require('./woopra'); \ No newline at end of file +exports['Woopra'] = require('./woopra'); diff --git a/lib/snappy/index.js b/lib/snappy/index.js new file mode 100644 index 0000000..545dca7 --- /dev/null +++ b/lib/snappy/index.js @@ -0,0 +1,90 @@ + +var debug = require('debug')('segmentio:integrations:snappy') + , Integration = require('segmentio-integration') + , request = require('request-retry')({ retries : 2 }) + , unixTime = require('unix-time') + , util = require('util'); + + +module.exports = Snappy; + + +function Snappy () { + this.name = 'Snappy'; + this.baseUrl = 'http://localhost:3000/v1'; +} + + +util.inherits(Snappy, Integration); + + +/** + * Check whether the integration is enabled + */ + +Snappy.prototype.enabled = function (message, settings) { + return Integration.enabled.apply(this, arguments) && + message.channel() === 'server'; +}; + + +/** + * Validate the settings for the project + */ + +Snappy.prototype.validate = function (message, settings) { + return this._missingSetting(settings, 'appToken'); +}; + + +/** + * Create or Update a Snappy user + */ +Snappy.prototype.identify = function (identify, settings, callback) { + + var userid = identify.userId() || identify.sessionId(); + + var request_body = { + email: identify.email(), + token: settings.appToken + } + + if (identify.created()) { + request_body['observed_at'] = unixTime(identify.created()); + } + request['traits'] = identify.traits(); + + var req = { + url : this.baseUrl + "/users/" + userid, + json : request_body + }; + + debug('making create user request'); + request.post(req, this._handleResponse(callback)); +}; + + +/** + * Track a Snappy event + */ +Snappy.prototype.track = function (track, settings, callback) { + + var userid = track.userId() || track.sessionId() + , properties = track.properties(); + + var request_body = { + userid: userid, + token: settings.appToken, + name: track.event(), + properties: properties, + obaserved_at: unixTime(track.timestamp()) + }; + + var req = { + url: this.baseUrl + '/events', + json: request_body + }; + + debug('making track request'); + request.post(req, this._handleResponse(callback)); +}; diff --git a/test/test.snappy.js b/test/test.snappy.js new file mode 100644 index 0000000..874f007 --- /dev/null +++ b/test/test.snappy.js @@ -0,0 +1,76 @@ + +var express = require('express') + , facade = require('segmentio-facade') + , helpers = require('./helpers') + , integrations = require('..') + , should = require('should') + , settings = require('./auth.json')['Snappy'] + , cio = new integrations['Snappy'](); + + +var app = express().use(express.bodyParser()) + , server; + + +describe('Snappy', function () { + + before(function (done) { server = app.listen(4000, done); }); + after(function(done) { server.close(done); }); + + describe('.enabled()', function () { + + it('should only be enabled for server side messages', function () { + cio.enabled(new facade.Alias({ channel : 'server' })).should.be.ok; + cio.enabled(new facade.Alias({ channel : 'client' })).should.not.be.ok; + cio.enabled(new facade.Alias({})).should.not.be.ok; + }); + }); + + + describe('.validate()', function () { + + it('should require an appToken', function () { + cio.validate({}, {}).should.be.an.instanceOf(Error); + cio.validate({}, {appToken : '' }).should.be.an.instanceOf(Error); + }); + + it('should validate with the required settings', function () { + should.not.exist(cio.validate({}, { appToken : 'xxx' })); + }); + }); + + + describe('.track()', function () { + + it('should get a good response from the API', function (done) { + var track = helpers.track(); + cio.track(track, settings, done); + }); + + it('will error on an invalid set of keys', function (done) { + var track = helpers.track(); + cio.track(track, { appToken : 'x' }, function (err) { + should.exist(err); + err.status.should.eql(401); + done(); + }); + }); + }); + + describe('.identify()', function () { + + it('should get a good response from the API', function (done) { + var identify = helpers.identify(); + cio.identify(identify, settings, done); + }); + + it('will error on an invalid set of keys', function (done) { + var identify = helpers.identify(); + cio.identify(identify, { appToken : 'x' }, function (err) { + should.exist(err); + err.status.should.eql(401); + done(); + }); + }); + }); +});