From 7f30ad85d5415e8fb3bbbf729ac090d453205372 Mon Sep 17 00:00:00 2001 From: James Lal Date: Fri, 6 Jul 2012 14:10:11 -0700 Subject: Adding connection --- lib/caldav/caldav.js | 1 + lib/caldav/connection.js | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/caldav/index.js | 1 + 3 files changed, 101 insertions(+) create mode 100644 lib/caldav/connection.js (limited to 'lib/caldav') diff --git a/lib/caldav/caldav.js b/lib/caldav/caldav.js index 065b815..f0afe0a 100644 --- a/lib/caldav/caldav.js +++ b/lib/caldav/caldav.js @@ -42,6 +42,7 @@ * Maps exports to a file path. */ set exports(val) { + console.log(paths); return paths[this.path] = val; }, 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')] +)); diff --git a/lib/caldav/index.js b/lib/caldav/index.js index b54a9ac..df7dfa5 100644 --- a/lib/caldav/index.js +++ b/lib/caldav/index.js @@ -9,6 +9,7 @@ exports.Xhr = ns.require('xhr'); exports.Request = ns.require('request'); exports.Templates = ns.require('templates'); + exports.Connection = ns.require('connection'); }.apply( this, -- cgit