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 | |
parent | 8997061718d9990960dfea0ba58e88138f3b5a88 (diff) | |
download | jsCalDAV-1edfd60cccfdda4a21acbdef834f7b9db260af88.tar.gz |
Adding lightly tested calendar home resolver
-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 | ||||
-rwxr-xr-x | scripts/connect | 76 | ||||
-rw-r--r-- | test/caldav/request/calendar_home_test.js | 35 | ||||
-rw-r--r-- | test/support/propfind.js | 0 |
6 files changed, 196 insertions, 43 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( diff --git a/scripts/connect b/scripts/connect index 149d54a..84f25a7 100755 --- a/scripts/connect +++ b/scripts/connect @@ -34,6 +34,34 @@ var con = new CalDav.Connection({ }); var Propfind = CalDav.Request.Propfind; +var CalendarHome = CalDav.Request.CalendarHome; + +function getCalendarDetails(caluri) { + + var calFind = findProp(caluri); + calFind.prop(['ical', 'calendar-color']); + calFind.prop('owner'); + calFind.prop('displayname'); + calFind.prop('resourcetype'); + calFind.prop(['calserver', 'getctag']); + calFind.depth = 1; + + + // found calendar home find calendars. + calFind.send(function(err, data) { + var url, name; + + for (url in data) { + if (data[url].resourcetype.value.indexOf('calendar') !== -1) { + name = data[url].displayname.value.value; + console.log('CAL RESOURCE:', name, '-', url); + } + } + console.log('DATA:'); + console.log(JSON.stringify(data)); + }); + +} function findProp(uri) { return new Propfind(con, { @@ -41,12 +69,6 @@ function findProp(uri) { }); } -// Have some url determine where the principal is -var findCal = findProp(); -findCal.prop('current-user-principal'); -findCal.prop('resourcetype'); -findCal.prop('principal-URL'); - function getProp(propName, obj, single) { var key, url, level, results = {}; @@ -65,41 +87,11 @@ function getProp(propName, obj, single) { return results; } -findCal.send(function(err, data) { - - // found principal find calendar home - var principaluri = getProp('current-user-principal', data, true); - console.log('PRINCIPAL:', principaluri); - var principalFind = findProp(principaluri); - - principalFind.prop('resourcetype'); - principalFind.prop(['caldav', 'calendar-home-set']); - - principalFind.send(function(err, data) { - var caluri = getProp('calendar-home-set', data, true); - var calFind = findProp(caluri); - console.log('CALENDAR_HOME:', caluri); - calFind.prop(['ical', 'calendar-color']); - calFind.prop('owner'); - calFind.prop('displayname'); - calFind.prop('resourcetype'); - calFind.prop(['calserver', 'getctag']); - calFind.depth = 1; - - - // found calendar home find calendars. - calFind.send(function(err, data) { - var url, name; - - for (url in data) { - if (data[url].resourcetype.value.indexOf('calendar') !== -1) { - name = data[url].displayname.value.value; - console.log('CAL RESOURCE:', name, '-', url); - } - } - console.log('DATA:'); - console.log(JSON.stringify(data)); - }); - }); +var home = new CalendarHome(con, { + url: config.uri +}); +home.send(function(err, data) { + console.log('CALENDAR_HOME:', data); + getCalendarDetails(data); }); diff --git a/test/caldav/request/calendar_home_test.js b/test/caldav/request/calendar_home_test.js new file mode 100644 index 0000000..525d407 --- /dev/null +++ b/test/caldav/request/calendar_home_test.js @@ -0,0 +1,35 @@ +requireRequest(); + +testSupport.lib('request/propfind'); +testSupport.lib('request/calendar_home'); + +suite('caldav/request/propfind', function() { + var Propfind; + var Connection; + var Home; + var subject; + var con; + + var url = 'http://google.com', + subject; + + suiteSetup(function() { + Propfind = Caldav.require('request/propfind'); + Connection = Caldav.require('connection'); + Home = Caldav.require('request/calendar_home'); + }); + + setup(function() { + con = new Connection(); + subject = new Home(con, { + url: url + }); + }); + + test('initialization', function() { + assert.equal(subject.url, url); + assert.equal(subject.connection, con); + }); + +}); + diff --git a/test/support/propfind.js b/test/support/propfind.js new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/support/propfind.js |