/** * Author: Wesnydy Lima Ribeiro * Email: wesnydy@lavid.ufpb.br */ 'use strict'; /** * Required libs. */ var shortid = require('shortid') , amqp = require('../helpers/amqpManager') , error = require('../helpers/error') , settings = require('../config/settings'); exports.translate = function(req, res, next) { if (!req.body.text) return error.badRequest('The text key is missing.', next); var params = {}; params.id = shortid.generate(); params.text = req.body.text; params.lang = req.body.lang ? req.body.lang : settings.defaultLang; amqp.sendToQueue(JSON.stringify(params), id,'texts', false, res, function(err) { if (err) return error.internalError('An internal communication error has occurred.', next); amqp.receiveFromQueue(id, 'translations', false, res, function(err, message) { if (err) return error.internalError('An internal communication error has occurred.', next); res.status(200).send(message); }); }); }; // TEMPORARY exports.translateURL = function(req, res, next) { if (!req.param('text')) return error.badRequest('The text param is missing.', next); var id = shortid.generate(); var text = req.param('text').toString('utf8'); amqp.sendToQueue(text, id,'texts', false, res, function(err) { if (err) return error.internalError('An internal communication error has occurred.', next); amqp.receiveFromQueue(id, 'translations', false, res, function(err, message) { if (err) return error.internalError('An internal communication error has occurred.', next); res.status(200).send(message); }); }); };