Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from Oct 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,10 @@ API:


$.getGithubFile(user, repo, sha, callback, startLineNum, endLineNum) $.getGithubFile(user, repo, sha, callback, startLineNum, endLineNum)


or

$.getGithubFileByFilePath(user, repo, filePath, callback, startLineNum, endLineNum)

Examples: Examples:


// fetch this README.md file and return all of the lines // fetch this README.md file and return all of the lines
Expand All @@ -26,6 +30,13 @@ Examples:
console.log(contents) console.log(contents)
}, 6, 15); }, 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: To get the sha for a file in git run something like:


git rev-parse HEAD:README.md git rev-parse HEAD:README.md

or use the `$.getGithubFileByFilePath` method as documented above.
39 changes: 33 additions & 6 deletions src/main/javascript/github-files.js
@@ -1,4 +1,7 @@
;(function ($) { ;(function ($) {
var githubCacheFilePath = [];
var githubCacheSha = [];

var fnSuccess = var fnSuccess =
function (data, startLineNum, endLineNum, callback) { function (data, startLineNum, endLineNum, callback) {
if (data.data.content && data.data.encoding === "base64") { if (data.data.content && data.data.encoding === "base64") {
Expand All @@ -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 = $.getGithubFile =
function(user, repo, sha, callback, startLineNum, endLineNum) { function(user, repo, sha, callback, startLineNum, endLineNum) {
$.ajax({ if(githubCacheSha[sha]){
type: "GET" fnSuccess(githubCacheSha[sha], +startLineNum || 1, +endLineNum || 0, callback);
,url: "https://api.github.com/repos/" + user + "/" + repo + "/git/blobs/" + sha }else{
,dataType: "jsonp" $.ajax({
,success: function(data) {fnSuccess(data, +startLineNum || 1, +endLineNum || 0, callback);} 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)); }(jQuery));