Skip to content

Commit

Permalink
Ensuring compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
VladRomero committed May 30, 2017
1 parent 09f8e0c commit 56986c9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 46 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/misc.xml
.idea/workspace.xml
node_modules/
coverage/
70 changes: 70 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "barracks-messenger-sdk",
"version": "2.0.2",
"build": 0,
"description": "Barracks SDK node module",
"main": "src/index.js",
"scripts": {
"lint": "jshint **/**.js",
"test": "npm run coverage && npm run check-coverage",
"coverage": "DEBUG=true istanbul cover ./node_modules/mocha/bin/_mocha tests/*_test.js -- -R spec",
"check-coverage": "istanbul check-coverage --statement 0 --branch 0 --function 0",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls"
},
"contributors": [
{
"email": "[email protected]",
"name": "Brice Argenson"
},
{
"email": "[email protected]",
"name": "Grégoire Weber"
},
{
"email": "[email protected]",
"name": "Paul Aigeperse"
},
{
"email": "[email protected]",
"name": "Pierre-Olivier Dybman"
},
{
"email": "[email protected]",
"name": "Simon Guerout"
}
],
"license": "Apache-2.0",
"devDependencies": {
"chai": "1.10.0",
"chai-fs": "0.1.0",
"chai-http": "1.0.0",
"coveralls": "2.11.15",
"debug": "2.2.0",
"grunt": "0.4.5",
"grunt-contrib-jshint": "0.11.3",
"grunt-contrib-watch": "0.6.1",
"grunt-mocha-test": "0.12.7",
"istanbul": "0.4.5",
"jshint": "2.9.4",
"mocha": "2.3.4",
"mqtt": "2.8.0",
"nock": "8.0.0",
"proxyquire": "1.7.10",
"request-debug": "0.2.0",
"sinon": "^2.1.0",
"sinon-chai": "^2.9.0"
},
"dependencies": {
"es6-promise": "4.0.5",
"md5-file": "3.1.1",
"request": "2.74.0",
"uuid": "^3.0.1"
},
"engines": {
"node": ">=0.10 <7.0"
},
"repository": {
"type": "git",
"url": "https://github.com/barracksiot/javascript-client"
}
}
42 changes: 20 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
'use strict';

// var ERRORS to be declared
const ERROR_REQUEST_FAILED = 'REQUEST_FAILED';
const ERROR_UNEXPECTED_SERVER_RESPONSE = 'UNEXPECTED_SERVER_RESPONSE';
const ERROR_MISSING_MANDATORY_ARGUMENT = 'MISSING_MANDATORY_ARGUMENT';
var ERROR_REQUEST_FAILED = 'REQUEST_FAILED';
var ERROR_UNEXPECTED_SERVER_RESPONSE = 'UNEXPECTED_SERVER_RESPONSE';
var ERROR_MISSING_MANDATORY_ARGUMENT = 'MISSING_MANDATORY_ARGUMENT';

const DEFAULT_BARRACKS_BASE_URL = 'https://app.barracks.io';
const DEFAULT_BARRACKS_MQTT_ENDPOINT = 'mqtt://app.barracks.io';
var DEFAULT_BARRACKS_BASE_URL = 'https://app.barracks.io';
var DEFAULT_BARRACKS_MQTT_ENDPOINT = 'mqtt://app.barracks.io';

require('es6-promise').polyfill();
const responseBuilder = require('./responseBuilder');
const fs = require('fs');
const request = require('request');
const fileHelper = require('./fileHelper');
const mqtt = require('mqtt');
//const uuid = require('uuid/v1');
var fs = require('fs');
var request = require('request');
var mqtt = require('mqtt');
var uuid = require('uuid/v1');


// package.json is empty for now
Expand All @@ -30,30 +28,30 @@ function Barracks(options) {
}

Barracks.prototype.listenMessages = function (apiKey, unitId, timeout) {
return new Promise((resolve, reject) => {
const mqttEndpoint = DEFAULT_BARRACKS_MQTT_ENDPOINT;
const client = mqtt.connect(mqttEndpoint, {
clientId: `${apiKey}.${unitId}`,
return new Promise(function (resolve, reject){
var mqttEndpoint = DEFAULT_BARRACKS_MQTT_ENDPOINT;
var client = mqtt.connect(mqttEndpoint, {
clientId: apiKey + '.' + unitId,
clean: false
});

client.on('connect', () => {
client.on('connect', function() {
console.log('Connected to ' + mqttEndpoint);
client.subscribe(`${apiKey}.${unitId}`, { qos: 2 });
client.subscribe(apiKey + '.' + unitId, { qos: 2 });
});

client.on('message', (topic, message, packet) => {
client.on('message', function(topic, message, packet) {
console.log('Received: ' + message.toString() + ' [retain=' + packet.retain + ']');
});

client.on('error', (error) => {
logger.error(error);
client.on('error', function(error) {
console.log(error);
client.end();
reject('Connection error:' + error);
});

client.on('close', () => {
logger.debug('Connection closed');
client.on('close', function() {
console.log('Connection closed');
resolve();
});

Expand Down
50 changes: 26 additions & 24 deletions tests/index_test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/* jshint expr: true, maxstatements: 100 */
/* global describe, it, beforeEach */

const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chai = require('chai');
const expect = chai.expect;
const proxyquire = require('proxyquire');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var chai = require('chai');
var expect = chai.expect;
var proxyquire = require('proxyquire');

chai.use(sinonChai);

const UNIT_ID = 'unit1';
const API_KEY = 'validKey';
const CUSTOM_CLIENT_DATA = {
var UNIT_ID = 'unit1';
var API_KEY = 'validKey';
var CUSTOM_CLIENT_DATA = {
aKey: 'aValue',
anotherKey: true
};

describe('Constructor : ', function () {

let Barracks;
var Barracks = require('../src/index.js');

beforeEach(function () {
beforeEach(function() {
Barracks = require('../src/index.js');
});

Expand All @@ -34,58 +34,58 @@ describe('Constructor : ', function () {
});
}

it('Should return the Barracks object with default values when minimum options given', function () {
it('Should return the Barracks object with default values when minimum options given', function() {
// Given
const options = {
var options = {
apiKey: API_KEY
};

// When
const barracks = new Barracks(options);
var barracks = new Barracks(options);

// Then
validateBarracksObject(barracks, 'https://app.barracks.io');
});

it('Should return the Barracks object with baseUrl overriden when url option given', function () {
it('Should return the Barracks object with baseUrl overriden when url option given', function() {
// Given
const url = 'not.barracks.io';
const options = {
var url = 'not.barracks.io';
var options = {
apiKey: API_KEY,
baseURL: url
};

// When
const barracks = new Barracks(options);
var barracks = new Barracks(options);

// Then
validateBarracksObject(barracks, url);
});

it('Should return the Barracks object that do not accept self signed cert when option given with invalid value', function () {
it('Should return the Barracks object that do not accept self signed cert when option given with invalid value', function() {
// Given
const options = {
var options = {
apiKey: API_KEY,
allowSelfSigned: 'plop'
};

// When
const barracks = new Barracks(options);
var barracks = new Barracks(options);

// Then
validateBarracksObject(barracks, 'https://app.barracks.io');
expect(process.env.NODE_TLS_REJECT_UNAUTHORIZED).to.be.equals(undefined);
});

it('Should return the Barracks object that accepts self signed cert when option given', function () {
it('Should return the Barracks object that accepts self signed cert when option given', function() {
// Given
const options = {
var options = {
apiKey: API_KEY,
allowSelfSigned: true
};

// When
const barracks = new Barracks(options);
var barracks = new Barracks(options);

// Then
validateBarracksObject(barracks, 'https://app.barracks.io');
Expand All @@ -95,6 +95,8 @@ describe('Constructor : ', function () {

describe('listenMessages(apiKey, unitId, timeout)', function() {

let barracks;
var barracks;
beforeEach({});

it('should work perfectly fine') ;
});

0 comments on commit 56986c9

Please sign in to comment.