aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-07-07 16:33:27 -0700
committerJames Lal <james@lightsofapollo.com>2012-07-07 16:33:27 -0700
commitee8dac6ecd2927d6ea1930ae478ff65766b446d9 (patch)
treef6482199d2a3fd6d57ee60e4ca659ebe9a82da4e
parentccf8b1382cd6a72abc93b837e5c39272df9c2d72 (diff)
downloadjsCalDAV-ee8dac6ecd2927d6ea1930ae478ff65766b446d9.tar.gz
resources request
-rw-r--r--lib/caldav/index.js1
-rw-r--r--lib/caldav/request/index.js3
-rw-r--r--lib/caldav/request/resources.js69
-rw-r--r--lib/caldav/resource_root.js14
-rw-r--r--lib/caldav/resources/index.js13
-rwxr-xr-xscripts/connect13
-rw-r--r--test/caldav/request/resources_test.js103
-rw-r--r--test/caldav/resource_root_test.js5
8 files changed, 196 insertions, 25 deletions
diff --git a/lib/caldav/index.js b/lib/caldav/index.js
index df7dfa5..e079028 100644
--- a/lib/caldav/index.js
+++ b/lib/caldav/index.js
@@ -10,6 +10,7 @@
exports.Request = ns.require('request');
exports.Templates = ns.require('templates');
exports.Connection = ns.require('connection');
+ exports.Resources = ns.require('resources');
}.apply(
this,
diff --git a/lib/caldav/request/index.js b/lib/caldav/request/index.js
index 6790fd1..6f8c23d 100644
--- a/lib/caldav/request/index.js
+++ b/lib/caldav/request/index.js
@@ -4,7 +4,8 @@
Abstract: ns.require('request/abstract'),
CalendarQuery: ns.require('request/calendar_query'),
Propfind: ns.require('request/propfind'),
- CalendarHome: ns.require('request/calendar_home')
+ CalendarHome: ns.require('request/calendar_home'),
+ Resources: ns.require('request/resources')
};
}.apply(
diff --git a/lib/caldav/request/resources.js b/lib/caldav/request/resources.js
new file mode 100644
index 0000000..1df7cf1
--- /dev/null
+++ b/lib/caldav/request/resources.js
@@ -0,0 +1,69 @@
+(function(module, ns) {
+
+ var Propfind = ns.require('request/propfind');
+
+ function ResourceFinder(connection, options) {
+ Propfind.apply(this, arguments);
+
+ this._resources = {};
+ this.depth = 1;
+ }
+
+ ResourceFinder.prototype = {
+ __proto__: Propfind.prototype,
+
+ addResource: function(name, handler) {
+ this._resources[name] = handler;
+ },
+
+ _processResult: function(req, callback) {
+ var results = {};
+ var url;
+ var root;
+ var collection;
+ var self = this;
+
+ if ('multistatus' in this.sax.root) {
+ root = this.sax.root.multistatus;
+
+ for (url in root) {
+ collection = root[url];
+
+ if ('resourcetype' in collection) {
+ collection.resourcetype.forEach(function(type) {
+ if (type in self._resources) {
+
+ if (!(type in results)) {
+ results[type] = {};
+ }
+
+ results[type][url] = new self._resources[type](
+ self.connection,
+ collection
+ );
+ }
+ });
+ }
+ }
+
+ callback(null, results, req);
+
+ } else {
+ //XXX: Improve error handling
+ callback(
+ new Error('unexpected xml result'),
+ this.sax.root, req
+ );
+ }
+ }
+
+ };
+
+ module.exports = ResourceFinder;
+
+}.apply(
+ this,
+ (this.Caldav) ?
+ [Caldav('request/resources'), Caldav] :
+ [module, require('../caldav')]
+));
diff --git a/lib/caldav/resource_root.js b/lib/caldav/resource_root.js
deleted file mode 100644
index 0cff9b7..0000000
--- a/lib/caldav/resource_root.js
+++ /dev/null
@@ -1,14 +0,0 @@
-(function(module, ns) {
-
- function ResourceRoot() {
-
- }
-
- module.exports = ResourceRoot;
-
-}.apply(
- this,
- (this.Caldav) ?
- [Caldav('resource_root'), Caldav] :
- [module, require('./caldav')]
-));
diff --git a/lib/caldav/resources/index.js b/lib/caldav/resources/index.js
new file mode 100644
index 0000000..e152458
--- /dev/null
+++ b/lib/caldav/resources/index.js
@@ -0,0 +1,13 @@
+(function(module, ns) {
+
+ module.exports = {
+ Calendar: ns.require('resources/calendar')
+ };
+
+}.apply(
+ this,
+ (this.Caldav) ?
+ [Caldav('resources'), Caldav] :
+ [module, require('../caldav')]
+));
+
diff --git a/scripts/connect b/scripts/connect
index 050df1b..b5d12cb 100755
--- a/scripts/connect
+++ b/scripts/connect
@@ -35,6 +35,7 @@ var con = new CalDav.Connection({
var Propfind = CalDav.Request.Propfind;
var CalendarHome = CalDav.Request.CalendarHome;
+var Calendar = CalDav.Resources.Calendar;
function getCalendarDetails(caluri) {
@@ -52,16 +53,18 @@ function getCalendarDetails(caluri) {
// found calendar home find calendars.
calFind.send(function(err, data) {
- var url, name;
+ var url, name, calendars = {};
for (url in data) {
if (data[url].resourcetype.value.indexOf('calendar') !== -1) {
- name = data[url].displayname.value.value;
- console.log('CAL RESOURCE:', name, '-', url);
+ calendars[url] = new Calendar(con, { url: url });
+ calendars[url].updateFromServer(data[url]);
+
+ console.log('CAL RESOURCE:', calendars[url]);
}
}
- console.log('DATA:');
- console.log(JSON.stringify(data));
+ //console.log('DATA:');
+ //console.log(JSON.stringify(data));
//console.log('RAW:');
//console.log(calFind.xhr.xhr.responseText);
});
diff --git a/test/caldav/request/resources_test.js b/test/caldav/request/resources_test.js
new file mode 100644
index 0000000..c8a8675
--- /dev/null
+++ b/test/caldav/request/resources_test.js
@@ -0,0 +1,103 @@
+requireRequest();
+testSupport.lib('request/propfind');
+testSupport.lib('request/resources');
+testSupport.helper('mock_request');
+
+suite('caldav/resource_finder', function() {
+
+ var Connection;
+ var MockRequest;
+ var MockPropfind;
+ var Propfind;
+ var Finder;
+ var subject;
+ var con;
+
+ var url = 'http://google.com',
+ subject;
+
+ suiteSetup(function() {
+ Connection = Caldav.require('connection');
+ Finder = Caldav.require('request/resources');
+ Propfind = Caldav.require('request/propfind');
+ MockRequest = Caldav.require('support/mock_request');
+ });
+
+ suiteSetup(function() {
+ MockPropfind = MockRequest.create(['prop']);
+ });
+
+ setup(function() {
+ MockPropfind.reset();
+
+ con = new Connection();
+ subject = new Finder(con, {
+ url: url,
+ Propfind: MockPropfind
+ });
+
+ });
+
+ test('initializer', function() {
+ assert.equal(subject.url, url);
+ assert.equal(subject.connection, con);
+ assert.deepEqual(subject._resources, {});
+ assert.instanceOf(subject, Propfind);
+ assert.equal(subject.depth, 1);
+ });
+
+ test('#addResource', function() {
+ var fn = function() {};
+
+ subject.addResource('foo', fn);
+
+ assert.equal(subject._resources['foo'], fn);
+ });
+
+ suite('#_processResult', function() {
+
+ var Handler = function() {
+ this.args = arguments;
+ };
+
+ function status(value, status) {
+ if (typeof(status) === 'undefined') {
+ status = '200';
+ }
+
+ return { value: value, status: status };
+ }
+
+ function resource(name, type) {
+ return {
+ name: status(name),
+ resourcetype: [type]
+ };
+ }
+
+ setup(function() {
+ subject.addResource('calendar', Handler);
+ });
+
+ test('handled & unhandled resource', function() {
+ var result;
+ var input = {
+ 'a': resource('1', 'calendar'),
+ 'b': resource('2', 'other')
+ };
+
+ subject.sax.root = { multistatus: input };
+ subject._processResult(null, function(err, data) {
+ result = data;
+ });
+
+ assert.ok(result.calendar['a']);
+ assert.instanceOf(result.calendar['a'], Handler);
+ assert.equal(Object.keys(result.calendar).length, 1);
+
+ assert.equal(result.calendar['a'].args[1].name.value, '1');
+ });
+
+ });
+
+});
diff --git a/test/caldav/resource_root_test.js b/test/caldav/resource_root_test.js
deleted file mode 100644
index 4af8286..0000000
--- a/test/caldav/resource_root_test.js
+++ /dev/null
@@ -1,5 +0,0 @@
-testSupport.lib('template');
-
-suite('resource_root', function() {
-
-});