Skip to content

Commit

Permalink
fix lots of little issues reported by codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Jul 1, 2015
1 parent 61f580b commit fd58572
Show file tree
Hide file tree
Showing 54 changed files with 621 additions and 592 deletions.
12 changes: 5 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ function create (env, ctx) {
app.set('title', appInfo);
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.

app.use(compression({filter: shouldCompress}));

function shouldCompress(req, res) {
//TODO: return false here if we find a condition where we don't want to compress
// fallback to standard filter function
return compression.filter(req, res);
}
app.use(compression({filter: function shouldCompress(req, res) {
//TODO: return false here if we find a condition where we don't want to compress
// fallback to standard filter function
return compression.filter(req, res);
}}));

//if (env.api_secret) {
// console.log("API_SECRET", env.api_secret);
Expand Down
20 changes: 10 additions & 10 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function config ( ) {
* First inspect a bunch of environment variables:
* * PORT - serve http on this port
* * MONGO_CONNECTION, CUSTOMCONNSTR_mongo - mongodb://... uri
* * CUSTOMCONNSTR_mongo_collection - name of mongo collection with "sgv" documents
* * CUSTOMCONNSTR_mongo_collection - name of mongo collection with `sgv` documents
* * API_SECRET - if defined, this passphrase is fed to a sha1 hash digest, the hex output is used to create a single-use token for API authorization
* * NIGHTSCOUT_STATIC_FILES - the "base directory" to use for serving
* * NIGHTSCOUT_STATIC_FILES - the `base directory` to use for serving
* static files over http. Default value is the included `static`
* directory.
*/
Expand All @@ -23,10 +23,10 @@ function config ( ) {

if (readENV('APPSETTING_ScmType') == readENV('ScmType') && readENV('ScmType') == 'GitHub') {
env.head = require('./scm-commit-id.json');
console.log("SCM COMMIT ID", env.head);
console.log('SCM COMMIT ID', env.head);
} else {
git.short(function record_git_head (head) {
console.log("GIT HEAD", head);
console.log('GIT HEAD', head);
env.head = head || readENV('SCM_COMMIT_ID') || readENV('COMMIT_HASH', '');
});
}
Expand Down Expand Up @@ -54,7 +54,7 @@ function config ( ) {
env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');

env.enable = readENV('ENABLE', "");
env.enable = readENV('ENABLE', '');

env.defaults = { // currently supported keys must defined be here
'units': 'mg/dL'
Expand Down Expand Up @@ -127,7 +127,7 @@ function config ( ) {
// if a passphrase was provided, get the hex digest to mint a single token
if (useSecret) {
if (readENV('API_SECRET').length < consts.MIN_PASSPHRASE_LENGTH) {
var msg = ["API_SECRET should be at least", consts.MIN_PASSPHRASE_LENGTH, "characters"];
var msg = ['API_SECRET should be at least', consts.MIN_PASSPHRASE_LENGTH, 'characters'];
var err = new Error(msg.join(' '));
// console.error(err);
throw err;
Expand Down Expand Up @@ -170,9 +170,9 @@ function config ( ) {
console.warn('BG_HIGH is now ' + env.thresholds.bg_high);
}

//if any of the BG_* thresholds are set, default to "simple" otherwise default to "predict" to preserve current behavior
//if any of the BG_* thresholds are set, default to `simple` otherwise default to `predict` to preserve current behavior
var thresholdsSet = readIntENV('BG_HIGH') || readIntENV('BG_TARGET_TOP') || readIntENV('BG_TARGET_BOTTOM') || readIntENV('BG_LOW');
env.alarm_types = readENV('ALARM_TYPES') || (thresholdsSet ? "simple" : "predict");
env.alarm_types = readENV('ALARM_TYPES') || (thresholdsSet ? 'simple' : 'predict');

//TODO: maybe get rid of ALARM_TYPES and only use enable?
if (env.alarm_types.indexOf('simple') > -1) {
Expand Down Expand Up @@ -223,8 +223,8 @@ function readENV(varName, defaultValue) {
|| process.env[varName]
|| process.env[varName.toLowerCase()];

if (typeof value === 'string' && value.toLowerCase() == 'on') value = true;
if (typeof value === 'string' && value.toLowerCase() == 'off') value = false;
if (typeof value === 'string' && value.toLowerCase() == 'on') { value = true; }
if (typeof value === 'string' && value.toLowerCase() == 'off') { value = false; }

return value != null ? value : defaultValue;
}
Expand Down
73 changes: 37 additions & 36 deletions lib/api/devicestatus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,49 @@
var consts = require('../../constants');

function configure (app, wares, ctx) {
var express = require('express'),
api = express.Router( );

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw( ));
// json body types get handled as parsed json
api.use(wares.bodyParser.json( ));
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));

// List settings available
api.get('/devicestatus/', function(req, res) {
var q = req.query;
if (!q.count) {
q.count = 10;
}
ctx.devicestatus.list(q, function (err, results) {
return res.json(results);
});
var express = require('express'),
api = express.Router( );

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw( ));
// json body types get handled as parsed json
api.use(wares.bodyParser.json( ));
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));

// List settings available
api.get('/devicestatus/', function(req, res) {
var q = req.query;
if (!q.count) {
q.count = 10;
}
ctx.devicestatus.list(q, function (err, results) {
return res.json(results);
});
});

function config_authed (app, api, wares, ctx) {
function config_authed (app, api, wares, ctx) {

api.post('/devicestatus/', /*TODO: auth disabled for quick UI testing... wares.verifyAuthorization, */ function(req, res) {
var obj = req.body;
ctx.devicestatus.create(obj, function (err, created) {
if (err)
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
res.json(created);
});
});
api.post('/devicestatus/', /*TODO: auth disabled for quick UI testing... wares.verifyAuthorization, */ function(req, res) {
var obj = req.body;
ctx.devicestatus.create(obj, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
} else {
res.json(created);
}
});
});

}
}

if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) {
config_authed(app, api, wares, ctx);
}
if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) {
config_authed(app, api, wares, ctx);
}

return api;
return api;
}

module.exports = configure;
Expand Down
4 changes: 2 additions & 2 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function create (env, ctx) {
// Only allow access to the API if API_SECRET is set on the server.
app.disable('api');
if (env.api_secret) {
console.log("API_SECRET", env.api_secret);
console.log('API_SECRET', env.api_secret);
app.enable('api');
}

Expand All @@ -28,7 +28,7 @@ function create (env, ctx) {
app.extendedClientSettings = ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {};
env.enable.toLowerCase().split(' ').forEach(function (value) {
var enable = value.trim();
console.info("enabling feature:", enable);
console.info('enabling feature:', enable);
app.enable(enable);
});
}
Expand Down
6 changes: 3 additions & 3 deletions lib/api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function configure (app, wares) {
'json', 'svg', 'csv', 'txt', 'png', 'html', 'js'
]));
// Status badge/text/json
api.get('/status', function (req, res, next) {
api.get('/status', function (req, res) {
var info = { status: 'ok'
, apiEnabled: app.enabled('api')
, careportalEnabled: app.enabled('api') && app.enabled('careportal')
Expand All @@ -34,13 +34,13 @@ function configure (app, wares) {
res.redirect(302, badge + '.svg');
},
js: function ( ) {
var head = "this.serverSettings =";
var head = 'this.serverSettings =';
var body = JSON.stringify(info);
var tail = ';';
res.send([head, body, tail].join(' '));
},
text: function ( ) {
res.send("STATUS OK");
res.send('STATUS OK');
},
json: function ( ) {
res.json(info);
Expand Down
69 changes: 35 additions & 34 deletions lib/api/treatments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,45 @@
var consts = require('../../constants');

function configure (app, wares, ctx) {
var express = require('express'),
api = express.Router( );

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw( ));
// json body types get handled as parsed json
api.use(wares.bodyParser.json( ));
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));

// List treatments available
api.get('/treatments/', function(req, res) {
ctx.treatments.list({find: req.params}, function (err, results) {
return res.json(results);
});
var express = require('express'),
api = express.Router( );

// invoke common middleware
api.use(wares.sendJSONStatus);
// text body types get handled as raw buffer stream
api.use(wares.bodyParser.raw( ));
// json body types get handled as parsed json
api.use(wares.bodyParser.json( ));
// also support url-encoded content-type
api.use(wares.bodyParser.urlencoded({ extended: true }));

// List treatments available
api.get('/treatments/', function(req, res) {
ctx.treatments.list({find: req.params}, function (err, results) {
return res.json(results);
});
});

function config_authed (app, api, wares, ctx) {

api.post('/treatments/', /*TODO: auth disabled for now, need to get login figured out... wares.verifyAuthorization, */ function(req, res) {
var treatment = req.body;
ctx.treatments.create(treatment, function (err, created) {
if (err) {
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
} else {
res.json(created);
}
});
});

function config_authed (app, api, wares, ctx) {

api.post('/treatments/', /*TODO: auth disabled for now, need to get login figured out... wares.verifyAuthorization, */ function(req, res) {
var treatment = req.body;
ctx.treatments.create(treatment, function (err, created) {
if (err)
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
res.json(created);
});
});

}
}

if (app.enabled('api') && app.enabled('careportal')) {
config_authed(app, api, wares, ctx);
}
if (app.enabled('api') && app.enabled('careportal')) {
config_authed(app, api, wares, ctx);
}

return api;
return api;
}

module.exports = configure;
Expand Down
2 changes: 1 addition & 1 deletion lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function boot (env) {
}

function ensureIndexes (ctx, next) {
console.info("Ensuring indexes");
console.info('Ensuring indexes');
ctx.store.ensureIndexes(ctx.entries( ), ctx.entries.indexedFields);
ctx.store.ensureIndexes(ctx.treatments( ), ctx.treatments.indexedFields);
ctx.store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);
Expand Down
2 changes: 1 addition & 1 deletion lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function init (env, ctx) {
}

function ender ( ) {
if (id) cancelInterval(id);
if (id) { cancelInterval(id); }
stream.emit('end');
}

Expand Down
Loading

0 comments on commit fd58572

Please sign in to comment.