aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJames Lal <james@lightsofapollo.com>2012-06-26 07:21:43 +0200
committerJames Lal <james@lightsofapollo.com>2012-06-26 07:21:43 +0200
commit646e0b6991c9e85de9b8eee20e601079fb128429 (patch)
treecdd2a21d7816936556eab9f99dd32e8914c2a426 /lib
parent96683161e43fc0101c74f0875d1a2d445afe7e8d (diff)
downloadjsCalDAV-646e0b6991c9e85de9b8eee20e601079fb128429.tar.gz
abstract request
Diffstat (limited to 'lib')
-rw-r--r--lib/webcals/request/abstract.js65
-rw-r--r--lib/webcals/xhr.js2
2 files changed, 65 insertions, 2 deletions
diff --git a/lib/webcals/request/abstract.js b/lib/webcals/request/abstract.js
index 7d31540..fe47de9 100644
--- a/lib/webcals/request/abstract.js
+++ b/lib/webcals/request/abstract.js
@@ -1,9 +1,72 @@
(function(module, ns) {
- function Abstract() {
+ var SAX = ns.require('sax');
+ var XHR = ns.require('xhr');
+
+ /**
+ * Creates an (Web/Cal)Dav request.
+ *
+ * @param {String} url location of resource.
+ * @param {Object} options additional options for request.
+ */
+ function Abstract(url, options) {
+ var key;
+
+ if (typeof(url) === 'undefined' || !url) {
+ throw new Error('request requires a url');
+ }
+
+ this.url = url;
+ this.sax = new SAX();
+
+ for (key in options) {
+ if (options.hasOwnProperty(key)) {
+ this[key] = options[key];
+ }
+ }
}
+ Abstract.prototype = {
+
+ _createXhr: function() {
+ return new XHR({
+ url: this.url
+ });
+ },
+
+ _createPayload: function() {
+ return '';
+ },
+
+ /**
+ * Sends request to server.
+ *
+ * @param {Function} callback node style callback.
+ * Receives three arguments
+ * error, parsedData, xhr.
+ */
+ send: function(callback) {
+ var self = this;
+ var req = this.xhr = this._createXhr();
+ req.data = this._createPayload();
+
+ // in the future we may stream data somehow
+ req.send(function xhrResult() {
+ var xhr = req.xhr;
+ if (xhr.status > 199 && xhr.status < 300) {
+ // success
+ self.sax.write(xhr.responseText).close();
+
+ callback(null, self.sax.root, req);
+ } else {
+ // fail
+ callback(new Error('http error code: ' + xhr.status));
+ }
+ });
+ }
+ };
+
module.exports = Abstract;
}.apply(
diff --git a/lib/webcals/xhr.js b/lib/webcals/xhr.js
index f049c3f..f20415f 100644
--- a/lib/webcals/xhr.js
+++ b/lib/webcals/xhr.js
@@ -92,7 +92,7 @@
if (xhr.readyState === 4) {
data = xhr.responseText;
this.waiting = false;
- callback(data, xhr);
+ callback(null, xhr);
}
}.bind(this);