Skip to content

Commit

Permalink
Moving some functions from public to private, started on validation, …
Browse files Browse the repository at this point in the history
…and added new unit tests
  • Loading branch information
rv-emendoza committed Nov 15, 2016
1 parent 2b7f9f0 commit acb7f33
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 184 deletions.
10 changes: 5 additions & 5 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
var Behance = require('../index.js');

// Create an Instance of Behance with API Key
var key = require('../api.json').key;
var key = 1234;
var Be = new Behance(key);

var fs = require('fs');

// Get Projects
// Be.projects({q: 'motorcycle'}, function(err, res, data) {
// console.dir(data);
// fs.writeFile('../test/api-responses/projects.json', JSON.stringify(data, null, 4));
// });
Be.projects({q: 'motorcycle'}, function(err, res, data) {
console.dir(err);
// fs.writeFile('../test/api-responses/projects.json', JSON.stringify(data, null, 4));
});

// Get Project
// Be.project('4889175', function(err, res, data) {
Expand Down
127 changes: 77 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,95 @@
'use strict';

// Dependencies
var request = require('request'),
q = require('querystring');


/**
* Create an instance of Behance
* @param {string} token - authentication for Behance API
*/
var Behance = function(token) {
this.clientId = token;

// Throw an error if Auth Key is not specified
if (this.clientId === undefined) {
throw new Error('Please supply an authorization token.');
}
};

const
request = require('request'),
q = require('querystring'),
queryValidation = require('./libs/query-validation.json');

/**
* Endpoint and Query Builder
* @param {string} endpoint - endpoint to query
* @param {object} options - Queries
* @api private
*/
Behance.prototype.buildUrl = function(endpoint, options) {
var _this = this;
var query = '?' + (options ? q.stringify(options) + '&' : ''),
clientId = 'client_id=' + _this.clientId;
function requestUrl(endpoint, token, options) {
let _this = this,
query = '?' + (options ? q.stringify(options) + '&' : ''),
clientId = 'client_id=' + token;

return 'https://api.behance.net/v2/' + endpoint + query + clientId;
};


/**
* Request Handler
* @param {string} requestUrl - Requested Url
* @param {string} url - Requested Url
* @param {function} cb - callback
* @api private
*/
Behance.prototype.requestHandler = function(requestUrl, cb) {
request(requestUrl, function(err, res, body) {
if (!body) {
return cb(new Error('No response from the Behance API'));
function requestHandler(url, cb) {
request(url, function(err, res, data) {

if (res.statusCode === 403) {
return cb(Error('No response from the Behance API'));
}

try {
body = JSON.parse(body);
data = JSON.parse(data);
} catch(e) {}

cb(err, res, body);
return cb(err, res, data);
});
};
}

/**
* Compare Keys
* @param {object} obj1 - object to compare
* @param {object} obj2 - object to compare against
* @return {bool}
* @api private
*/
function compareKeys(obj1, obj2, fn) {
for (var prop in obj1) {
if (!obj2.hasOwnProperty(prop)) {
throw Error('property ' + prop + ' is not a valid query for ' + fn);
} else {
return true;
}
}
}

/**
* Create an instance of Behance
* @param {string} token - authentication for Behance API
*/
var Behance = function(token) {
this.clientId = token;

// Throw an error if Auth Key is not specified
if (this.clientId === undefined) {
throw Error('Please supply an authorization token.');
}
};

/**
* Endpoints that only support Options
*/
var endpointWithOptionOnly = [{
const endpointWithOptionOnly = [{
name: 'projects',
path: 'projects'
path: 'projects',
queries: queryValidation.projects
}, {
name: 'creativesToFollow',
path: 'creativestofollow'
path: 'creativestofollow',
queries: queryValidation.creativesToFollow
}, {
name: 'users',
path: 'users'
path: 'users',
queries: queryValidation.users
}, {
name: 'collections',
path: 'collections'
path: 'collections',
queries: queryValidation.collections
}];

endpointWithOptionOnly.forEach(function(def) {
Expand All @@ -79,16 +99,19 @@ endpointWithOptionOnly.forEach(function(def) {
* @param {function} cb - callback
*/
Behance.prototype[def.name] = function(opts, cb) {
var endpoint = def.path;
this.requestHandler(this.buildUrl(endpoint, opts), cb);
let _this = this,
endpoint = def.path;

if (compareKeys(opts, def.queries, def.name)) {
requestHandler(requestUrl(endpoint, _this.clientId, opts), cb);
}
};
});


/**
* Endpoints that require an ID with no Options
*/
var endpointWithOnlyAnId = [{
const endpointWithOnlyAnId = [{
name: 'project',
pathprefix: 'projects/'
}, {
Expand All @@ -114,20 +137,21 @@ endpointWithOnlyAnId.forEach(function(def) {
* @param {function} cb - callback
*/
Behance.prototype[def.name] = function(id, cb) {
let _this = this,
endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : '');

if (arguments.length !== 2) {
throw new Error('.' + def.name + ' requires both an id and a callback function.');
throw Error('.' + def.name + ' requires both an id and a callback function.');
}

var endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : '');
this.requestHandler(this.buildUrl(endpoint), cb);
requestHandler(requestUrl(endpoint, _this.clientId), cb);
};
});


/**
* Endpoints that require an ID and support Options
*/
var endpointWithIdAndOptions = [{
const endpointWithIdAndOptions = [{
name: 'projectComments',
pathprefix: 'projects/',
pathsuffix: '/comments'
Expand Down Expand Up @@ -169,21 +193,24 @@ endpointWithIdAndOptions.forEach(function(def) {
* @param {function} cb - callback
*/
Behance.prototype[def.name] = function(id, opts, cb) {
let _this = this,
endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : '');

if (arguments.length < 2) {
throw new Error('.' + def.name + ' requires at least an id and a callback function.');
throw Error('.' + def.name + ' requires at least an id and a callback function.');
}
var endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : '');
this.requestHandler(this.buildUrl(endpoint, opts), cb);

requestHandler(requestUrl(endpoint, _this.clientId, opts), cb);
};
});


/**
* Get Creative Fields
*/
Behance.prototype.fields = function(cb) {
var endpoint = 'fields';
this.requestHandler(this.buildUrl(endpoint), cb);
let _this = this,
endpoint = 'fields';
requestHandler(requestUrl(endpoint, _this.clientId), cb);
};

module.exports = Behance;
35 changes: 35 additions & 0 deletions libs/query-validation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"projects": {
"q": "",
"sort": "",
"time": "",
"field": "",
"country": "",
"state": "",
"city": "",
"page": "",
"tags": "",
"color_hex": "",
"color_range": "",
"license": ""
},
"creativesToFollow": {
"page": ""
},
"users": {
"q": "",
"field": "",
"country": "",
"state": "",
"city": "",
"page": "",
"sort": "",
"tags": ""
},
"collections": {
"q": "",
"time": "",
"page": "",
"sort": ""
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"coveralls": "^2.11.14",
"istanbul": "^0.4.5",
"mocha": "^3.1.2",
"nock": "^9.0.0"
"nock": "^9.0.0",
"rewire": "^2.5.2"
}
}

0 comments on commit acb7f33

Please sign in to comment.