Skip to content

Allow content to be pulled from a filePath, also cache the calls. #4

Merged
merged 5 commits into from Oct 30, 2012
View
11 README.md
@@ -9,6 +9,10 @@ API:
$.getGithubFile(user, repo, sha, callback, startLineNum, endLineNum)
+or
+
+ $.getGithubFileByFilePath(user, repo, filePath, callback, startLineNum, endLineNum)
+
Examples:
// fetch this README.md file and return all of the lines
@@ -26,6 +30,13 @@ Examples:
console.log(contents)
}, 6, 15);
+ // fetch this README.md file and return all of the lines
+ $.getGithubFileByFilePath("jamesward", "github-files", "README.md", function(contents) {
+ console.log(contents)
+ });
+
To get the sha for a file in git run something like:
git rev-parse HEAD:README.md
+
+or use the `$.getGithubFileByFilePath` method as documented above.
View
39 src/main/javascript/github-files.js
@@ -1,4 +1,7 @@
;(function ($) {
+ var githubCacheFilePath = [];
+ var githubCacheSha = [];
+
var fnSuccess =
function (data, startLineNum, endLineNum, callback) {
if (data.data.content && data.data.encoding === "base64") {
@@ -13,13 +16,37 @@
}
};
+ $.getGithubFileByFilePath =
+ function(user, repo, filePath, callback, startLineNum, endLineNum) {
+ if(githubCacheFilePath[filePath]){
+ $.getGithubFile(user, repo, githubCacheFilePath[filePath], callback, startLineNum, endLineNum)
+ }else{
+ $.ajax({
+ type: "GET"
+ ,url: "https://api.github.com/repos/" + user + "/" + repo + "/contents/"+filePath
+ ,dataType: "jsonp"
+ ,success: function(data){
+ githubCacheFilePath[filePath] = data.data.sha;
+ $.getGithubFile(user, repo, githubCacheFilePath[filePath], callback, startLineNum, endLineNum)
+ }
+ });
+ }
+ };
+
$.getGithubFile =
function(user, repo, sha, callback, startLineNum, endLineNum) {
- $.ajax({
- type: "GET"
- ,url: "https://api.github.com/repos/" + user + "/" + repo + "/git/blobs/" + sha
- ,dataType: "jsonp"
- ,success: function(data) {fnSuccess(data, +startLineNum || 1, +endLineNum || 0, callback);}
- });
+ if(githubCacheSha[sha]){
+ fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
+ }else{
+ $.ajax({
+ type: "GET"
+ ,url: "https://api.github.com/repos/" + user + "/" + repo + "/git/blobs/" + sha
+ ,dataType: "jsonp"
+ ,success: function(data) {
+ githubCacheSha[sha] = data
+ fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
+ }
+ });
+ }
};
}(jQuery));
Something went wrong with that request. Please try again.