Skip to content

Commit

Permalink
Merge pull request github-tools#82 from raphink/dev/auto-paging
Browse files Browse the repository at this point in the history
Auto paging
  • Loading branch information
mattpass committed Oct 23, 2013
2 parents 7b45b4d + 1599cbc commit 6d5f502
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions github.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

function _request(method, path, data, cb, raw, sync) {
function getURL() {
var url = API_URL + path;
var url = path.indexOf('//') >= 0 ? path : API_URL + path;
return url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
}

Expand All @@ -47,9 +47,9 @@
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300 || this.status === 304) {
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true);
cb(null, raw ? this.responseText : this.responseText ? JSON.parse(this.responseText) : true, this);
} else {
cb({request: this, error: this.status});
cb({path: path, request: this, error: this.status});
}
}
}
Expand All @@ -66,14 +66,42 @@
if (sync) return xhr.response;
}

function _requestAllPages(path, cb) {
var results = [];
(function iterate() {
_request("GET", path, null, function(err, res, xhr) {
if (err) {
return cb(err);
}

results.push.apply(results, res);

var links = (xhr.getResponseHeader('link') || '').split(/\s*,\s*/g),
next = _.find(links, function(link) { return /rel="next"/.test(link); });

if (next) {
next = (/<(.*)>/.exec(next) || [])[1];
}

if (!next) {
cb(err, results);
} else {
path = next;
iterate();
}
});
})();
}



// User API
// =======

Github.User = function() {
this.repos = function(cb) {
_request("GET", "/user/repos?type=all&per_page=1000&sort=updated", null, function(err, res) {
// Github does not always honor the 1000 limit so we want to iterate over the data set.
_requestAllPages("/user/repos?type=all&per_page=1000&sort=updated", function(err, res) {
cb(err, res);
});
};
Expand Down Expand Up @@ -120,7 +148,8 @@
// -------

this.userRepos = function(username, cb) {
_request("GET", "/users/"+username+"/repos?type=all&per_page=1000&sort=updated", null, function(err, res) {
// Github does not always honor the 1000 limit so we want to iterate over the data set.
_requestAllPages("/users/"+username+"/repos?type=all&per_page=1000&sort=updated", function(err, res) {
cb(err, res);
});
};
Expand All @@ -138,7 +167,8 @@
// -------

this.orgRepos = function(orgname, cb) {
_request("GET", "/orgs/"+orgname+"/repos?type=all&per_page=1000&sort=updated&direction=desc", null, function(err, res) {
// Github does not always honor the 1000 limit so we want to iterate over the data set.
_requestAllPages("/orgs/"+orgname+"/repos?type=all&&page_num=1000&sort=updated&direction=desc", function(err, res) {
cb(err, res);
});
};
Expand Down

0 comments on commit 6d5f502

Please sign in to comment.