Skip to content

Commit

Permalink
Merge commit 'af34614d68f4de6c9f05166bad69a9922885817d'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins User committed Mar 27, 2018
2 parents 19e22ab + af34614 commit 0b31651
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
35 changes: 17 additions & 18 deletions lib/Pipedrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* (MIT License)
*/

(function() {
(function () {
'use strict';

var _ = require('lodash'),
Expand All @@ -19,11 +19,11 @@
CollectionItem = require(__dirname + '/CollectionItem'),
wrapCollectionItems = require(__dirname + '/wrapCollectionItems');

exports.authenticate = function(auth, callback) {
exports.authenticate = function (auth, callback) {
var client = new exports.Client('unauthorized-client-nodejs'),
restHandlers = new RestHandlers();

return restHandlers.listItems('authorizations', auth, function(error, data, additionalData) {
return restHandlers.listItems('authorizations', auth, function (error, data, additionalData) {
var collectionItems = wrapCollectionItems(data, 'authorizations', {});
callback(error, collectionItems, additionalData);
}, false);
Expand Down Expand Up @@ -55,38 +55,37 @@
this.removeAllListeners = listener.removeAllListeners;
}

_.each(blueprint.apiObjects, function(item) {
that[item.substr(0,1).toUpperCase() + item.substr(1)] = new Collection(item, _.extend({}, options, {apiToken: apiToken}));
_.each(blueprint.apiObjects, function (item) {
that[item.substr(0, 1).toUpperCase() + item.substr(1)] = new Collection(item, _.extend({}, options, {apiToken: apiToken}));
});

return this;
}

Client.prototype.getAll = function(resource, params, callback) {
Client.prototype.getAll = function (resource, params, callback) {
// params was added later so ensure backwards compatibility
if (_.isFunction(params) && _.isUndefined(callback)) {
callback = params;
params = {};
}

if (!this[resource]) {
return callback(new Error(resource + ' is not supported object type for getAll()'));
}

var collection = [],
self = this,
page = 0,
perPage = 50;

function fetch(page) {
var start = page * perPage;
if (!self[resource]) {
throw new Error(resource+' is not supported object type for getAll()');
}
var pageParams = _.extend({}, params, {
start: start,
limit: perPage
});
if (!self[resource]) {
throw new Error(resource+' is not supported object type for getAll()');
}
self[resource].getAll(pageParams, function(err, models) {
var start = page * perPage,
pageParams = _.extend({}, params, {
start: start,
limit: perPage
});

self[resource].getAll(pageParams, function (err, models) {
if (err) {
callback(err);
} else {
Expand Down
42 changes: 29 additions & 13 deletions test/unit/client.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
var should = require('should');
var should = require('should/as-function'),
Pipedrive = require('./../..'),
blueprint = require('./../../lib/blueprint');

describe('client module', function() {
describe('client module', function () {

it('should expose some of the main object types', function() {
var Pipedrive = require('./../..'),
pipedrive = new Pipedrive.Client('apitoken'),
blueprint = require('./../../lib/blueprint');
beforeEach(function () {
client = new Pipedrive.Client('apitoken');
strictClient = new Pipedrive.Client('apitoken', {strictMode: true});
});

it('should expose main API objects', function () {
should(client.Activities).be.an.Object();
should(client.Deals).be.an.Object();

// iterate through all top level objects which should be exposed, e.g. pipedrive.Deals, ...
blueprint.apiObjects.forEach(function(obj) {
should(pipedrive[obj.substr(0,1).toUpperCase() + obj.substr(1)]).be.an.Object();
// iterate through all
blueprint.apiObjects.forEach(function (obj) {
should(client[obj.substr(0, 1).toUpperCase() + obj.substr(1)]).be.an.Object();
});
});

it('should expose .on() listener in strict mode', function() {
var Pipedrive = require('./../..'),
pipedrive = new Pipedrive.Client('apitoken', { strictMode: true });
describe('client.on()', function () {
it('should be defined in strict mode', function () {
should(strictClient.on).be.a.Function();
});

should(pipedrive.on).be.a.Function();
it('should not be defined in regular mode', function () {
should(client.on).be.a.Undefined();
});
});

it('client.getAll() should throw error if non-existant resource is requested', function (done) {
client.getAll('bananas', function (error) {
should(error.message).equal('bananas is not supported object type for getAll()');
return done();
});
});
});

0 comments on commit 0b31651

Please sign in to comment.