Skip to content

Instantly share code, notes, and snippets.

@xyn9
Created October 16, 2011 18:25
Show Gist options
  • Save xyn9/1291234 to your computer and use it in GitHub Desktop.
Save xyn9/1291234 to your computer and use it in GitHub Desktop.
github api 簡易ライブラリ
// github.js
//
// ==UserScript==
// @name simple github api
// @version 0.92
// @include
// @require
// @description github api 簡易ライブラリ
// @homepage
// @author xyn9 <xyn9.mail@gmail.com>
// @lisence (CC) Attribution Share Alike; http://creativecommons.org/licenses/by-sa/2.1/jp
// ==UserScript==
// ------------------------------------------------------------
//
function github(_PROFILE){
//
var _this_ = this;
//
var $http = new ActiveXObject('Microsoft.XMLHTTP');
//
var $API = {
url: 'https://api.github.com'
, token_url: 'https://github.com'
,
key: _PROFILE.client_id
, skey: _PROFILE.client_secret
, token: _PROFILE.token
, scope: 'repo,public_repo,delete_repo,gist'
};
// ------------------------------------------------------------
function session(_options, _token){
//
var url = $API[(_token ? 'token_url' : 'url')] + _options.action;
//
if(! _token){ _options.parameters.access_token = $API.token; }
//
var param, query = [];
for(var p in _options.parameters){
if(param = _options.parameters[p]){ query.push(p +'='+ encodeURIComponent(param)); }
}
//
with( $http ){
open(_options.method, (url + query.join('&').replace(/^(.+)$/,'?$1')), false);
send(_options.input ? _options.input : null);
}
//
return $http;
};
// ------------------------------------------------------------
// repository
this.repos = {
//
list: function (){
return session({
method: 'GET'
, action: '/user/repos'
, parameters: {}
});
}
,
//
create: function (_name, _options){
//
var params = ['"name":"'+ _name +'"'];
for(var o in _options){
params.push('"'+ o +'":"'+ _options[o] +'"');
}
//
return session({
method: 'POST'
, action: '/user/repos'
, parameters: {}
, input: ('{'+ params.join(',') +'}')
});
}
,
//
'_delete': function (_name){
return session({
method: 'DELETE'
, action: ('/repos/'+ _PROFILE.id +'/'+ _name)
, parameters: {}
});
}
//
};
// ------------------------------------------------------------
// gist
this.gist = {
//
list: function (){
return session({
method: 'GET'
, action: '/gists'
, parameters: {}
});
}
,
//
create: function (_files, _description, _private){
//
var file_list = [];
for(var f in _files){ file_list.push('"'+ f +'":{"content":"'+ _files[f] +'"}'); }
//
return session({
method: 'POST'
, action: '/gists'
, parameters: {}
, input: [
'{'
, '"public": '+ (! _private)
, ', "description": "'+ _description.replace(/"/g,'\\"') +'"'
, ', "files": {'+ file_list.join('\n,') +'}'
, '}'
].join('\n')
});
}
,
//
'_delete': function (_id){
return session({
method: 'DELETE'
, action: ('/gists/'+ _id)
, parameters: {}
});
}
//
};
// ------------------------------------------------------------
// downloads
this.downloads = {
//
list: function (_repos, _id){
return session({
method: 'GET'
, action: ['/repos',(_id ? _id : _PROFILE.id),_repos,'downloads')].join('/')
, parameters: {}
});
}
,
//
create: function (_repos, _name, _size, _options){
//
var params = ['"name":"'+ _name +'","size":"'+ _size +'"'];
for(var o in _options){ params.push('"'+ o +'":"'+ _options[o] +'"'); }
//
return session({
method: 'POST'
, action: ['/repos', _PROFILE.id, _repos, 'downloads'].join('/')
, parameters: {}
, input: ('{'+ params.join(',') +'}')
});
}
,
//
'_delete': function (_repos, _f_id, _id){
return session({
method: 'DELETE'
, action: ('/repos/'+ (_id ? _id : _PROFILE.id) +'/'+ _repos +'/downloads/'+ _f_id)
, parameters: {}
});
}
//
};
// ------------------------------------------------------------
// user
this.user = {
starred: function (_repos, _owner, _del){
return session({
method: (_del ? 'DELETE' : 'PUT')
, action: ('/user/starred/'+ _owner +'/'+ _repos)
, parameters: {}
});
}
};
// ------------------------------------------------------------
this.get_token = function (_code){
//
if(! _code){
return ($API.token_url +'/login/oauth/authorize?client_id='+ $API.key +'&scope='+ $API.scope);
}
//
var result = session({
method: 'POST'
, action: '/login/oauth/access_token'
, parameters: {
client_id: $API.key, client_secret: $API.skey
, code: _code
}
}, 1);
//
return (
(
/^20\d$/.test(result.status)
&& (/access_token=([^=&]+)/i).test(result.responseText)
)
? ($API.token = RegExp.$1)
: 0
);
//
};
//
return _this_;
//
}
/*
http://developer.github.com/v3/
Authorize URL
https://github.com/login/oauth/authorize
Access Token URL
https://github.com/login/oauth/access_token
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment