|
21 | 21 | };
|
22 | 22 | var _shell = Josh.Shell({console: _console});
|
23 | 23 | var _pathhandler = new Josh.PathHandler(_shell, {console: _console});
|
| 24 | + var _rateLimitTemplate = _.template("<%=remaining%>/<%=limit%><% if(noAuthToken) {%> <a href='http://josh.claassen.net/github'>Authenticate with Github to increase your Rate Limit.</a><%}%>"); |
24 | 25 | var _self = {
|
25 |
| - api: "https://api.github.com/", |
| 26 | + api: "https://api.github.com/" |
26 | 27 | };
|
27 | 28 | _shell.templates.repos = _.template("<ul class='widelist'><% _.each(repos, function(repo) { %><li><%- repo.name %></li><% }); %></ul>");
|
28 | 29 | _shell.templates.prompt = _.template("<em>[<%= self.user.login %>/<%= self.repo.name %>]</em></br>(<%=self.branch%>) <strong><%= node.path %> $</strong>");
|
|
58 | 59 | });
|
59 | 60 |
|
60 | 61 | function getRepos(callback) {
|
61 |
| - var uri = _self.api + "users/" + _self.user.login + "/repos?callback=?"; |
62 |
| - _console.log("fetching: " + uri); |
63 |
| - return $.getJSON(uri, function(response) { |
64 |
| - checkRateLimit(response.meta); |
65 |
| - _self.repos = response.data; |
| 62 | + return get("users/" + _self.user.login + "/repos", null, function(data) { |
| 63 | + _self.repos = data; |
66 | 64 | callback();
|
67 | 65 | });
|
68 | 66 | }
|
69 | 67 |
|
| 68 | + function get(resource, args, callback) { |
| 69 | + var url = _self.api + resource; |
| 70 | + if(_self.access_token) { |
| 71 | + args = args || {}; |
| 72 | + args.access_token = _self.access_token; |
| 73 | + } |
| 74 | + if(args) { |
| 75 | + url += "?" + _.map(args,function(v, k) { |
| 76 | + return k + "=" + v; |
| 77 | + }).join("&"); |
| 78 | + } |
| 79 | + _console.log("fetching: " + url); |
| 80 | + var request = { |
| 81 | + url: url, |
| 82 | + dataType: 'jsonp' |
| 83 | + }; |
| 84 | + $.ajax(request).done(function(response) { |
| 85 | + _console.log(response.meta); |
| 86 | + $('#ratelimit').html(_rateLimitTemplate({ |
| 87 | + remaining: response.meta["X-RateLimit-Remaining"], |
| 88 | + limit: response.meta["X-RateLimit-Limit"], |
| 89 | + noAuthToken: !_self.access_token |
| 90 | + })); |
| 91 | + if(response.meta["X-RateLimit-Remaining"] == 0) { |
| 92 | + alert("Whoops, you've hit the github rate limit. You'll need to authenticate to continue"); |
| 93 | + _shell.deactivate(); |
| 94 | + return; |
| 95 | + } |
| 96 | + callback(response.data); |
| 97 | + }) |
| 98 | + } |
| 99 | + |
70 | 100 | function setUser(user, repo, callback) {
|
71 | 101 | if(_self.user && _self.user.login === user) {
|
72 | 102 | return callback();
|
73 | 103 | }
|
74 |
| - var uri = _self.api + "users/" + user + "?callback=?"; |
75 |
| - _console.log("fetching: " + uri); |
76 |
| - return $.getJSON(uri, function(response) { |
77 |
| - checkRateLimit(response.meta); |
78 |
| - _self.user = response.data; |
| 104 | + return get("users/" + user, null, function(data) { |
| 105 | + _self.user = data; |
79 | 106 | getRepos(function() {
|
80 | 107 | setRepo(repo, function() {
|
81 | 108 | callback();
|
|
84 | 111 | });
|
85 | 112 | }
|
86 | 113 |
|
87 |
| - function checkRateLimit(meta) { |
88 |
| - _console.log(response.meta); |
89 |
| - if(response.meta["X-RateLimit-Remaining"] == 0) { |
90 |
| - alert("Whoops, you've hit the github rate limit. You'll need to authenticate to continue"); |
91 |
| - } |
92 |
| - _shell.deactivate(); |
93 |
| - } |
94 |
| - |
95 | 114 | function getDir(path, callback) {
|
96 |
| - if(path && path.length > 1 && path[path.length-1] === '/') { |
97 |
| - path = path.substr(0,path.length-1); |
| 115 | + if(path && path.length > 1 && path[path.length - 1] === '/') { |
| 116 | + path = path.substr(0, path.length - 1); |
98 | 117 | }
|
99 |
| - var uri = _self.api + "repos/" + _self.user.login + "/" + _self.repo.name + "/contents" + path + "?callback=?"; |
100 | 118 | //var uri = _self.api + "repos/" + _self.user.login + "/" + _self.repo.name + "/contents" + path + "?ref=" + _self.branch + "&callback=?";
|
101 |
| - _console.log("fetching: " + uri); |
102 |
| - $.getJSON(uri, function(response) { |
103 |
| - checkRateLimit(response.meta); |
104 |
| - if(Object.prototype.toString.call(response.data) !== '[object Array]') { |
| 119 | + get("repos/" + _self.user.login + "/" + _self.repo.name + "/contents" + path, null, function(data) { |
| 120 | + if(Object.prototype.toString.call(data) !== '[object Array]') { |
105 | 121 | _console.log("path '" + path + "' was a file");
|
106 | 122 | return callback();
|
107 | 123 | }
|
|
110 | 126 | return x;
|
111 | 127 | })) || "",
|
112 | 128 | path: path,
|
113 |
| - children: response.data |
| 129 | + children: data |
114 | 130 | };
|
115 | 131 | _console.log("got node at: " + node.path);
|
116 | 132 | return callback(node);
|
|
200 | 216 |
|
201 | 217 |
|
202 | 218 | $(document).ready(function() {
|
203 |
| - var $consolePanel = $('#shell-panel'); |
204 |
| - $consolePanel.resizable({ handles: "s"}); |
205 |
| - _console.log("initializing 'sdether'"); |
206 |
| - setUser("sdether", "josh.js", function() { |
207 |
| - _console.log("activating"); |
208 |
| - _shell.activate(); |
209 |
| - }); |
| 219 | + $.ajax({ |
| 220 | + url: 'http://josh.claassen.net/github-token', |
| 221 | + type: "get", |
| 222 | + dataType: "json", |
| 223 | + xhrFields: { |
| 224 | + withCredentials: true |
| 225 | + } |
| 226 | + }) |
| 227 | + .done(function(data) { |
| 228 | + _console.log(data); |
| 229 | + _self.access_token = data.access_token; |
| 230 | + var $consolePanel = $('#shell-panel'); |
| 231 | + $consolePanel.resizable({ handles: "s"}); |
| 232 | + _console.log("initializing 'sdether'"); |
| 233 | + setUser("sdether", "josh.js", function() { |
| 234 | + _console.log("activating"); |
| 235 | + _shell.activate(); |
| 236 | + }); |
| 237 | + }); |
210 | 238 | });
|
211 | 239 | })(root, $, _);
|
212 | 240 | })(this, $, _);
|
0 commit comments