Skip to content
This repository
Browse code

Update Gist command.

(Issue #3)
  • Loading branch information...
commit ccff27894a04aa16716cf78492a8d74d01e1be83 1 parent 7dfb42c
Brad Greenlee authored March 02, 2012
3  Github.sublime-commands
@@ -6,5 +6,6 @@
6 6
     { "caption": "GitHub: Open Gist in Editor", "command": "open_gist_in_editor" },
7 7
     { "caption": "GitHub: Open Starred Gist in Editor", "command": "open_starred_gist_in_editor" },
8 8
     { "caption": "GitHub: Open Gist in Browser", "command": "open_gist_in_browser" },
9  
-    { "caption": "GitHub: Open Starred Gist in Browser", "command": "open_starred_gist_in_browser" }
  9
+    { "caption": "GitHub: Open Starred Gist in Browser", "command": "open_starred_gist_in_browser" },
  10
+    { "caption": "GitHub: Update Gist", "command": "update_gist" }
10 11
 ]
4  README.md
Source Rendered
@@ -73,6 +73,10 @@ The following commands are available in the Command Palette:
73 73
     Displays a quick select panel listing only your starred gists, and selecting one will
74 74
     open that gist in your default web browser.
75 75
 
  76
+* **GitHub: Update Gist**
  77
+
  78
+    Update the gist open in the current editor.
  79
+
76 80
 
77 81
 ## Issues
78 82
 
43  gist.py
@@ -58,6 +58,9 @@ def get_token(cls, username, password):
58 58
     def post(self, endpoint, data=None):
59 59
         return self.request('post', endpoint, data=data)
60 60
 
  61
+    def patch(self, endpoint, data=None):
  62
+        return self.request('patch', endpoint, data=data)
  63
+
61 64
     def get(self, endpoint, params=None):
62 65
         return self.request('get', endpoint, params=params)
63 66
 
@@ -106,6 +109,13 @@ def create(self, description="", filename=None, content="", public=False):
106 109
                                      "files": {filename: {"content": content}}})
107 110
         return data["html_url"]
108 111
 
  112
+    def update(self, gist, content):
  113
+        filename = gist["files"].keys()[0]
  114
+        resp = self.patch("/gists/" + gist["id"],
  115
+                           {"description": gist["description"],
  116
+                            "files": {filename: {"content": content}}})
  117
+        return resp["html_url"]
  118
+
109 119
     def list(self, starred=False):
110 120
         page = 1
111 121
         data = []
@@ -227,6 +237,7 @@ def on_done(self, idx):
227 237
             new_view.insert(edit, 0, content)
228 238
             new_view.end_edit(edit)
229 239
             new_view.set_name(filename)
  240
+            new_view.settings().set('gist', gist)
230 241
         else:
231 242
             sublime.set_clipboard(content)
232 243
             sublime.status_message(self.MSG_SUCCESS % filename)
@@ -364,3 +375,35 @@ class PublicGistFromSelectionCommand(GistFromSelectionCommand):
364 375
     Command to create a public Github gist from the current selection.
365 376
     """
366 377
     public = True
  378
+
  379
+
  380
+class UpdateGistCommand(BaseGistCommand):
  381
+    MSG_SUCCESS = "Gist updated and url copied to the clipboard."
  382
+
  383
+    def run(self, edit):
  384
+        super(UpdateGistCommand, self).run(edit)
  385
+        self.gist = self.view.settings().get('gist')
  386
+        if not self.gist:
  387
+            sublime.error_message("Can't update: this doesn't appear to be a valid gist.")
  388
+            return
  389
+        if self.github_token:
  390
+            self.update()
  391
+        else:
  392
+            self.callback = self.update
  393
+            self.get_token()
  394
+
  395
+    def update(self):
  396
+        text = self.view.substr(sublime.Region(0, self.view.size()))
  397
+        gistapi = GistApi(self.github_token)
  398
+        try:
  399
+            gist_url = gistapi.update(self.gist, text)
  400
+            sublime.set_clipboard(gist_url)
  401
+            sublime.status_message(self.MSG_SUCCESS)
  402
+        except GistApi.UnauthorizedException:
  403
+            # clear out the bad token so we can reset it
  404
+            self.settings.set("github_token", "")
  405
+            sublime.save_settings("GitHub.sublime-settings")
  406
+            sublime.error_message(self.ERR_UNAUTHORIZED)
  407
+            sublime.set_timeout(self.get_username, 50)
  408
+        except GistApi.UnknownException, e:
  409
+            sublime.error_message(e.message)
7  sublime_requests.py
@@ -44,7 +44,7 @@ def _build_response(self, text):
44 44
         response._content = raw_response.read()
45 45
         return response
46 46
 
47  
-    def request(self, method, url, headers=None, params=None, data=None, auth=None, allow_redirects=False):
  47
+    def request(self, method, url, headers=None, params=None, data=None, auth=None, allow_redirects=False, config=None):
48 48
         curl = commandline.find_binary('curl')
49 49
         curl_options = ['-i', '-L', '-f', '--user-agent', 'Sublime Github', '-s']
50 50
         if auth:
@@ -54,8 +54,10 @@ def request(self, method, url, headers=None, params=None, data=None, auth=None,
54 54
         if headers:
55 55
             for k, v in headers.iteritems():
56 56
                 curl_options.extend(['-H', "%s: %s" % (k, v)])
57  
-        if method == 'post':
  57
+        if method in ('post', 'patch'):
58 58
             curl_options.extend(['-d', data])
  59
+        if method == 'patch':
  60
+            curl_options.extend(['-X', 'PATCH'])
59 61
         if params:
60 62
             url += '?' + '&'.join(['='.join([k, str(v)]) for k, v in params.iteritems()])
61 63
 
@@ -81,6 +83,7 @@ def request(self, method, url, headers=None, params=None, data=None, auth=None,
81 83
     def post(self, *args, **kwargs):
82 84
         return self.request("post", *args, **kwargs)
83 85
 
  86
+
84 87
 def session(verify=None):
85 88
     if hasattr(httplib, "HTTPSConnection"):
86 89
         return requests.session(verify=verify)

0 notes on commit ccff278

Please sign in to comment.
Something went wrong with that request. Please try again.