Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.

Commit

Permalink
Complete 'Friends and Followers resources' section, add _getUsingCurs…
Browse files Browse the repository at this point in the history
…or() utility function
  • Loading branch information
jdub committed Jan 4, 2011
1 parent 18c19ee commit b8bc931
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions lib/twitter.js
Expand Up @@ -415,9 +415,15 @@ Twitter.prototype.getWeeklyTrends = function(params, callback) {
// List resources

Twitter.prototype.getLists = function(screen_name, params, callback) {
// FIXME: handle cursor stuff
if (typeof params === 'function') {
callback = params;
params = null;
}

params = merge(params, {key:'lists'});

var url = '/' + screen_name + '/lists.json';
this.get(url, params, callback);
this._getUsingCursor(url, params, callback);
return this;
}

Expand Down Expand Up @@ -531,6 +537,40 @@ Twitter.prototype.deleteDirectMessage

// Friends and Followers resources

Twitter.prototype.getFriendsIds = function(id, callback) {
if (typeof id === 'function') {
callback = id;
id = null;
}

var params = { key: 'ids' };
if (typeof id === 'string')
params.screen_name = id;
else if (typeof id === 'number')
params.user_id = id;

var url = '/friends/ids.json';
this._getUsingCursor(url, params, callback);
return this;
}

Twitter.prototype.getFollowersIds = function(id, callback) {
if (typeof id === 'function') {
callback = id;
id = null;
}

var params = { key: 'ids' };
if (typeof id === 'string')
params.screen_name = id;
else if (typeof id === 'number')
params.user_id = id;

var url = '/followers/ids.json';
this._getUsingCursor(url, params, callback);
return this;
}

// Account resources

Twitter.prototype.verifyCredentials = function(callback) {
Expand Down Expand Up @@ -584,3 +624,37 @@ Twitter.prototype.deleteFavorite
// Streamed Tweets resources

// Search resources


/*
* INTERNAL UTILITY FUNCTIONS
*/

Twitter.prototype._getUsingCursor = function(url, params, callback) {
var self = this,
key = params.key || null,
result = [];

// if we don't have a key to fetch, we're screwed
if (!key)
callback(new Error('FAIL: Results key must be provided to _getUsingCursor().'));
delete params.key;

// kick off the first request, using cursor -1
params = merge(params, {cursor:-1});
this.get(url, params, fetch);

function fetch(data) {
// FIXME: what if data[key] is not a list?
if (data[key]) result = result.concat(data[key]);

if (data.next_cursor_str === '0') {
callback(result);
} else {
params.cursor = data.next_cursor_str;
self.get(url, params, fetch);
}
}

return this;
}

0 comments on commit b8bc931

Please sign in to comment.