diff options
author | James Lal <james@lightsofapollo.com> | 2012-07-06 14:10:11 -0700 |
---|---|---|
committer | James Lal <james@lightsofapollo.com> | 2012-07-06 14:10:11 -0700 |
commit | 7f30ad85d5415e8fb3bbbf729ac090d453205372 (patch) | |
tree | d51ccc995aa1ad6a80df03a644bf9922bdaea289 /lib/caldav/connection.js | |
parent | e6fab580162a1cae2165ef88e254df284e6c7208 (diff) | |
download | jsCalDAV-7f30ad85d5415e8fb3bbbf729ac090d453205372.tar.gz |
Adding connection
Diffstat (limited to 'lib/caldav/connection.js')
-rw-r--r-- | lib/caldav/connection.js | 99 |
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')] +)); |