Skip to content

Commit d65d323

Browse files
committed
Display error message if API exceds its limits
1 parent 703e382 commit d65d323

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

jquery.github.repos.js

+55-31
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/*
2-
* Project: jQuery Github Repos
3-
* Description: A jQuery plugin to display your Github Repositories.
4-
* Author: Zeno Rocha
5-
* License: MIT
2+
* jQuery Github Repos v0.3.1
3+
* A jQuery plugin to display your Github Repositories.
4+
* http://git.io/3A1RMg
5+
*
6+
* Zeno Rocha
7+
* MIT License
68
*/
79

810
// the semi-colon before function invocation is a safety net against concatenated
@@ -30,6 +32,7 @@
3032
this.element = element;
3133
this.$container = $(element);
3234
this.repo = this.$container.attr("data-repo");
35+
this.cached;
3336

3437
// jQuery has an extend method which merges the contents of two or
3538
// more objects, storing the result in the first object. The first object
@@ -45,42 +48,63 @@
4548

4649
Plugin.prototype.init = function () {
4750

48-
var self = this;
51+
var self = this;
4952

5053
$.ajax({
5154
url: 'https://api.github.com/repos/' + this.repo,
5255
dataType: 'jsonp',
5356
success: function(results){
5457

55-
var repo = results.data;
56-
var date = new Date(repo.pushed_at);
57-
var pushed_at = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
58-
var $widget = $(' \
59-
<div class="github-box"> \
60-
<div class="github-box-header"> \
61-
<h3> \
62-
<a href="' + repo.url.replace('api.','').replace('repos/','') + '">' + repo.name + '</a> \
63-
</h3> \
64-
<div class="github-stats"> \
65-
<a class="repo-watchers" href="' + repo.url.replace('api.','').replace('repos/','') + '/watchers">' + repo.watchers + '</a> \
66-
<a class="repo-forks" href="' + repo.url.replace('api.','').replace('repos/','') + '/forks">' + repo.forks + '</a> \
67-
</div> \
68-
</div> \
69-
<div class="github-box-content"> \
70-
<p>' + repo.description + ' &mdash; <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \
71-
</div> \
72-
<div class="github-box-download"> \
73-
<p class="repo-update">Latest commit to <strong>master</strong> on ' + pushed_at + '</p> \
74-
<a class="repo-download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \
75-
</div> \
76-
</div> \
77-
');
78-
79-
$widget.appendTo(self.$container);
58+
// Handle API failures
59+
if (results.meta.status >= 400 && results.data.message){
60+
console.warn(results.data.message);
61+
return;
62+
}
63+
else {
64+
self.applyTemplate(results.data);
65+
}
8066

8167
}
82-
8368
});
69+
70+
};
71+
72+
Plugin.prototype.applyTemplate = function (repo) {
73+
74+
var self = this;
75+
76+
var date = new Date(repo.pushed_at);
77+
var pushed_at = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear();
78+
79+
var $widget = $(' \
80+
<div class="github-box"> \
81+
<div class="github-box-header"> \
82+
<h3> \
83+
<a href="' + repo.url.replace('api.','').replace('repos/','') + '">' + repo.name + '</a> \
84+
</h3> \
85+
<div class="github-stats"> \
86+
<a class="repo-watchers" href="' + repo.url.replace('api.','').replace('repos/','') + '/watchers">' + repo.watchers + '</a> \
87+
<a class="repo-forks" href="' + repo.url.replace('api.','').replace('repos/','') + '/forks">' + repo.forks + '</a> \
88+
</div> \
89+
</div> \
90+
<div class="github-box-content"> \
91+
<p>' + repo.description + ' &mdash; <a href="' + repo.url.replace('api.','').replace('repos/','') + '#readme">Read More</a></p> \
92+
</div> \
93+
<div class="github-box-download"> \
94+
<p class="repo-update">Latest commit to <strong>master</strong> on ' + repo.pushed_at + '</p> \
95+
<a class="repo-download" href="' + repo.url.replace('api.','').replace('repos/','') + '/zipball/master">Download as zip</a> \
96+
</div> \
97+
</div> \
98+
');
99+
100+
// console.log($widget);
101+
self.appendTemplate($widget);
102+
103+
};
104+
105+
Plugin.prototype.appendTemplate = function ($widget) {
106+
var self = this;
107+
$widget.appendTo(self.$container);
84108
};
85109

86110
// A really lightweight plugin wrapper around the constructor,

jquery.github.repos.min.js

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)