Skip to content

Commit

Permalink
Added Pinterest preset
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Sep 30, 2015
1 parent 757f3b1 commit e56fb5f
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Respecting quota limits when calling an API is the most common use case and ther
- [Google+](lib/server/core/presets/google-plus.js)
- [Instagram](lib/server/core/presets/instagram.js)
- [MailChimp](lib/server/core/presets/mailchimp.js)
- [Pinterest](lib/server/core/presets/pinterest.js)
- [StackExchange](lib/server/core/presets/stackexchange.js)
- [Twitter](lib/server/core/presets/twitter.js)
- [YouTube](lib/server/core/presets/youtube.js)
Expand Down
61 changes: 61 additions & 0 deletions lib/server/core/presets/pinterest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var Manager = require('../manager.js');
var _ = require('lodash');

/**
* Quota Preset for Pinterest's API
*
* Quota rules based on: https://developers.pinterest.com/docs/api/overview/
* Pinterest's API docs: https://developers.pinterest.com/docs/getting-started/introduction/
*
* In a cluster environment a local Server can be used if all requests made on
* behalf of a particular user are only made by a single node.js instance.
*
* @param options
* @returns {Manager}
*/
module.exports = function (options) {

var manager = new Manager({
backoff: 'timeout'
});

function addRule(resource) {

manager.addRule({
name: resource,
limit: 1000,
window: 60*60*1000,
throttling: 'window-sliding',
queueing: 'fifo',
scope: 'userId',
resource: resource
});

}

// Each unique client ID and user ID pair is limited to approximately 1000 calls per endpoint per hour.
addRule('v1/boards');
addRule('v1/boards/<board_id>');
addRule('v1/boards/<board_id>/pins');
addRule('v1/me');
addRule('v1/me/boards');
addRule('v1/me/followers');
addRule('v1/me/following/boards');
addRule('v1/me/following/boards/<board_id>');
addRule('v1/me/following/interests');
addRule('v1/me/following/interests/<interest_id>');
addRule('v1/me/following/users');
addRule('v1/me/following/users/<user_id>');
addRule('v1/me/likes');
addRule('v1/me/pins');
addRule('v1/me/search/boards');
addRule('v1/me/search/pins');
addRule('v1/pins');
addRule('v1/pins/<pin_id>');
addRule('v1/users/<username_or_id>');

return manager;

};
95 changes: 95 additions & 0 deletions test/spec/presets/pinterest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

var quota = require('../../../lib/index.js');

var _ = require('lodash');
var BPromise = require('bluebird');


describe('Preset Pinterest', function () {

it('should grant 1000 requests per endpoint', function () {

var quotaServer = new quota.Server();
quotaServer.addManager('pinterest');

var quotaClient = new quota.Client(quotaServer);

return BPromise.resolve()
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1000 });

})
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 }, { maxWait: 0 })
.then(function () {
throw new Error('Expected OutOfQuotaError');
})
.catch(quota.OutOfQuotaError, function (err) {
return; // Expected
});

})
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me/boards': 1000 });

})
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me/boards': 1 }, { maxWait: 0 })
.then(function () {
throw new Error('Expected OutOfQuotaError');
})
.catch(quota.OutOfQuotaError, function (err) {
return; // Expected
});

});

});

it('should allow updating the limit', function () {

var quotaServer = new quota.Server();
quotaServer.addManager('pinterest');

var quotaClient = new quota.Client(quotaServer);

return BPromise.resolve()
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 })
.then(function (grant) {
grant.dismiss({
forRule: {
'v1/me': {
limit: 2
}
}
});
});

})
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 });

})
.then(function () {

return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 }, { maxWait: 0 })
.then(function () {
throw new Error('Expected OutOfQuotaError');
})
.catch(quota.OutOfQuotaError, function (err) {
return; // Expected
});

});

});

});

0 comments on commit e56fb5f

Please sign in to comment.