Skip to content

Commit

Permalink
kml cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Jul 26, 2017
1 parent fa115da commit f640502
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
queriesPath: '/tmp/query.txt',

// Firebase repository where all alive reports will be sent.
// KMLs and other commands can also be sent through it.
firebase: {
apiKey: 'AIzaSyAddzwazFGRJiC-GW35Zgr7XdhUk8x0890',
authDomain: 'liquid-galaxy-api.firebaseapp.com',
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/api/kml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ describe('KML', () => {

expect(result.status).to.equal(400);
});

it('should clean kml', async () => {
const result = await fetchApi('/kmls/clean', { headers, method: 'POST' });

expect(result.status).to.equal(200);

expect(stubWriteFile[0].filename).to.equal(kmlPath);
expect(stubWriteFile[0].contents).to.equal('');
});
});

describe('POST /queries', () => {
Expand Down
12 changes: 8 additions & 4 deletions src/controllers/kml.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ const { ServerError } = require('../helpers/server');
const {
saveKmlOnDisk,
saveKmlUriOnDisk,
cleanKml: cleanKmlService,
saveQueryOnDisk,
} = require('../services').kml;

function createKml(values) {
const { contents, uri } = values;
function createKml({ contents, uri }) {
if (Number(!!contents) + Number(!!uri) !== 1) {
throw new ServerError('Either Contents or Uri must be defined.', 400);
}

return contents ? saveKmlOnDisk(contents) : saveKmlUriOnDisk(uri);
}

function createQuery(values) {
const contents = values.contents || '';
function cleanKml() {
return cleanKmlService('');
}

function createQuery({ contents = '' }) {
return saveQueryOnDisk(contents);
}

module.exports = {
createKml,
cleanKml,
createQuery,
};
26 changes: 6 additions & 20 deletions src/firebase/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ const controllers = require('../controllers');

const { kml } = controllers;

const SERVER_TIME = firebase.database.ServerValue.TIMESTAMP;

const KML_VALUE = 'kml:value';
const KML_HREF = 'kml:href';
const QUERIES = 'queries';

/* eslint-disable quote-props */
const routes = value => ({
[KML_VALUE]: () => kml.createKml({ contents: value }),
[KML_HREF]: () => kml.createKml({ uri: value }),
[QUERIES]: () => kml.createQuery({ contents: value }),
'kml:value': () => kml.createKml({ contents: value }),
'kml:href': () => kml.createKml({ uri: value }),
'kml:clean': () => kml.cleanKml(),
'queries': () => kml.createQuery({ contents: value }),
});
/* eslint-enable quote-props */

function controllerHandler(route, value) {
const routeAction = routes(value)[route];
Expand Down Expand Up @@ -47,17 +44,6 @@ function listenQueue(uid) {
});
}

function demoKml(uid) {
const encodedUid = encodeUid(uid);
const dbRef = firebase.database().ref(`queue/${encodedUid}`);
dbRef.push({
type: KML_VALUE,
value: '123',
timestamp: SERVER_TIME,
});
}

module.exports = {
listenQueue,
demoKml,
};
1 change: 1 addition & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ router.get('/', c(hello.hello));
* KMLs.
*/
router.post('/kmls', c(kml.createKml, req => [req.body]));
router.post('/kmls/clean', c(kml.cleanKml));
router.post('/queries', c(kml.createQuery, req => [req.body]));

/**
Expand Down
8 changes: 8 additions & 0 deletions src/services/kml.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ function saveKmlUriOnDisk(uri) {
return fs.writeFile(kmlPath, `${uri}?${uuid()}`);
}

/**
* Empties kmls.txt.
*/
function cleanKml() {
return fs.writeFile(kmlPath, '');
}

function saveQueryOnDisk(contents) {
return fs.writeFile(queriesPath, contents);
}

module.exports = {
saveKmlOnDisk,
saveKmlUriOnDisk,
cleanKml,
saveQueryOnDisk,
};
1 change: 1 addition & 0 deletions src/sockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function connectionHandler(socket) {
* KMLs.
*/
socket.on('P:/kmls', c(kml.createKml, data => [data]));
socket.on('P:/kmls/clean', c(kml.cleanKml));
socket.on('P:/queries', c(kml.createQuery, data => [data]));
}

Expand Down

0 comments on commit f640502

Please sign in to comment.