From 01a202705337f67cc6f900741d515cbcd2e9402a Mon Sep 17 00:00:00 2001 From: langmi Date: Sun, 4 Mar 2012 18:00:38 +0100 Subject: [PATCH] added parsing of image_url and test --- .gitignore | 1 + lib/rss.js | 18 +++++++++++------- test/test.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/lib/rss.js b/lib/rss.js index 521aea6..e77c6ee 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -48,13 +48,17 @@ function ifTruePush(bool, array, data) { function generateXML (data){ // todo: xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" - var channel = [ - { title: { _cdata: data.title } }, - { description: { _cdata: data.description || data.title } }, - { link: data.site_url || 'http://github.com/dylan/node-rss' }, - { generator: 'NodeJS RSS Module' }, - { lastBuildDate: new Date().toGMTString() } - ]; + var channel = []; + channel.push({ title: { _cdata: data.title } }); + channel.push({ description: { _cdata: data.description || data.title } }); + channel.push({ link: data.site_url || 'http://github.com/dylan/node-rss' }); + // image_url set? + if (data.image_url) { + channel.push({ image: [ {url: data.image_url}, {title: data.title}, {link: data.site_url} ] }); + } + channel.push({ generator: 'NodeJS RSS Module' }); + channel.push({ lastBuildDate: new Date().toGMTString() }); + ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); // { updated: new Date().toGMTString() } diff --git a/test/test.js b/test/test.js index f6f915b..66ecc5b 100644 --- a/test/test.js +++ b/test/test.js @@ -29,27 +29,71 @@ module.exports = { title: 'item 1', description: 'description 1', url: 'http://example.com/article1', - date: 'May 24, 2012' + date: 'May 24, 2012 04:00:00 GMT' }) .item({ title: 'item 2', description: 'description 2', url: 'http://example.com/article2', - date: 'May 25, 2012' + date: 'May 25, 2012 04:00:00 GMT' }) .item({ title: 'item 3', description: 'description 3', url: 'http://example.com/article3', guid: 'item3', - date: 'May 26, 2012' + date: 'May 26, 2012 04:00:00 GMT' }) .item({ title: 'item 4 & html test with ', description: 'description 4 uses some html', url: 'http://example.com/article4?this&that', author: 'Guest Author', - date: 'May 27, 2012' + date: 'May 27, 2012 04:00:00 GMT' + }); + + var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT'; + var result = feed.xml(); + + test.equal(result.length, expectedResult.length); + test.equal(result, expectedResult); + test.done(); + }, + + 'easy test without image_url': function(test) { + var feed = new RSS({ + title: 'title', + description: 'description', + feed_url: 'http://example.com/rss.xml', + site_url: 'http://example.com', + author: 'Dylan Greene' + }); + + feed.item({ + title: 'item 1', + description: 'description 1', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 2', + description: 'description 2', + url: 'http://example.com/article2', + date: 'May 25, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 3', + description: 'description 3', + url: 'http://example.com/article3', + guid: 'item3', + date: 'May 26, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 4 & html test with ', + description: 'description 4 uses some html', + url: 'http://example.com/article4?this&that', + author: 'Guest Author', + date: 'May 27, 2012 04:00:00 GMT' }); var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT';