1 |
|
2 | 'use strict';
|
3 |
|
4 | const capiV2 = require('./utils/capi-v2');
|
5 | const models = {
|
6 | 'content': (data) => data.content
|
7 | .map(a => a.apiUrl.replace('http://api.ft.com/content/', '')),
|
8 | 'annotations': (data, count) => data.annotations.map(a => a.label)
|
9 | };
|
10 |
|
11 | module.exports = (opts) => {
|
12 | const count = opts.count || 10;
|
13 | const model = opts.model || 'content';
|
14 | const queryString = '?' + ['industry', 'position', 'sector', 'country']
|
15 | .filter(o => opts[o])
|
16 | .map(o => `user-${o}=${opts[o]}`)
|
17 | .join('&');
|
18 |
|
19 | const capiV2Options = {
|
20 | path: `/hui/${model}/${opts.period || 'last-1-day'}${queryString}`,
|
21 | type: 'hui'
|
22 | };
|
23 |
|
24 | if ('agent' in opts) capiV2Options.agent = opts.agent;
|
25 |
|
26 | return capiV2(capiV2Options)
|
27 | .then(data => models[model](data).slice(0, count))
|
28 | .then(articles => {
|
29 | articles.indexCount = articles.length;
|
30 | return articles;
|
31 | });
|
32 | };
|