aboutsummaryrefslogtreecommitdiffstats
path: root/lib/caldav/connection.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/caldav/connection.js')
-rw-r--r--lib/caldav/connection.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/caldav/connection.js b/lib/caldav/connection.js
new file mode 100644
index 0000000..953d405
--- /dev/null
+++ b/lib/caldav/connection.js
@@ -0,0 +1,99 @@
+(function(module, ns) {
+
+ var XHR = ns.require('xhr');
+
+ /**
+ * Connection objects contain
+ * general information to be reused
+ * across XHR requests.
+ *
+ * Also handles normalization of path details.
+ */
+ function Connection(options) {
+ if (typeof(options) === 'undefined') {
+ options = {};
+ }
+
+ var key;
+
+ for (key in options) {
+ if (options.hasOwnProperty(key)) {
+ this[key] = options[key];
+ }
+ }
+
+ var domain = options.domain;
+
+ if (domain) {
+ if (domain.substr(-1) === '/') {
+ this.domain = domain.substr(0, domain.length - 1);
+ }
+ }
+
+ }
+
+ Connection.prototype = {
+ /**
+ * Default username for requests.
+ */
+ user: '',
+
+ /**
+ * Default passwords for requests.
+ */
+ password: '',
+
+ /**
+ * Default domain for requests.
+ */
+ domain: '',
+
+ /**
+ * Creates new XHR request based on default
+ * options for connection.
+ *
+ * @return {Caldav.Xhr} http request set with default options.
+ */
+ request: function(options) {
+ if (typeof(options) === 'undefined') {
+ options = {};
+ }
+
+ var copy = {};
+ var key;
+ // copy options
+
+ for (key in options) {
+ copy[key] = options[key];
+ }
+
+ if (!copy.user) {
+ copy.user = this.user;
+ }
+
+ if (!copy.password) {
+ copy.password = this.password;
+ }
+
+ if (copy.url && copy.url.indexOf('http') !== 0) {
+ var url = copy.url;
+ if (url.substr(0, 1) !== '/') {
+ url = '/' + url;
+ }
+ copy.url = this.domain + url;
+ }
+
+ return new XHR(copy);
+ }
+
+ };
+
+ console.log('!HIT!!');
+ module.exports = Connection;
+
+}.apply(
+ this,
+ (this.Caldav) ?
+ [Caldav('connection'), Caldav] :
+ [module, require('./caldav')]
+));