diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 1837cef..0000000 --- a/.editorconfig +++ /dev/null @@ -1,17 +0,0 @@ -root = true - -[*] -end_of_line = lf -insert_final_newline = true - -[*.yml] -indent_style = space -indent_size = 2 - -[*.json] -indent_style = space -indent_size = 2 - -[*.{js,css,less,coffee}] -indent_style = space -indent_size = 2 diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2bd34f0..0000000 --- a/.gitignore +++ /dev/null @@ -1,29 +0,0 @@ -.idea/ - -# Logs -logs -*.log - -# Runtime data -pids -*.pid -*.seed - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (http://nodejs.org/api/addons.html) -build/Release - -# Dependency directory -# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git -node_modules diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index bfab963..0000000 --- a/.travis.yml +++ /dev/null @@ -1,4 +0,0 @@ -sudo: false -language: node_js -node_js: - - "0.12" diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index b9166d6..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,36 +0,0 @@ -# Change Log -Following guidelines of http://keepachangelog.com/ - -## [Unreleased] - -## [2.0.2] - Oct 8, 2017 -- Fix error on JSON request with certain unicode chars #6 - -## [2.0.1] - Aug 19, 2016 -- update README -- bump version - -## [2.0.0] - Aug 19, 2016 -- implement `GET /metrickeys` -- update `GET /lastpushes` -- implement `GET /lastpushes/{id}` to return specific push -- implement `DELETE /data` -- update `user-agent` and `Accept` header - -## [0.2.3] - Jan 25, 2016 -- [Databox](https://databox.com) API now uses `GET /lastpushes` for retrieval of last pushes. -- Fixes for broken test suite -- `.editorconfig` was added -- [JSHint](http://jshint.com/) errors were fixed - -## [0.2.1] - Jul 22, 2015 -- Support for additional KPI attributes -- Updated test suite for additional KPI attributes support -- Updated example for additional KPI attributes - -[Unreleased]: https://github.com/databox/databox-js/compare/2.0.2...master -[2.0.2]: https://github.com/databox/databox-js/compare/2.0.1...2.0.2 -[2.0.1]: https://github.com/databox/databox-js/compare/2.0.0...2.0.1 -[2.0.0]: https://github.com/databox/databox-js/compare/0.2.3...2.0.0 -[0.2.3]: https://github.com/databox/databox-js/compare/0.2.1...0.2.3 -[0.2.1]: https://github.com/databox/databox-js/tree/0.2.1 diff --git a/example.js b/example.js deleted file mode 100755 index 83aa0f7..0000000 --- a/example.js +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env node - -var Databox = require('./lib/databox'); - -var client = new Databox({ - 'push_token': (process.env.DATABOX_PUSH_TOKEN || 'adxg1kq5a4g04k0wk0s4wkssow8osw84'), -}); - -client.push({ - key: 'js.prices.gas', - value: 322, - date: '2015-06-23 09:00:00' -}, function (result) { - console.log(result); -}); - -client.push({ - key: 'js.prices.test.gas', - value: 322 -}); - -client.push({ - key: 'js.prices.test.gas', - value: 124, - unit: 'USD', - attributes: { - 'station': 'omv' - } -}); - -client.insertAll([ - { - key: 'js.prices.gas', - value: 322 - }, - { - key: 'js.prices.gas', - value: 100, - date: '2015-06-23 09:00:00' - } -], function (result) { - console.log(result); -}); - -client.lastPush(function (pushes) { - var sha = pushes[0].response.body.id; - - console.log('Last push id: ' + sha); - client.getPush(sha, function (onePush) { - console.log('#getPush response:', JSON.stringify(onePush, null, 2)); - }); -}); - -client.lastPushes(2, function (pushes) { - var sha = []; - for (var i in pushes) { - sha.push(pushes[i].response.body.id); - } - - console.log('Last two pushes:', sha); - client.getPush(sha, function (multiplePushes) { - console.log('#getPush response:', JSON.stringify(multiplePushes, null, 2)); - }); -}); - -client.metrics(function (res) { - console.log('#metrics response', JSON.stringify(res, null, 2)); -}); - -client.purge(function (res) { - console.log('#purge response', JSON.stringify(res, null, 2)); -}); diff --git a/lib/databox.js b/lib/databox.js deleted file mode 100644 index 710db6f..0000000 --- a/lib/databox.js +++ /dev/null @@ -1,162 +0,0 @@ -var https = require('https'), - url = require('url'), - util = require('util'), - package_json = require('../package.json'); - -function Databox(config) { - this.config = config || {}; - - if (config.hasOwnProperty('push_token')) - this.push_token = config.push_token; - - this.push_token = process.env.DATABOX_PUSH_TOKEN || this.push_token; - - if (typeof(this.push_token) == "undefined") - throw new Error("Missing push_token."); - - this.config.push_host = 'https://push.databox.com/'; - this.config.user_agent = util.format('databox-js/%s', package_json.version); - this.config.accept = util.format('application/vnd.databox.v%s+json', package_json.version.split('.')[0]); -} - -Databox.prototype._pushJSONRequest = function (i_options, data, cb) { - if (typeof(i_options) == "undefined") i_options = {}; - - var options = { - hostname: url.parse(this.config.push_host).hostname, - port: url.parse(this.config.push_host).port || 443, - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'User-Agent': this.config.user_agent, - 'Accept': this.config.accept - }, - auth: util.format("%s:%s", this.config.push_token, '') - }; - - for (var k in i_options) { - if (i_options.hasOwnProperty(k)) options[k] = i_options[k]; - } - - var jsonData = null; - if (data !== null) { - jsonData = JSON.stringify({data: data}); - options.headers['Content-Length'] = jsonData.length; - } - - var request = https - .request(options, function (response) { - var bf = ''; - response.on('data', function (data) { - bf += data; - if (bf.length > 1e6) request.connection.destroy(); - }); - - response.on('end', function (data) { - if (typeof(cb) != "undefined") { - - try { - outObject = JSON.parse(bf); - cb(outObject); - } catch (e) { - var p = 1; - cb(e); - } - - outObject = null; - bf = null; - } - }); - }); - - if (data !== null) request.write(jsonData); - request.end(); - - return request; -}; - -Databox.prototype._processKPI = function (kpi) { - var out = {}; - - out[util.format("$%s", kpi.key)] = kpi.value; - - if (kpi.hasOwnProperty("date") || typeof(kpi.date) != "undefined") { - out.date = kpi.date; - //TODO: If it's some kind of Date/Time instance do "casting". - } - - if (kpi.hasOwnProperty("unit") || typeof(kpi.unit) != "undefined") { - out.unit = kpi.unit; - } - - if (kpi.hasOwnProperty('attributes') || typeof(kpi.attributes) != "undefined") { - for (var k in kpi.attributes) - // When the attribute value contain any extended UNICODE character, - // produces the return of the following server Error: - // {message : "Invalid request body - JSON parse error", type : "invalid_json"} - // - // The proposed patch, converts each string char to its (Hex) explicit Unicode - // representation. An example transformation, using a Spanish char with acent: - // "รณ" => "\u00f3" - // This disambiguation prevents the unexpected error on the server side. - // Original Code Line to Patch: - // out[k] = kpi.attributes[k]; - // <-- BEGINING OF THE PATCH - out[k] = String( kpi.attributes[k]).replace(/[\u007F-\uFFFF]/g, function(chr) { - return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).substr(-4); - }); - // END OF THE PATCH --> - } - - return out; -}; - -Databox.prototype.getPush = function (sha, cb) { - var path = (sha instanceof Array) ? '/lastpushes?id=' + sha.join(',') : '/lastpushes/' + sha; - - return this._pushJSONRequest({ - method: 'GET', - path: path - }, null, cb); -}; - -Databox.prototype.lastPushes = function (n, cb) { - return this._pushJSONRequest({ - method: 'GET', - path: util.format("/lastpushes?limit=%d", parseInt(n, 10)) - }, null, cb); -}; - -Databox.prototype.lastPush = function (cb) { - return this.lastPushes(1, cb); -}; - -Databox.prototype.push = function (kpi, cb) { - return this._pushJSONRequest('/', - [kpi].map(this._processKPI), - cb - ); -}; - -Databox.prototype.insertAll = function (kpis, cb) { - return this._pushJSONRequest('/', - kpis.map(this._processKPI), - cb - ); -}; - -Databox.prototype.metrics = function (cb) { - return this._pushJSONRequest({ - method: 'GET', - path: '/metrickeys' - }, null, cb); -}; - -Databox.prototype.purge = function (cb) { - return this._pushJSONRequest({ - method: 'DELETE', - path: '/data' - }, null, cb); -}; - -module.exports = Databox; diff --git a/package.json b/package.json deleted file mode 100644 index 110f05b..0000000 --- a/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "databox", - "version": "2.0.1", - "description": "Node.js bindings for Databox - Mobile Executive Dashboard.", - "author": { - "name": "Oto Brglez", - "email": "otobrglez@gmail.com" - }, - "scripts": { - "test": "mocha", - "pretest": "jshint lib test *.js" - }, - "readmeFilename": "README.md", - "devDependencies": { - "mocha": "*", - "jshint": "*" - }, - "repository": { - "type": "git", - "url": "https://github.com/databox/databox-js.git" - }, - "bugs": { - "url": "https://github.com/databox/databox-js/issues" - }, - "main": "./lib/databox" -} diff --git a/test/databox.test.js b/test/databox.test.js deleted file mode 100644 index 5b5b45a..0000000 --- a/test/databox.test.js +++ /dev/null @@ -1,136 +0,0 @@ -var path = require('path'), - assert = require('assert'), - util = require('util'), - Databox = require(path.join('../lib/', 'databox.js')); - -describe('Databox', function () { - - describe('Setup', function () { - it('Should set push_token', function () { - var push_token = 123; - var d = new Databox({ - 'push_token': push_token - }); - - assert.equal(d.push_token, push_token); - }); - - it('Should cry if no \'push_token\'', function () { - assert.throws(function () { - new Databox(); - }, Error); - - assert.throws(function () { - new Databox({}); - }, Error); - }); - }); - - describe('Config', function () { - var databox, config; - beforeEach(function () { - databox = new Databox({ - 'push_token': "some_token" - }); - config = databox.config; - }); - - it('Should have valid user_agent', function () { - assert(config.user_agent.match(/^databox\-js\/\d+\.\d+\.\d+/i) !== null); - }); - - it('Should have push_host', function () { - assert(config.push_host.match(/databox/) !== null); - }); - - it('Should have valid accept', function () { - assert(config.accept.match(/^application\/vnd\.databox\.v\d\+json/) !== null); - }); - }); - - describe('Pushing', function () { - var databox; - before(function () { - databox = new Databox({ - 'push_token': 'token' - }); - }); - - it('Should push KPI w/ #push', function (done) { - var key = 'me.key'; - var value = 299; - - Databox.prototype._pushJSONRequest = function (options, data, cb) { - var i_value = data[0][util.format('$%s', key)]; - assert.equal(i_value, value); - done(); - }; - - databox.push({ - key: key, - value: value - }); - }); - - it('Should push KPI w/ #push and additional attributes', function (done) { - var key = 'test'; - var value = 300; - - Databox.prototype._pushJSONRequest = function (options, data, cb) { - var i_value = data[0][util.format('$%s', key)]; - assert.equal(i_value, value); - assert.equal(data[0].me, 'Oto'); - done(); - }; - - databox.push({ - key: key, - value: value, - attributes: { - 'me': 'Oto' - } - }); - - }); - - it('Should push KPI w/ #push & callback', function (done) { - var key = 'me.key'; - var value = 299; - - Databox.prototype._pushJSONRequest = function (options, data, cb) { - cb([{status: 'ok'}]); - }; - - databox.push({ - key: key, - value: value - }, function (data) { - assert(data !== null); - done(); - }); - }); - - it('Should push KPIs w/ #insertAll', function (done) { - var key = 'me.key'; - var value = 299; - - Databox.prototype._pushJSONRequest = function (options, data, cb) { - var i_value = data[0][util.format('$%s', key)]; - assert.equal(i_value, value); - assert(data.length, 2); - done(); - }; - - databox.insertAll([ - { - key: key, - value: value - }, { - key: key, - value: value, - date: '2015-01-01 12:00:00' - } - ]); - }); - }); -});