mirror of
https://github.com/LightAir/turbo-rss.git
synced 2026-02-04 03:56:19 +00:00
Code climate
This commit is contained in:
108
lib/index.js
108
lib/index.js
@@ -2,27 +2,48 @@
|
|||||||
|
|
||||||
const xml = require('xml');
|
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);
|
array.push(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateXML(data) {
|
/**
|
||||||
|
* @param related
|
||||||
let channel = [];
|
* @param itemValues
|
||||||
|
*/
|
||||||
channel.push({title: {_cdata: data.title}});
|
function addRelated(related, itemValues) {
|
||||||
channel.push({link: data.link || 'http://github.com/LightAir/turbo-rss'});
|
ifTruePush(related, itemValues, {
|
||||||
channel.push({description: {_cdata: data.description || data.title}});
|
'yandex:related': related.map(
|
||||||
channel.push({language: 'ru'});
|
function (rel) {
|
||||||
|
return {
|
||||||
data.items.forEach(function (item) {
|
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 = [];
|
let item_values = [];
|
||||||
|
|
||||||
item_values.push({_attr: {'turbo': 'true'}});
|
item_values.push({_attr: {'turbo': 'true'}});
|
||||||
|
|
||||||
item_values.push({link: item.url});
|
item_values.push({link: item.url});
|
||||||
item_values.push({'turbo:source': item.url});
|
item_values.push({'turbo:source': item.url});
|
||||||
|
|
||||||
@@ -45,24 +66,27 @@ function generateXML(data) {
|
|||||||
item_values.push({'turbo:content': {_cdata: fullContent}});
|
item_values.push({'turbo:content': {_cdata: fullContent}});
|
||||||
|
|
||||||
if (typeof item.related !== 'undefined') {
|
if (typeof item.related !== 'undefined') {
|
||||||
ifTruePush(item.related, item_values, {
|
addRelated(item.related, item_values);
|
||||||
'yandex:related': item.related.map(
|
|
||||||
function (related) {
|
|
||||||
return {
|
|
||||||
link: [{
|
|
||||||
_attr: {
|
|
||||||
'url': related.link,
|
|
||||||
'img': related.image_url
|
|
||||||
}
|
|
||||||
}, related.text]
|
|
||||||
};
|
|
||||||
}
|
|
||||||
)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
channel.push({item: 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 = {
|
let _attr = {
|
||||||
'xmlns:yandex': 'http://news.yandex.ru',
|
'xmlns:yandex': 'http://news.yandex.ru',
|
||||||
@@ -79,6 +103,12 @@ function generateXML(data) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base function
|
||||||
|
* @param options
|
||||||
|
* @param items
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
function YTurbo(options, items) {
|
function YTurbo(options, items) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
@@ -87,18 +117,18 @@ function YTurbo(options, items) {
|
|||||||
this.link = options.link;
|
this.link = options.link;
|
||||||
this.items = items || [];
|
this.items = items || [];
|
||||||
|
|
||||||
this.item = function (options) {
|
this.item = function (data) {
|
||||||
options = options || {};
|
data = data || {};
|
||||||
let item = {
|
let item = {
|
||||||
title: options.title || 'No title',
|
title: data.title || 'No title',
|
||||||
description: options.description || '',
|
description: data.description || '',
|
||||||
image_url: options.image_url,
|
image_url: data.image_url,
|
||||||
url: options.url,
|
url: data.url,
|
||||||
author: options.author,
|
author: data.author,
|
||||||
date: options.date || options.pubDate,
|
date: data.date || data.pubDate,
|
||||||
content: options.content,
|
content: data.content,
|
||||||
menu: options.menu,
|
menu: data.menu,
|
||||||
related: options.related
|
related: data.related
|
||||||
};
|
};
|
||||||
|
|
||||||
this.items.push(item);
|
this.items.push(item);
|
||||||
|
|||||||
BIN
Лист ознакомления.doc
Normal file
BIN
Лист ознакомления.doc
Normal file
Binary file not shown.
Reference in New Issue
Block a user