Code coverage report for kobold-core/lib/connectionAdapter/connectionAdapter.js

Statements: 40.74% (11 / 27)      Branches: 50% (2 / 4)      Functions: 23.53% (4 / 17)      Lines: 40.74% (11 / 27)      Ignored: none     

All files » kobold-core/lib/connectionAdapter/ » connectionAdapter.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229      1 1 1             1                         30   30 30   30                                                                                               32                   35                                                                                                                                                                                                                                                                                     1  
// Copyright 2014, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
 
var Base = require('preceptor-core').Base;
var Promise = require('promise');
var request = require('request');
 
/**
 * Abstract connection adapter
 *
 * @class ConnectionAdapter
 */
var ConnectionAdapter = Base.extend(
 
	/**
	 * Connection adapter constructor
	 *
	 * @constructor
	 * @class ConnectionAdapter
	 * @param {object} [options]
	 *
	 * @property {object} _options
	 * @property {Promise} _promise
	 */
	function (options) {
		this.__super();
 
		this._options = options || {};
		this.setPromise(Promise.resolve());
 
		this.initialize();
	},
 
	/** @lends ConnectionAdapter.prototype */
	{
		/**
		 * Initializes the source-adapter
		 *
		 * @method initialize
		 */
		initialize: function () {
			// Nothing by default
		},
 
 
		/**
		 * Makes a request and returns a promise
		 *
		 * @method _request
		 * @param {object} options
		 * @param {string} options.url Url of host
		 * @param {string} options.method Method of request
		 * @param {object} [options.headers] Request headers
		 * @param {string|Buffer} [options.body] Body of request
		 * @return {Promise} With {object} Response
		 * @private
		 */
		_request: function (options) {
			return new Promise(function (resolve, reject) {
				request(options, function (err, response, body) {
					if (err) {
						reject(err);
					} else {
						response.body = body;
						resolve(response);
					}
				});
			});
		},
 
 
		/**
		 * Gets the source-adapter promise
		 *
		 * @method getPromise
		 * @return {Promise}
		 */
		getPromise: function () {
			return this._promise;
		},
 
		/**
		 * Sets the promise
		 *
		 * @method setPromise
		 * @param {Promise} promise
		 */
		setPromise: function (promise) {
			this._promise = promise;
		},
 
 
		/**
		 * Gets all bucket names
		 *
		 * @method getBuckets
		 * @param {function} [filterFn]
		 * @return {Promise} With {string[]} List of buckets
		 */
		getBuckets: function (filterFn) {
			throw new Error('Unimplemented adapter function "getBuckets".');
		},
 
		/**
		 * Does the instance have a specific bucket?
		 *
		 * @method hasBucket
		 * @param {string} bucket
		 * @param {function} [filterFn]
		 * @return {Promise} With {boolean} Bucket exists?
		 */
		hasBucket: function (bucket, filterFn) {
			throw new Error('Unimplemented adapter function "hasBucket".');
		},
 
		/**
		 * Gets all bucket keys
		 *
		 * @method getBucketKeys
		 * @param {string} bucket
		 * @param {function} [filterFn]
		 * @return {Promise} With {string[]} Bucket keys
		 */
		getBucketKeys: function (bucket, filterFn) {
			throw new Error('Unimplemented adapter function "getBucketKeys".');
		},
 
		/**
		 * Does the instance have a specific key in a bucket?
		 *
		 * @method hasBucketKey
		 * @param {string} bucket
		 * @param {string} key
		 * @param {function} [filterFn]
		 * @return {Promise} With {boolean} Bucket key exists?
		 */
		hasBucketKey: function (bucket, key, filterFn) {
			throw new Error('Unimplemented adapter function "hasBucketKey".');
		},
 
		/**
		 * Gets an object
		 *
		 * @method getObject
		 * @param {string} bucket
		 * @param {string} key
		 * @return {Promise} With {Buffer}
		 */
		getObject: function (bucket, key) {
			throw new Error('Unimplemented adapter function "getObject".');
		},
 
		/**
		 * Gets an object as JSON
		 *
		 * @method getObjectAsJSON
		 * @param {string} bucket
		 * @param {string} key
		 * @return {Promise} With {*}
		 */
		getObjectAsJSON: function (bucket, key) {
			throw new Error('Unimplemented adapter function "getObjectAsJSON".');
		},
 
 
		/**
		 * Sets an object
		 *
		 * @method setObject
		 * @param {string} bucket
		 * @param {string} key
		 * @param {Buffer|string} data
		 * @param {string} [mimeType='application/octet-stream']
		 * @return {Promise}
		 */
		setObject: function (bucket, key, data, mimeType) {
			throw new Error('Unimplemented adapter function "setObject".');
		},
 
		/**
		 * Sets an object from JSON
		 *
		 * @method setObjectFromJSON
		 * @param {string} bucket
		 * @param {string} key
		 * @param {*} data
		 * @return {Promise}
		 */
		setObjectFromJSON: function (bucket, key, data) {
			throw new Error('Unimplemented adapter function "setObjectFromJSON".');
		},
 
 
		/**
		 * Removes an object
		 *
		 * @method removeObject
		 * @param {string} bucket
		 * @param {string} key
		 * @return {Promise}
		 */
		removeObject: function (bucket, key) {
			throw new Error('Unimplemented adapter function "removeObject".');
		},
 
		/**
		 * Removes all object of a bucket
		 *
		 * @method removeAllObjects
		 * @param {string} bucket
		 * @return {Promise}
		 */
		removeAllObjects: function (bucket) {
			throw new Error('Unimplemented adapter function "removeAllObjects".');
		}
	},
 
	{
		/**
		 * Type of class
		 *
		 * @property TYPE
		 * @type string
		 */
		TYPE: 'ConnectionAdapter'
	});
 
module.exports = ConnectionAdapter;