Created
August 12, 2012 06:01
-
-
Save dannygarcia/3330033 to your computer and use it in GitHub Desktop.
GitHub Repo File Structure Crawler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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