Permalink
Browse files

Update Gist command.

(Issue #3)
  • Loading branch information...
1 parent 7dfb42c commit ccff27894a04aa16716cf78492a8d74d01e1be83 @bgreenlee committed Mar 3, 2012
Showing with 54 additions and 3 deletions.
  1. +2 −1 Github.sublime-commands
  2. +4 −0 README.md
  3. +43 −0 gist.py
  4. +5 −2 sublime_requests.py
View
3 Github.sublime-commands
@@ -6,5 +6,6 @@
{ "caption": "GitHub: Open Gist in Editor", "command": "open_gist_in_editor" },
{ "caption": "GitHub: Open Starred Gist in Editor", "command": "open_starred_gist_in_editor" },
{ "caption": "GitHub: Open Gist in Browser", "command": "open_gist_in_browser" },
- { "caption": "GitHub: Open Starred Gist in Browser", "command": "open_starred_gist_in_browser" }
+ { "caption": "GitHub: Open Starred Gist in Browser", "command": "open_starred_gist_in_browser" },
+ { "caption": "GitHub: Update Gist", "command": "update_gist" }
]
View
4 README.md
@@ -73,6 +73,10 @@ The following commands are available in the Command Palette:
Displays a quick select panel listing only your starred gists, and selecting one will
open that gist in your default web browser.
+* **GitHub: Update Gist**
+
+ Update the gist open in the current editor.
+
## Issues
View
43 gist.py
@@ -58,6 +58,9 @@ def get_token(cls, username, password):
def post(self, endpoint, data=None):
return self.request('post', endpoint, data=data)
+ def patch(self, endpoint, data=None):
+ return self.request('patch', endpoint, data=data)
+
def get(self, endpoint, params=None):
return self.request('get', endpoint, params=params)
@@ -106,6 +109,13 @@ def create(self, description="", filename=None, content="", public=False):
"files": {filename: {"content": content}}})
return data["html_url"]
+ def update(self, gist, content):
+ filename = gist["files"].keys()[0]
+ resp = self.patch("/gists/" + gist["id"],
+ {"description": gist["description"],
+ "files": {filename: {"content": content}}})
+ return resp["html_url"]
+
def list(self, starred=False):
page = 1
data = []
@@ -227,6 +237,7 @@ def on_done(self, idx):
new_view.insert(edit, 0, content)
new_view.end_edit(edit)
new_view.set_name(filename)
+ new_view.settings().set('gist', gist)
else:
sublime.set_clipboard(content)
sublime.status_message(self.MSG_SUCCESS % filename)
@@ -364,3 +375,35 @@ class PublicGistFromSelectionCommand(GistFromSelectionCommand):
Command to create a public Github gist from the current selection.
"""
public = True
+
+
+class UpdateGistCommand(BaseGistCommand):
+ MSG_SUCCESS = "Gist updated and url copied to the clipboard."
+
+ def run(self, edit):
+ super(UpdateGistCommand, self).run(edit)
+ self.gist = self.view.settings().get('gist')
+ if not self.gist:
+ sublime.error_message("Can't update: this doesn't appear to be a valid gist.")
+ return
+ if self.github_token:
+ self.update()
+ else:
+ self.callback = self.update
+ self.get_token()
+
+ def update(self):
+ text = self.view.substr(sublime.Region(0, self.view.size()))
+ gistapi = GistApi(self.github_token)
+ try:
+ gist_url = gistapi.update(self.gist, text)
+ sublime.set_clipboard(gist_url)
+ sublime.status_message(self.MSG_SUCCESS)
+ except GistApi.UnauthorizedException:
+ # clear out the bad token so we can reset it
+ self.settings.set("github_token", "")
+ sublime.save_settings("GitHub.sublime-settings")
+ sublime.error_message(self.ERR_UNAUTHORIZED)
+ sublime.set_timeout(self.get_username, 50)
+ except GistApi.UnknownException, e:
+ sublime.error_message(e.message)
View
7 sublime_requests.py
@@ -44,7 +44,7 @@ def _build_response(self, text):
response._content = raw_response.read()
return response
- def request(self, method, url, headers=None, params=None, data=None, auth=None, allow_redirects=False):
+ def request(self, method, url, headers=None, params=None, data=None, auth=None, allow_redirects=False, config=None):
curl = commandline.find_binary('curl')
curl_options = ['-i', '-L', '-f', '--user-agent', 'Sublime Github', '-s']
if auth:
@@ -54,8 +54,10 @@ def request(self, method, url, headers=None, params=None, data=None, auth=None,
if headers:
for k, v in headers.iteritems():
curl_options.extend(['-H', "%s: %s" % (k, v)])
- if method == 'post':
+ if method in ('post', 'patch'):
curl_options.extend(['-d', data])
+ if method == 'patch':
+ curl_options.extend(['-X', 'PATCH'])
if params:
url += '?' + '&'.join(['='.join([k, str(v)]) for k, v in params.iteritems()])
@@ -81,6 +83,7 @@ def request(self, method, url, headers=None, params=None, data=None, auth=None,
def post(self, *args, **kwargs):
return self.request("post", *args, **kwargs)
+
def session(verify=None):
if hasattr(httplib, "HTTPSConnection"):
return requests.session(verify=verify)

0 comments on commit ccff278

Please sign in to comment.