tm-apps-list-api
Version:
80 lines (70 loc) • 2.48 kB
JavaScript
const R = require("ramda");
const hl = require("highland");
const restify = require("restify");
const request = require("superagent");
module.exports = {
"setCacheEndpoint": endpoint => {
return R.test(/dev/, endpoint) ? endpoint : endpoint.replace("org-", "");
},
"addMeta": list => {
return {
"data": {
"type": "articles",
"attributes": {
// Set timestamp to a static value. This is a legacy property.
"timestamp": 1000000000000
}
},
"included": list
};
},
"sendResponse": (logger, res, next) => (err, result) => {
if (err) {
logger.error("Sections Request Error", err);
logger.timeEnd('Finished Sections Request');
if (R.has("status", err) && R.has("message", err) && R.has(err.status, restify)) {
next(new restify[err.status](err.message));
} else {
next(new restify.InternalServerError(err.stack));
}
} else {
logger.timeEnd('Finished Sections Request');
res.charSet('utf-8');
res.send(result);
}
},
"notFound": message => hl(push => push({
"message": message,
"status": "NotFoundError"
})),
"getArticlesInParallel": (logger, getArticle, notFound, stream) => {
return stream
.map(R.compose(parseInt, R.last, R.split(".")))
.uniq()
.map(getArticle)
.parallel(40)
.compact()
.errors(err => {
logger.error("Sections Request. Error retrieving article", err);
})
.otherwise(notFound)
.collect();
},
"makeArticleRequest": (logger, articleEndpoint, source, publicationId) => hl.wrapCallback((id, cb) => {
request
.get(`${articleEndpoint}/${source}/${publicationId}/articles/${id}`)
.timeout(3000)
.end((err, res) => {
if (err || res.error) {
logger.error('error requesting article', {article_id: id, err_msg: err.message});
cb(res ? res.body : err);
} else {
cb(null, res.body);
}
});
}),
"shouldBeExcluded": article => {
return R.path(['data', 'attributes', 'exclude'])(article);
}
};
;