diff --git a/lib/rss.js b/lib/rss.js index b2a99d9..a903c77 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -2,7 +2,9 @@ Documentation coming soon. */ -var XML = require('xml'); +var XML = require('xml'), + mime = require('mime'), + fs = require('fs'); function RSS (options, items) { options = options || {}; @@ -24,7 +26,8 @@ function RSS (options, items) { guid: options.guid, categories: options.categories || [], author: options.author, - date: options.date + date: options.date, + enclosure: options.enclosure || false }; this.items.push(item); @@ -72,7 +75,33 @@ function generateXML (data){ ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } }); ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() }); + + if( item.enclosure && item.enclosure.url ) { + if( item.enclosure.file ) { + item_values.push({ + enclosure : { + _attr : { + url : item.enclosure.url, + length : fs.statSync(item.enclosure.file).size, + type : mime.lookup(item.enclosure.file) + } + } + }); + } else { + item_values.push({ + enclosure : { + _attr : { + url : item.enclosure.url, + length : item.enclosure.size || 0, + type : mime.lookup(item.enclosure.url) + } + } + }); + } + } + channel.push({ item: item_values }); + }); return { rss: [ diff --git a/package.json b/package.json index f2889ea..d69a64e 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ }, "dependencies": { - "xml": ">= 0.0.4" + "xml": ">= 0.0.4", + "mime": ">= 1.2.9" }, "main": "lib/rss.js", "engines": { "node": ">=0.4.0" }, diff --git a/readme.md b/readme.md index 575450e..e39b531 100644 --- a/readme.md +++ b/readme.md @@ -35,6 +35,7 @@ guid: '1123', // optional - defaults to url author: 'Guest Author', // optional - defaults to feed author property date: 'May 27, 2012' // any format that js Date can parse. + enclosure : {url:'...', file:'path-to-file'} // optional }); // cache the xml @@ -97,7 +98,7 @@ especially when tests are included. (The MIT License) -Copyright (c) 2011 Dylan Greene +Copyright (c) 2011-2013 Dylan Greene Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/test/image.png b/test/image.png new file mode 100644 index 0000000..9405f51 Binary files /dev/null and b/test/image.png differ diff --git a/test/test.js b/test/test.js index 597c5c4..b0a60ab 100644 --- a/test/test.js +++ b/test/test.js @@ -99,6 +99,51 @@ module.exports = { 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'; var result = feed.xml(); + test.equal(result.length, expectedResult.length); + test.equal(result, expectedResult); + test.done(); + }, + + 'test with enclosure' : 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', + enclosure : 'incorrect value' + }); + + feed.item({ + title: 'item 2', + description: 'description 2', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + enclosure : {url: '/media/some-file.flv'} + }); + + feed.item({ + title: 'item 3', + description: 'description 3', + url: 'http://example.com/article1', + date: 'May 24, 2012 04:00:00 GMT', + enclosure : {url: '/media/image.png', file : __dirname+'/image.png'} + }); + + 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/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT'+ + '<![CDATA[item 3]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT'+ + ''; + var result = feed.xml(); + test.equal(result.length, expectedResult.length); test.equal(result, expectedResult); test.done();