Permalink
Browse files

added transforms for dropbox.

  • Loading branch information...
1 parent ca9fafc commit 2dfe441d3fd1f5038d059beb4b10459ab618ee05 @AnalogJ AnalogJ committed Mar 1, 2014
Showing with 336 additions and 89 deletions.
  1. +140 −84 lib/dropbox/dropbox_client.js
  2. +110 −0 lib/dropbox/dropbox_transform.js
  3. +81 −0 test/dropbox/dropbox_transform_spec.js
  4. +5 −5 test/test_access_tokens.json
@@ -2,14 +2,14 @@ var Q = require('q')
, request = require('request')
, Dropbox = require("dropbox")
, winston = require('winston')
- , errorTypes = require('../errors.js')
+ , errorTypes = require('../errors.js')
, FFParameterRejected = errorTypes.FFParameterRejected
//http://coffeedoc.info/github/dropbox/dropbox-js/master/classes/Dropbox/Client.html#writeFile-instance
module.exports = function (oauth_data, provider_options) {
provider_options = provider_options || {}
- if(!oauth_data)
+ if (!oauth_data)
throw new FFParameterRejected("oauth_data cannot be null")
var _dropboxClientPromise = null
@@ -36,7 +36,7 @@ module.exports = function (oauth_data, provider_options) {
}
function errorHandler(error) {
- if(error){
+ if (error) {
switch (error.status) {
case Dropbox.ApiError.INVALID_TOKEN:
var FFTokenRejected = errorTypes.FFTokenRejected;
@@ -85,64 +85,92 @@ module.exports = function (oauth_data, provider_options) {
this.AccountInfo = function (options) {
options = options || {}
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.getAccountInfo(options || {}, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- })
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.getAccountInfo(options || {}, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseAccountInfo(response);
+ })
}
this.CheckQuota = function (options) {
options = options || {}
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.getAccountInfo(options || {}, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- })
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.getAccountInfo(options || {}, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseQuota(response);
+ })
}
this.CreateFile = function (fileName, parentIdentifier, content_buffer, options) {
options = options || {}
- if(!fileName){
+ if (!fileName) {
throw new FFParameterRejected("fileName cannot be emtpy.")
}
var path = createPath(fileName, parentIdentifier)
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.writeFile(path, content_buffer, options || {}, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- })
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.writeFile(path, content_buffer, options || {}, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseFileInformation(response);
+ })
}
- this.DeleteFile = function (identifier,options) {
+ this.DeleteFile = function (identifier, options) {
options = options || {}
- if(!identifier){
+ if (!identifier) {
throw new FFParameterRejected("identifier cannot be emtpy.")
}
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.remove(identifier, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- })
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.remove(identifier, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseDeletion(response);
+ })
}
- this.DownloadFile = function (identifier,options) {
+ this.DownloadFile = function (identifier, options) {
options = options || {}
- if(!identifier){
+ if (!identifier) {
throw new FFParameterRejected("identifier cannot be emtpy.")
}
return getClient().then(function (client) {
@@ -159,63 +187,91 @@ module.exports = function (oauth_data, provider_options) {
}
this.GetFileInformation = function (identifier, options) {
options = options || {}
- if(!identifier){
+ if (!identifier) {
throw new FFParameterRejected("identifier cannot be emtpy.")
}
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.stat(identifier, options || {}, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- })
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.stat(identifier, options || {}, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseFileInformation(response);
+ })
}
this.CreateFolder = function (folderName, parentIdentifier, options) {
options = options || {}
- if(!folderName){
+ if (!folderName) {
throw new FFParameterRejected("folderName cannot be emtpy.")
}
var path = createPath(folderName, parentIdentifier);
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.mkdir(path, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- });
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.mkdir(path, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseFolderInformation(response);
+ })
}
this.DeleteFolder = this.DeleteFile;
- this.GetFolderInformation = function(identifier, options){
+ this.GetFolderInformation = function (identifier, options) {
options = options || {}
identifier = identifier || '';
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.stat(identifier, options || {}, function (err, stat) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve(stat);
- });
- return deferred.promise;
- })
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.stat(identifier, options || {}, function (err, stat) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve(stat);
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseFolderInformation(response);
+ })
}
- this.RetrieveFolderItems = function (identifier,options) {
+ this.RetrieveFolderItems = function (identifier, options) {
options = options || {}
identifier = identifier || '';
var path = createPath(identifier)
- return getClient().then(function (client) {
- var deferred = Q.defer();
- client.readdir(path, function (err, content_array, folder_stat, content_stat_array) {
- err = errorHandler(err);
- if (err) return deferred.reject(err);
- return deferred.resolve({content_array: content_array, folder_stat: folder_stat, content_stat_array: content_stat_array});
- });
- return deferred.promise;
- });
+ return getClient()
+ .then(function (client) {
+ var deferred = Q.defer();
+ client.readdir(path, function (err, content_array, folder_stat, content_stat_array) {
+ err = errorHandler(err);
+ if (err) return deferred.reject(err);
+ return deferred.resolve({content_array: content_array, folder_stat: folder_stat, content_stat_array: content_stat_array});
+ });
+ return deferred.promise;
+ })
+ .then(function (response) {
+ var raw_response = options.raw_response || provider_options.raw_response;
+ if (raw_response)
+ return response;
+ return require('./dropbox_transform.js').parseFolderItems(response);
+ })
}
};
Oops, something went wrong.

0 comments on commit 2dfe441

Please sign in to comment.