Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Dec 24, 2015
1 parent b942389 commit e6b5906
Show file tree
Hide file tree
Showing 31 changed files with 1,619 additions and 1,691 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -13,9 +13,9 @@ It provide the following features:
- [x] Patch a content of the current buffer to a gist
- [ ] Rename files in a gist
- [ ] Remove files in a gist
- [ ] Delete a gist
- [ ] Star/Unstar a gist
- [ ] Folk a gist
- [x] Delete a gist
- [x] Star/Unstar a gist
- [x] Folk a gist
- [ ] List folks of a gist
- [ ] List commits of a gist

Expand Down
111 changes: 57 additions & 54 deletions autoload/gista/api.vim
Expand Up @@ -11,6 +11,7 @@ let s:G = s:V.import('Web.API.GitHub')
let s:registry = {}
let s:current_client = {}

" Private functions
function! s:get_client_cache() abort " {{{
if !exists('s:client_cache')
let s:client_cache = s:C.new('memory')
Expand Down Expand Up @@ -71,6 +72,12 @@ function! s:validate_apiname(apiname) abort " {{{
\ 'An API name "%value" has not been registered yet',
\)
endfunction " }}}
function! s:validate_username(username) abort " {{{
call gista#util#validate#pattern(
\ a:username, '^[a-zA-Z0-9_\-]\+$',
\ 'An API username "%value" requires to follow "%pattern"'
\)
endfunction " }}}
function! s:get_default_apiname() abort " {{{
let apiname = g:gista#api#default_apiname
try
Expand All @@ -84,22 +91,13 @@ function! s:get_default_apiname() abort " {{{
return 'GitHub'
endtry
endfunction " }}}
function! s:validate_username(username) abort " {{{
call gista#util#validate#pattern(
\ a:username, '^[a-zA-Z0-9_\-]\+$',
\ 'An API username "%value" requires to follow "%pattern"'
\)
endfunction " }}}
function! s:get_default_username(apiname) abort " {{{
if type(g:gista#api#default_username) == type('')
let username = g:gista#api#default_username
else
let default = get(g:gista#api#default_username, '_', '')
let username = get(g:gista#api#default_username, a:apiname, default)
endif
if empty(username)
return ''
endif
try
call s:validate_username(username)
return username
Expand Down Expand Up @@ -165,6 +163,17 @@ function! s:get_client(apiname) abort " {{{
return client
endfunction " }}}

" Protected function
function! gista#api#_get_available_apinames() abort " {{{
return keys(s:registry)
endfunction " }}}
function! gista#api#_get_available_usernames(apiname) abort " {{{
call s:validate_apiname(a:apiname)
let client = s:get_client(a:apiname)
return client.token_cache.keys()
endfunction " }}}

" Public function
function! gista#api#register(apiname, baseurl) abort " {{{
try
call gista#util#validate#not_empty(a:apiname,
Expand Down Expand Up @@ -210,24 +219,22 @@ function! gista#api#get_current_client() abort " {{{
endif
return s:current_client
endfunction " }}}
function! gista#api#get_client(apiname) abort " {{{
if empty(a:apiname)
return gista#api#get_current_client()
else
call s:validate_apiname(a:apiname)
return s:get_client(a:apiname)
endif
function! gista#api#get_current_apiname() abort " {{{
return gista#api#get_current_client().apiname
endfunction " }}}
function! gista#api#get_current_username() abort " {{{
return gista#api#get_current_client().get_authorized_username()
endfunction " }}}

function! gista#api#switch(...) abort " {{{
function! gista#api#switch_client(apiname, ...) abort " {{{
let options = extend({
\ 'verbose': 1,
\ 'apiname': '',
\ 'username': 0,
\ 'permanent': 0,
\}, get(a:000, 0, {})
\)
let client = gista#api#get_client(options.apiname)
call s:validate_apiname(a:apiname)
let client = s:get_client(a:apiname)
if type(options.username) == type('')
if empty(options.username)
call s:logout(client, options)
Expand All @@ -238,56 +245,52 @@ function! gista#api#switch(...) abort " {{{
let s:current_client = client
return client
endfunction " }}}
function! gista#api#session_enter(...) abort " {{{
let options = extend({
\ 'verbose': 1,
\ 'apiname': '',
\ 'username': 0,
\ 'permanent': 0,
\}, get(a:000, 0, {}),
\)
if exists('s:previous_client')

let s:session = {}
function! s:session.enter() abort " {{{
if has_key(self, '_previous_client')
call gista#util#prompt#throw(
\ 'SessionError: gista#api#session_exit() has not been called',
\ 'SessionError: session.exit() has not been called yet',
\)
return
endif
let s:previous_client = deepcopy(
\ gista#api#get_client(options.apiname)
\)
call gista#api#switch(options)
let self._previous_client = gista#api#get_current_client()
call gista#api#switch_client(self.apiname, {
\ 'verbose': self.verbose,
\ 'username': self.username,
\ 'permanent': 0,
\})
endfunction " }}}
function! gista#api#session_exit() abort " {{{
if !exists('s:previous_client')
function! s:session.exit() abort " {{{
if !has_key(self, '_previous_client')
call gista#util#prompt#throw(
\ 'SessionError: gista#api#session_enter() has not been called',
\ 'SessionError: session.enter() has not been called yet',
\)
return
endif
let s:current_client = s:previous_client
unlet s:previous_client
endfunction " }}}

