diff --git a/lib/index.js b/lib/index.js index adb64ed..6838903 100755 --- a/lib/index.js +++ b/lib/index.js @@ -2,27 +2,48 @@ const xml = require('xml'); -function ifTruePush(bool, array, data) { - if (bool) { +/** + * Check first argument. If true - push last argument to second argument + * @param condition + * @param array + * @param data + */ +function ifTruePush(condition, array, data) { + if (condition) { array.push(data); } } -function generateXML(data) { - - let channel = []; - - channel.push({title: {_cdata: data.title}}); - channel.push({link: data.link || 'http://github.com/LightAir/turbo-rss'}); - channel.push({description: {_cdata: data.description || data.title}}); - channel.push({language: 'ru'}); - - data.items.forEach(function (item) { +/** + * @param related + * @param itemValues + */ +function addRelated(related, itemValues) { + ifTruePush(related, itemValues, { + 'yandex:related': related.map( + function (rel) { + return { + link: [{ + _attr: { + 'url': rel.link, + 'img': rel.image_url + } + }, rel.text] + }; + } + ) + }); +} +/** + * Items processing + * @param items + * @param channel + */ +function items(items, channel) { + items.forEach(function (item) { let item_values = []; - item_values.push({_attr: {'turbo': 'true'}}); - item_values.push({link: item.url}); item_values.push({'turbo:source': item.url}); @@ -45,24 +66,27 @@ function generateXML(data) { item_values.push({'turbo:content': {_cdata: fullContent}}); if (typeof item.related !== 'undefined') { - ifTruePush(item.related, item_values, { - 'yandex:related': item.related.map( - function (related) { - return { - link: [{ - _attr: { - 'url': related.link, - 'img': related.image_url - } - }, related.text] - }; - } - ) - }); + addRelated(item.related, item_values); } channel.push({item: item_values}); }); +} + +/** + * @param data + * @returns {{rss: *[]}} + */ +function generateXML(data) { + + let channel = []; + + channel.push({title: {_cdata: data.title}}); + channel.push({link: data.link || 'http://github.com/LightAir/turbo-rss'}); + channel.push({description: {_cdata: data.description || data.title}}); + channel.push({language: 'ru'}); + + items(data.items, channel); let _attr = { 'xmlns:yandex': 'http://news.yandex.ru', @@ -79,6 +103,12 @@ function generateXML(data) { }; } +/** + * Base function + * @param options + * @param items + * @constructor + */ function YTurbo(options, items) { options = options || {}; @@ -87,18 +117,18 @@ function YTurbo(options, items) { this.link = options.link; this.items = items || []; - this.item = function (options) { - options = options || {}; + this.item = function (data) { + data = data || {}; let item = { - title: options.title || 'No title', - description: options.description || '', - image_url: options.image_url, - url: options.url, - author: options.author, - date: options.date || options.pubDate, - content: options.content, - menu: options.menu, - related: options.related + title: data.title || 'No title', + description: data.description || '', + image_url: data.image_url, + url: data.url, + author: data.author, + date: data.date || data.pubDate, + content: data.content, + menu: data.menu, + related: data.related }; this.items.push(item); diff --git a/Лист ознакомления.doc b/Лист ознакомления.doc new file mode 100644 index 0000000..3fcff78 Binary files /dev/null and b/Лист ознакомления.doc differ