-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
213 lines (186 loc) · 4.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
const h = require('highland'),
xml = require('xml'),
_findIndex = require('lodash/findIndex'),
_get = require('lodash/get'),
format = require('date-fns/format'),
docsUrl = 'http://blogs.law.harvard.edu/tech/rss',
generatorMessage = 'Feed delivered by Clay';
let log = require('./services/log').setup({ file: __filename });
/**
* Elevate category tags into the
* the top of the document
*
* @param {Array} group
* @return {Array}
*/
function elevateCategory(group) {
return group
.map(({ item }) => {
return item
.filter(entry => entry && entry.category)
.map(entry => entry && entry.category)
.join(',');
})
.filter(Boolean)
.map(string => ({ category: string }));
}
/**
* Add the meta tags around the feed
*
* @param {String} title
* @param {String} description
* @param {String} link
* @param {String|Number} [copyright]
* @param {String} [generator]
* @param {String} [docs]
* @param {String} [opt]
* @param {Object} image
* @return {Array}
*/
function feedMetaTags({ title, description, link, copyright, generator = generatorMessage, docs = docsUrl, opt, image }) {
return (group) => {
let now, siteMeta;
if (!title || !description || !link) {
throw new Error('A `title`, `description` and `link` property are all required in the `meta` object for the RSS renderer');
}
now = new Date();
siteMeta = [
{ title },
{ description },
{ link },
{ lastBuildDate: format(now, 'ddd, DD MMM YYYY HH:mm:ss ZZ') }, // Date format must be RFC 822 compliant
{ docs },
{ copyright: copyright || now.getFullYear() },
{ generator }
];
if (opt) {
siteMeta = siteMeta.concat(opt);
}
if (image) {
siteMeta = siteMeta.concat(formatImageTag(image.url, link, title));
}
return siteMeta.concat(elevateCategory(group), group);
};
}
/**
* Remove falsy values from an object
*
* @param {Object} obj
* @returns {Object}
*/
function cleanNullValues(obj) {
for (let propName in obj) {
if (!obj[propName]) {
delete obj[propName];
}
}
return obj;
}
/**
* Wraps content in top level RSS and Channel tags
*
* @param {Array} data
* @param {Object} attr
* @return {Object}
*/
function wrapInTopLevel(data, attr = {}) {
const defaultNamespaces = {
version: '2.0',
'xmlns:content': 'http://purl.org/rss/1.0/modules/content/',
'xmlns:mi': 'http://schemas.ingestion.microsoft.com/common/',
'xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'xmlns:media': 'http://search.yahoo.com/mrss/'
};
return {
rss: [{
_attr: cleanNullValues(Object.assign(defaultNamespaces, attr))
}, {
channel: data
}]
};
}
/**
* Wrap each entry in an object under the `item` property
*
* @param {Object} entry
* @return {Object}
*/
function wrapInItem(entry) {
if (entry.length) {
const imageIndex = findIndexOfElementInArray(entry, 'image');
if (imageIndex !== -1) entry.splice(imageIndex, 1);
}
return { item: entry };
}
function sendError(res, e, message = e.message) {
const status = 500;
res.status(status);
res.json({ status, message });
log('error', e.message, {
stack: e.stack
});
}
/**
* Given the data object from Amphora, make the XML
*
* @param {Object} data
* @param {Object} info
* @param {Object} res
* @return {Promise}
*/
function render({ feed, meta, attr }, info, res) {
if (feed.length) {
const imageIndex = findIndexOfElementInArray(feed[0], 'image'),
url = _get(feed[0][imageIndex], 'image.url');
if (url) meta.image = { url };
}
return h(feed)
.map(wrapInItem)
.collect()
.map(feedMetaTags(meta))
.map(data => wrapInTopLevel(data, attr))
.errors(e => sendError(res, e))
.toPromise(Promise)
.then(data => {
if (!data) {
throw new Error('No data send to XML renderer, cannot respond');
}
res.type('text/rss+xml');
res.send(xml(data, { declaration: true, indent: '\t' }));
})
.catch(e => sendError(res, e));
}
/**
* Finds the index of a given element in an array.
*
* @param {Array} array
* @param {String} element
* @returns {number}
*/
function findIndexOfElementInArray(array, element) {
return _findIndex(array, (item) => item[element]);
}
/**
* Formats image tag on the rss feed.
*
* @param {String} url
* @param {String} link
* @param {String} title
* @returns {Object}
*/
function formatImageTag(url, link, title) {
return { image: [
{ url },
{ link },
{ title }
]};
}
module.exports.render = render;
// Exported for testing
module.exports.wrapInItem = wrapInItem;
module.exports.wrapInTopLevel = wrapInTopLevel;
module.exports.feedMetaTags = feedMetaTags;
module.exports.elevateCategory = elevateCategory;
module.exports.cleanNullValues = cleanNullValues;
module.exports.setLog = (fake) => log = fake;