function! gista#api#complete_apiname(arglead, cmdline, cursorpos, ...) abort " {{{
let apinames = keys(s:registry)
return filter(apinames, 'v:val =~# "^" . a:arglead')
let s:current_client = self._previous_client
unlet self._previous_client
endfunction " }}}
function! gista#api#complete_username(arglead, cmdline, cursorpos, ...) abort " {{{
function! gista#api#session(...) abort " {{{
let options = extend({
\ 'verbose': 1,
\ 'apiname': '',
\ 'username': 0,
\}, get(a:000, 0, {}),
\)
try
let client = gista#api#get_client(options.apiname)
let usernames = client.token_cache.keys()
return filter(usernames, 'v:val =~# "^" . a:arglead')
catch /^vim-gista/
" fail silently
return []
endtry
let apiname = empty(options.apiname)
\ ? s:get_default_apiname()
\ : options.apiname
let session = extend(copy(s:session), {
\ 'verbose': options.verbose,
\ 'apiname': apiname,
\ 'username': options.username,
\})
return session
endfunction " }}}

function! gista#api#throw_api_exception(res) abort " {{{
call gista#util#prompt#throw(s:G.build_exception_message(a:res))
function! gista#api#throw_api_exception(response) abort " {{{
call gista#util#prompt#throw(s:G.build_exception_message(a:response))
endfunction " }}}

" Register APIs
Expand Down
3 changes: 1 addition & 2 deletions autoload/gista/api/commits.vim
Expand Up @@ -11,8 +11,7 @@ function! gista#api#commit#list(gistid, ...) abort " {{{
endfunction " }}}

" Configure variables
call gista#define_variables('api#commit', {
\})
call gista#define_variables('api#commit', {})

let &cpo = s:save_cpo
unlet! s:save_cpo
Expand Down
39 changes: 15 additions & 24 deletions autoload/gista/api/forks.vim
Expand Up @@ -21,35 +21,27 @@ function! gista#api#fork#post(gistid, ...) abort " {{{
if options.verbose
redraw
call gista#util#prompt#echo(printf(
\ 'Forking a gist "%s" in %s ...',
\ 'Forking a gist %s in %s ...',
\ gist.id,
\ client.apiname,
\))
endif

let url = printf('gists/%s/forks', gist.id)
let res = client.post(url, {}, {}, {
\ 'verbose': options.verbose,
\})
let res.content = get(res, 'content', '')
let res.content = empty(res.content) ? {} : s:J.decode(res.content)
if res.status != 201
call gista#api#throw_api_exception(res)
let res = client.post(url)
if res.status == 201
let res.content = get(res, 'content', '')
let res.content = empty(res.content) ? {} : s:J.decode(res.content)
let gist = res.content
let gist._gista_fetched = 1
let gist._gista_modified = 0
let gist._last_modified = s:G.parse_response_last_modified(res)
call client.content_cache.set(gist.id, gist)
" TODO
" Update cached entries of the gist
return gist
endif

let gist = gista#gist#mark_fetched(res.content)
call client.content_cache.set(gist.id, gist)
if !empty(username)
call gista#gist#apply_to_entry_cache(
\ client, gist.id,
\ function('gista#gist#mark_fetched'),
\)
call gista#gist#apply_to_entry_cache(
\ client, gist.id,
\ function('gista#gist#unmark_modified'),
\)
endif
return gist
call gista#api#throw_api_exception(res)
endfunction " }}}
function! gista#api#fork#list(gistid, ...) abort " {{{
call gista#util#prompt#throw(
Expand All @@ -58,8 +50,7 @@ function! gista#api#fork#list(gistid, ...) abort " {{{
endfunction " }}}

" Configure variables
call gista#define_variables('api#fork', {
\})
call gista#define_variables('api#fork', {})

let &cpo = s:save_cpo
unlet! s:save_cpo
Expand Down

0 comments on commit e6b5906

Please sign in to comment.