-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement alpha release of semantics API #128
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |