Skip to content

Commit

Permalink
implement alpha release of semantics API #128
Browse files Browse the repository at this point in the history
  • Loading branch information
vecna committed Apr 29, 2019
1 parent 311b69d commit 87d7f6c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function serveRequest(desc, func, req, res) {

if(_.isObject(httpresult.headers))
_.each(httpresult.headers, function(value, key) {
debug("function %s sets header %s", desc, key);
// debug("function %s sets header %s", desc, key);
res.setHeader(key, value);
});

Expand Down
11 changes: 11 additions & 0 deletions lib/contentAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ module.exports = [
desc: "Timeline CSV",
route: '/api/v2/timeline/:timelineId/csv/',
func: require('../routes/personal').timelineCSV,
},
/* semantics APIs */
{
desc: "Semantics",
route: '/api/v2/:lang/semantics/:paging?',
func: require('../routes/semantics').semantics,
},
{
desc: "Labels",
route: '/api/v2/:lang/labels/:paging?',
func: require('../routes/semantics').labels,
}
];

63 changes: 63 additions & 0 deletions routes/semantics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const _ = require('lodash');
const moment = require('moment');
const Promise = require('bluebird');
const debug = require('debug')('routes:semantics');
const nconf = require('nconf');

const mongo = require('../lib/mongo');
const params = require('../lib/params');

const supported = [ "es", "en", "pt", "vi", "it", "sv", "pl", "hu", "bg", "de", "ro", "tl", "fr" ];
const LanguageError = {
error: true,
message: 'missing language',
supported,
reminder: 'the list of supported language comes from a mongodb.distinct call, should be updated'
};
function validLanguage(propl) {
return (_.size(propl) == 2 || supported.indexOf(propl) !== -1);
}

function labels(req) {
const { amount, skip } = params.optionParsing(req.params.paging, 100);
debug("labels request, amount %d skip %d", amount, skip);

if(!validLanguage(req.params.lang))
return LanguageError;

return mongo
.readLimit(nconf.get('schema').labels, { lang: req.params.lang }, { when: -1 }, amount, skip)
.then(function(data) {
debug("retrived %d objects, with amount %d skip %d", _.size(data), amount, skip);
return { json: data };
})
.catch(function(e) {
debug("data (error): %s", e);
return { 'text': `error: ${e}` };
});
};

function semantics(req) {
const { amount, skip } = params.optionParsing(req.params.paging, 100);
debug("semantic request, amount %d skip %d", amount, skip);

if(!validLanguage(req.params.lang))
return LanguageError;

return mongo
.readLimit(nconf.get('schema').semantics, { lang: req.params.lang }, { when: -1 }, amount, skip)
.then(function(data) {
debug("retrived %d objects, with amount %d skip %d", _.size(data), amount, skip);
return { json: data };
})
.catch(function(e) {
debug("data (error): %s", e);
return { 'text': `error: ${e}` };
});
};


module.exports = {
labels,
semantics,
};

0 comments on commit 87d7f6c

Please sign in to comment.