Commit 807de32e authored by Sebastian Pekarek's avatar Sebastian Pekarek

first commit

parents
/.DS_Store
\ No newline at end of file
Copyright (c) 2013 Sebastian Pekarek
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
# ical-generator
ical-generator is a small piece of code which generates ical calendar files. I use this to generate subscriptionable calendar feeds.
## Installation
npm install ical-generator
## Example
```javascript
var ical = require('ical-generator'),
http = require('http'),
cal = ical();
cal.addEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event',
description: 'It works ;)',
url: 'http://sebbo.net/'
});
http.createServer(function(req, res) {
cal.serve(res);
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
```
\ No newline at end of file
var ical = require('../lib/ical-generator.js'),
http = require('http'),
cal = ical();
cal.addEvent({
start: new Date(),
end: new Date(new Date().getTime() + 3600000),
summary: 'Example Event',
description: 'It works ;)',
url: 'http://sebbo.net/'
});
http.createServer(function(req, res) {
cal.serve(res);
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
\ No newline at end of file
var a = {
newCalendar: function(events) {
var cal = {
domain: null,
prodid: '//sebbo.net//ical-generator//EN',
generated: null,
events: [],
methods: {
setDomain: function(domain) {
cal.domain = domain.toString();
return cal.methods;
},
setProdID: function(id) {
if(!id.company) {
throw 'prodid.company is a mandatory item.';
}
if(!id.product) {
throw 'prodid.product is a mandatory item.';
}
id.language = (id.language || 'EN').toUpperCase();
cal.prodid = '//' + id.company + '//' + id.product + '//' + id.language;
return cal.methods;
},
addEvent: function(e) {
var _event = {};
// UID
_event.uid = e.uid || ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).substr(-4);
// Date Start
if(!e.start) {
throw 'event.start is a mandatory item.';
}
if(Object.prototype.toString.call(e.start) != '[object Date]') {
throw 'event.start must be a Date Object.';
}
_event.start = e.start;
// Date Stop
if(!e.end) {
throw 'event.end is a mandatory item.';
}
if(Object.prototype.toString.call(e.end) != '[object Date]') {
throw 'event.end must be a Date Object.';
}
_event.end = e.end;
// Date Stamp
if(e.stamp && Object.prototype.toString.call(e.stamp) != '[object Date]') {
throw 'event.stamp must be a Date Object.';
}
_event.stamp = e.stamp || new Date();
// Summary
if(!e.summary) {
throw 'event.summary is a mandatory item.';
}
_event.summary = e.summary;
// Description
_event.description = e.description || null;
// Organizer
e.organizer = null;
if(e.organizer) {
if(!e.organizer.name) {
throw 'event.organizer.name is empty.';
}
if(!e.organizer.email) {
throw 'event.organizer.email is a empty.';
}
_event.organizer = {
name: e.organizer.name,
email: e.organizer.email
};
}
// URL
_event.url = e.url || null;
cal.events.push(_event);
return cal.methods;
},
generate: function() {
var g;
var _formatDate = function(d, dateonly) {
var pad = function(i) {
return (i < 10 ? '0': '') + i;
};
var s = d.getFullYear();
s += pad(d.getMonth() + 1);
s += pad(d.getDate());
if(!dateonly) {
s += 'T';
s += pad(d.getHours());
s += pad(d.getMinutes());
s += pad(d.getSeconds());
}
return s;
};
var _getUID = function(e) {
return _formatDate(e.start) + 'Z-' + e.uid + '@' + cal.domain;
};
// VCALENDAR and VERSION
g = 'BEGIN:VCALENDAR\nVERSION:2.0\n';
// PRODID
g += 'PRODID:-' + cal.prodid + '\n';
// Domain
if(!cal.domain) {
cal.domain = require("os").hostname() || 'localhost';
}
// Events
cal.events.forEach(function(e) {
g += 'BEGIN:VEVENT\n';
g += 'UID:' + _getUID(e) + '\n';
g += 'DTSTAMP:' + _formatDate(e.stamp) + '\n';
g += 'DTSTART:' + _formatDate(e.start) + '\n';
g += 'DTEND:' + _formatDate(e.end) + '\n';
if(e.summary) g += 'SUMMARY:' + e.summary + '\n';
if(e.description) g += 'DESCRIPTION:' + e.description + '\n';
if(e.organizer) g += 'ORGANIZER;CN="' + e.organizer.name + '":mailto:' + e.organizer.email + '\n';
if(e.url) g += 'URL;VALUE=URI:' + e.url + '\n';
g += 'END:VEVENT\n';
});
g += 'END:VCALENDAR';
cal.generated = g;
},
save: function(path, cb) {
if(!cal.generated) cal.methods.generate();
require('fs')[cb ? 'writeFile' : 'writeFileSync'](path, cal.generated, cb || undefined);
},
serve: function(res) {
if(!cal.generated) cal.methods.generate();
res.writeHead(200, {
'Content-Type': 'text/calendar',
'Content-Disposition': 'attachment; filename="calendar.ics"'
});
res.end(cal.generated);
}
}
};
return cal.methods;
}
};
module.exports = a.newCalendar;
\ No newline at end of file
{
"name": "ical-generator",
"description": "ical-generator is a small piece of code which generates ical calendar files",
"author": "Sebastian Pekarek <mail@sebbo.net>",
"version": "0.1.0",
"bugs": {
"url": "http://github.com/sebbo2002/ical-generator/issues",
"email": "bugs@sebbo.net"
},
"preferGlobal": false,
"main": "./lib/ical-generator.js",
"repository": {
"type": "git",
"url": "http://github.com/sebbo2002/ical-generator.git"
}
}
\ No newline at end of file
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