Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 5 commits into from

2 participants

Brent Beer James Ward
Brent Beer
  1. Added searching by a filePath.
    • Allows more automation around getting code-contents
    • Jekyll files could even use this to more easily embed content on a post.
  2. Because of additional method, caching is more important.
    • Two method calls mean getting rate-limited twice as fast
    • If we don't always cache, we would probably need an additional method parameter. This would end up breaking existing method calls. :frowning: :-1:
brntbeer added some commits
Brent Beer brntbeer Updated script to also work if you only give a filepath in a repo 176eeee
Brent Beer brntbeer additional commenting on readme 2a4607a
Brent Beer brntbeer added caching.
In general getting content by filepath's was sending 2 requests,
which reaches the request-limit of 60 very fast. Even though the
limits are on an IP basis this caching scheme seems best.

I also realize that ajax calls have a `cache: true`. However, this
was not working for `$.getGithubFile` for some reason, and this
method works absolutely.
9ed5730
Brent Beer brntbeer removed optional caching. always cache 3c9752f
Brent Beer brntbeer Accidentally forgot to remove optional-cache call all the way 86bfcf1
James Ward jamesward merged commit 621d025 into from
James Ward
Owner

Awesome! Thank you!

Brent Beer

No problem! Such a fast merge! :grin:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 30, 2012
  1. Brent Beer
  2. Brent Beer
  3. Brent Beer

    added caching.

    brntbeer authored
    In general getting content by filepath's was sending 2 requests,
    which reaches the request-limit of 60 very fast. Even though the
    limits are on an IP basis this caching scheme seems best.
    
    I also realize that ajax calls have a `cache: true`. However, this
    was not working for `$.getGithubFile` for some reason, and this
    method works absolutely.
  4. Brent Beer
  5. Brent Beer
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 6 deletions.
  1. +11 −0 README.md
  2. +33 −6 src/main/javascript/github-files.js
11 README.md
View
@@ -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.
39 src/main/javascript/github-files.js
View
@@ -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.