diff options
author | James Lal <james@lightsofapollo.com> | 2012-07-06 16:48:14 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2012-07-06 16:48:14 -0700 |
commit | 1edfd60cccfdda4a21acbdef834f7b9db260af88 (patch) | |
tree | f0b9a1bd3516bf49be257ed77ffad7ab6c92a1f1 /lib | |
parent | 8997061718d9990960dfea0ba58e88138f3b5a88 (diff) | |
download | jsCalDAV-1edfd60cccfdda4a21acbdef834f7b9db260af88.tar.gz |
Adding lightly tested calendar home resolver
Diffstat (limited to 'lib')
-rw-r--r-- | lib/caldav/request/calendar_details.js | 0 | ||||
-rw-r--r-- | lib/caldav/request/calendar_home.js | 125 | ||||
-rw-r--r-- | lib/caldav/request/index.js | 3 |
3 files changed, 127 insertions, 1 deletions
diff --git a/lib/caldav/request/calendar_details.js b/lib/caldav/request/calendar_details.js new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/caldav/request/calendar_details.js diff --git a/lib/caldav/request/calendar_home.js b/lib/caldav/request/calendar_home.js new file mode 100644 index 0000000..6401d8c --- /dev/null +++ b/lib/caldav/request/calendar_home.js @@ -0,0 +1,125 @@ +(function(module, ns) { + + var Propfind = ns.require('request/propfind'); + + + /** + * Creates a propfind request. + * + * @param {Caldav.Connection} connection connection details. + * @param {Object} options options for propfind. + */ + function CalendarHome(connection, options) { + var key; + + if (typeof(options) === 'undefined') { + options = {}; + } + + for (key in options) { + if (options.hasOwnProperty(key)) { + this[key] = options[key]; + } + } + + this.connection = connection; + } + + function findProperty(name, data, single) { + var url, results = [], prop; + + for (url in data) { + if (data.hasOwnProperty(url)) { + if (name in data[url]) { + prop = data[url][name]; + if (prop.status === '200') { + results.push(data[url][name].value); + } + } + } + } + + if (!results.length) + return false; + + if (typeof(single) !== 'undefined' && single) { + return results[0]; + } + + return results; + } + + CalendarHome.prototype = { + + _findPrincipal: function(url, callback) { + var find = new Propfind(this.connection, { + url: url + }); + + find.prop('current-user-principal'); + find.prop('principal-URL'); + + find.send(function(err, data) { + var principal; + + if (err) { + return callback(err); + } + + principal = findProperty('current-user-principal', data, true); + + if (!principal) { + principal = findProperty('principal-URL', data, true); + } + + callback(null, principal); + }); + }, + + _findCalendarHome: function(url, callback) { + var find = new Propfind(this.connection, { + url: url + }); + + find.prop(['caldav', 'calendar-home-set']); + + find.send(function(err, data) { + if (err) { + return callback(err); + } + + callback(null, findProperty('calendar-home-set', data, true)); + }); + }, + + /** + * Starts request to find calendar home url + * + * @param {Function} callback node style where second argument + * are the details of the home calendar. + */ + send: function(callback) { + var self = this; + // find principal + self._findPrincipal(self.url, function(err, url) { + + if (!url) { + return callback(new Error('Cannot resolve principal url')); + } + + self._findCalendarHome(url, function(err, details) { + callback(err, details); + }); + }); + } + + }; + + module.exports = CalendarHome; + +}.apply( + this, + (this.Caldav) ? + [Caldav('request/calendar_home'), Caldav] : + [module, require('../caldav')] +)); diff --git a/lib/caldav/request/index.js b/lib/caldav/request/index.js index fc49491..6790fd1 100644 --- a/lib/caldav/request/index.js +++ b/lib/caldav/request/index.js @@ -3,7 +3,8 @@ module.exports = { Abstract: ns.require('request/abstract'), CalendarQuery: ns.require('request/calendar_query'), - Propfind: ns.require('request/propfind') + Propfind: ns.require('request/propfind'), + CalendarHome: ns.require('request/calendar_home') }; }.apply( |