Commit 2c4853de authored by Sebastian's avatar Sebastian
Browse files

Merge branch 'feature/X-PUBLISHED-TTL' into develop

parents b94bfdcd 52ee902e
......@@ -172,6 +172,15 @@ var cal = ical().timezone('Europe/Berlin');
```
#### ttl([_Number_ ttl])
Use this method to set your feed's time to live. Is used to fill `REFRESH-INTERVAL` and `X-PUBLISHED-TTL` in your iCal.
```javascript
var cal = ical().ttl(60 * 60 * 24);
```
#### createEvent([_Object_ options])
Creates a new [Event](#event) ([`ICalEvent`](#event)) and returns it. Use options to prefill the event's attributes.
......
......@@ -46,7 +46,18 @@ module.exports.duration = function duration(seconds) {
seconds *= -1;
}
string += 'PT';
string += 'P';
// DAYS
if(seconds >= 86400) {
string += Math.floor(seconds / 86400) + 'D';
seconds %= 86400;
}
if(!seconds && string.length > 1) {
return string;
}
string += 'T';
// HOURS
if(seconds >= 3600) {
......
......@@ -14,7 +14,8 @@ var ICalCalendar = function(_data) {
generate = function(calendar) {
var g = '';
var tools = require('./_tools.js'),
g = '';
// VCALENDAR and VERSION
g = 'BEGIN:VCALENDAR\r\nVERSION:2.0\r\n';
......@@ -27,10 +28,17 @@ var ICalCalendar = function(_data) {
g += 'X-WR-CALNAME:' + data.name + '\r\n';
}
// Timezone
if(data.timezone) {
g += 'X-WR-TIMEZONE:' + data.timezone + '\r\n';
}
// TTL
if(data.ttl) {
g += 'REFRESH-INTERVAL;VALUE=DURATION:' + tools.duration(data.ttl) + '\r\n';
g += 'X-PUBLISHED-TTL:' + tools.duration(data.ttl) + '\r\n';
}
// Events
data.events.forEach(function(event) {
g += event.generate(calendar);
......@@ -216,6 +224,25 @@ var ICalCalendar = function(_data) {
};
/**
* Set/Get your feed's TTL.
* Used to set `X-PUBLISHED-TTL` and `REFRESH-INTERVAL`.
*
* @param [ttl] TTL
* @example cal.ttl(60 * 60 * 24); // 1 day
* @since 0.2.5
* @returns {ICalCalendar}
*/
this.ttl = function(ttl) {
if(!ttl) {
return data.ttl;
}
data.ttl = parseInt(ttl, 10) || null;
return this;
};
/**
* Create a new Event and return the calendar object…
*
......
......@@ -16,6 +16,14 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
var tools = require(__dirname + '/../lib/_tools.js');
assert.equal(tools.duration(0), 'PT0S');
});
it('case #3', function() {
var tools = require(__dirname + '/../lib/_tools.js');
assert.equal(tools.duration(178225), 'P2DT1H30M25S');
});
it('case #4', function() {
var tools = require(__dirname + '/../lib/_tools.js');
assert.equal(tools.duration(259200), 'P3D');
});
});
});
......@@ -150,6 +158,28 @@ describe('ical-generator 0.2.x / ICalCalendar', function() {
});
});
describe('ttl()', function() {
it('setter should return this', function() {
var cal = ical();
assert.deepEqual(cal, cal.ttl(60 * 60 * 24));
});
it('getter should return value', function() {
var cal = ical().ttl(86400);
assert.equal(cal.ttl(), 86400);
});
it('should change something', function() {
var cal = ical().ttl(86400);
cal.createEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event'
});
assert.ok(cal.toString().indexOf('P1D') > -1);
});
});
describe('createEvent()', function() {
it('should return a ICalEvent instance', function() {
var cal = ical(),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment