Skip to content

Commit 537e123

Browse files
committed
JSCS github module
1 parent a4e8252 commit 537e123

File tree

1 file changed

+84
-96
lines changed

1 file changed

+84
-96
lines changed

src/modules/github.js

+84-96
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,99 @@
1-
//
2-
// GitHub
3-
//
4-
(function(hello){
5-
6-
function formatError(o,headers){
7-
var code = headers ? headers.statusCode : ( o && "meta" in o && "status" in o.meta && o.meta.status );
8-
if( (code===401||code===403) ){
9-
o.error = {
10-
code : "access_denied",
11-
message : o.message || (o.data?o.data.message:"Could not get response")
12-
};
13-
delete o.message;
1+
(function(hello) {
2+
3+
function formatError(o, headers) {
4+
var code = headers ? headers.statusCode : (o && 'meta' in o && 'status' in o.meta && o.meta.status);
5+
if ((code === 401 || code === 403)) {
6+
o.error = {
7+
code: 'access_denied',
8+
message: o.message || (o.data ? o.data.message : 'Could not get response')
9+
};
10+
delete o.message;
11+
}
1412
}
15-
}
1613

17-
function formatUser(o){
18-
if(o.id){
19-
o.thumbnail = o.picture = o.avatar_url;
20-
o.name = o.login;
14+
function formatUser(o) {
15+
if (o.id) {
16+
o.thumbnail = o.picture = o.avatar_url;
17+
o.name = o.login;
18+
}
2119
}
22-
}
23-
24-
function paging(res,headers,req){
25-
if(res.data&&res.data.length&&headers&&headers.Link){
26-
var next = headers.Link.match(/<(.*?)>;\s*rel=\"next\"/);
27-
if(next){
28-
res.paging = {
29-
next : next[1]
30-
};
20+
21+
function paging(res, headers, req) {
22+
if (res.data && res.data.length && headers && headers.Link) {
23+
var next = headers.Link.match(/<(.*?)>;\s*rel=\"next\"/);
24+
if (next) {
25+
res.paging = {
26+
next: next[1]
27+
};
28+
}
3129
}
3230
}
33-
}
34-
35-
hello.init({
36-
github : {
37-
name : 'GitHub',
38-
oauth : {
39-
version : 2,
40-
auth : 'https://github.com/login/oauth/authorize',
41-
grant : 'https://github.com/login/oauth/access_token',
42-
response_type : 'code'
43-
},
44-
45-
scope : {
46-
basic : '',
47-
email : 'user:email'
48-
},
49-
base : 'https://api.github.com/',
50-
get : {
51-
'me' : 'user',
52-
'me/friends' : 'user/following?per_page=@{limit|100}',
53-
'me/following' : 'user/following?per_page=@{limit|100}',
54-
'me/followers' : 'user/followers?per_page=@{limit|100}',
55-
'me/like' : 'user/starred?per_page=@{limit|100}'
56-
},
57-
// post : {
58-
59-
// // https://developer.github.com/v3/activity/starring/#star-a-repository
60-
// 'me/like' : function(p,callback){
61-
// p.method = 'put';
62-
// p.headers['Content-Length'] = 0;
63-
// var id = p.data.id;
64-
// p.data = null;
65-
// callback("user/starred/"+id);
66-
// }
67-
// },
68-
// del : {
69-
70-
// // https://developer.github.com/v3/activity/starring/#unstar-a-repository
71-
// 'me/like' : "user/starred/@{id}"
72-
// },
73-
wrap : {
74-
me : function(o,headers){
75-
76-
formatError(o,headers);
77-
formatUser(o);
78-
79-
return o;
31+
32+
hello.init({
33+
github: {
34+
name: 'GitHub',
35+
oauth: {
36+
version: 2,
37+
auth: 'https://github.com/login/oauth/authorize',
38+
grant: 'https://github.com/login/oauth/access_token',
39+
response_type: 'code'
40+
},
41+
42+
scope: {
43+
basic: '',
44+
email: 'user:email'
8045
},
81-
"default" : function(o,headers,req){
8246

83-
formatError(o,headers);
47+
base: 'https://api.github.com/',
48+
49+
get: {
50+
me: 'user',
51+
'me/friends': 'user/following?per_page=@{limit|100}',
52+
'me/following': 'user/following?per_page=@{limit|100}',
53+
'me/followers': 'user/followers?per_page=@{limit|100}',
54+
'me/like': 'user/starred?per_page=@{limit|100}'
55+
},
56+
57+
wrap: {
58+
me: function(o, headers) {
59+
60+
formatError(o, headers);
61+
formatUser(o);
8462

85-
if(Object.prototype.toString.call(o) === '[object Array]'){
86-
o = {data:o};
87-
paging(o,headers,req);
88-
for(var i=0;i<o.data.length;i++){
89-
formatUser(o.data[i]);
63+
return o;
64+
},
65+
66+
default: function(o, headers, req) {
67+
68+
formatError(o, headers);
69+
70+
if (Object.prototype.toString.call(o) === '[object Array]') {
71+
o = {data:o};
72+
paging(o, headers, req);
73+
for (var i = 0; i < o.data.length; i++) {
74+
formatUser(o.data[i]);
75+
}
9076
}
77+
78+
return o;
9179
}
92-
return o;
93-
}
94-
},
95-
xhr : function(p){
96-
97-
if( p.method !== 'get' && p.data ){
98-
// Serialize payload as JSON
99-
p.headers = p.headers || {};
100-
p.headers['Content-Type'] = 'application/json';
101-
if (typeof(p.data) === 'object'){
102-
p.data = JSON.stringify(p.data);
80+
},
81+
82+
xhr: function(p) {
83+
84+
if (p.method !== 'get' && p.data) {
85+
86+
// Serialize payload as JSON
87+
p.headers = p.headers || {};
88+
p.headers['Content-Type'] = 'application/json';
89+
if (typeof (p.data) === 'object') {
90+
p.data = JSON.stringify(p.data);
91+
}
10392
}
104-
}
10593

106-
return true;
94+
return true;
95+
}
10796
}
108-
}
109-
});
97+
});
11098

11199
})(hello);

0 commit comments

Comments
 (0)