Skip to content

Instantly share code, notes, and snippets.

@dannygarcia
Created August 12, 2012 06:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannygarcia/3330033 to your computer and use it in GitHub Desktop.
Save dannygarcia/3330033 to your computer and use it in GitHub Desktop.
GitHub Repo File Structure Crawler
var ajax = function (url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.send();
xhr.onreadystatechange = function (e) {
if (e.target.status === 200 && e.target.readyState === 4) {
callback(JSON.parse(e.target.response));
}
};
};
var buildGithubTree = function (user, repo) {
var self = this;
this.struct = [];
this.addBranch = function (array, item) {
ajax(item._links.self, function (data) {
self.processTree(array, data);
});
};
this.processTree = function (array, items) {
var item, i;
for (i = 0; i < items.length; i++) {
item = items[i];
if (item.type === 'dir') {
array[i] = {folder : []};
this.addBranch(array[i].folder, item);
} else {
array[i] = {item : item};
}
}
};
ajax('https://api.github.com/repos/' + user + '/' + repo + '/contents/', function (data) {
self.processTree(self.struct, data);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment