/
gh-gist.el
181 lines (150 loc) · 6.01 KB
/
gh-gist.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
;;; gh-gist.el --- gist module for gh.el
;; Copyright (C) 2011 Yann Hodique
;; Author: Yann Hodique <yann.hodique@gmail.com>
;; Keywords:
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;;; Code:
(require 'eieio)
(require 'gh-api)
(require 'gh-auth)
(require 'gh-common)
;;;###autoload
(defclass gh-gist-api (gh-api-v3)
()
"Gist API")
;;;###autoload
(defclass gh-gist-gist-stub ()
((files :initarg :files :type list :initform nil)
(public :initarg :public)
(description :initarg :description))
"Class for user-created gist objects")
;;;###autoload
(defclass gh-gist-gist (gh-gist-gist-stub)
((date :initarg :date)
(update :initarg :update)
(push-url :initarg :push-url)
(pull-url :initarg :pull-url)
(html-url :initarg :html-url)
(comments :initarg :comments)
(user :initarg :user :initform nil)
(id :initarg :id :type string)
(url :initarg :url :type string)
(history :initarg :history :initform nil)
(forks :initarg :forks :initform nil))
"Gist object")
(defclass gh-gist-gist-file ()
((filename :initarg :filename)
(size :initarg :size)
(url :initarg :url)
(content :initarg :content)))
(defun gh-gist-read-list (gists)
(mapcar 'gh-gist-read gists))
(defun gh-gist-file-read (file &optional into)
(let* ((label (car file))
(file (cdr file))
(target (or into (gh-gist-gist-file label))))
(with-slots (filename size url content)
target
(setq
filename (gh-read file 'filename)
size (gh-read file 'size)
url (gh-read file 'raw_url)
content (gh-read file 'content)))
target))
(defun gh-gist-files-read (files)
(mapcar 'gh-gist-file-read files))
(defun gh-gist-read (gist &optional into)
(let ((target (or into (gh-gist-gist "gist"))))
(with-slots (date update push-url pull-url html-url comments files user
public description id url history forks)
target
(setq date (gh-read gist 'created_at)
update (gh-read gist 'updated_at)
push-url (gh-read gist 'git_push_url)
pull-url (gh-read gist 'git_pull_url)
html-url (gh-read gist 'html_url)
comments (gh-read gist 'comments)
files (gh-gist-files-read (gh-read gist 'files))
user (gh-user-read (gh-read gist 'user) (oref target :user))
public (gh-read gist 'public)
description (gh-read gist 'description)
id (gh-read gist 'id)
url (gh-read gist 'url)
history (gh-read gist 'history)
forks (gh-read gist 'forks)))
target))
(defmethod gh-gist-gist-to-obj ((gist gh-gist-gist-stub))
`(("description" . ,(oref gist :description))
("public" . ,(oref gist :public))
("files" . ,(mapcar 'gh-gist-gist-file-to-obj (oref gist :files)))))
(defmethod gh-gist-gist-has-files ((gist gh-gist-gist-stub))
(not (memq nil (mapcar (lambda (f)
(oref f :content)) (oref gist :files)))))
(defmethod gh-gist-gist-file-to-obj ((file gh-gist-gist-file))
`(,(oref file :filename) . (("content" . ,(oref file :content)))))
(defmethod gh-gist-list ((api gh-gist-api) &optional username)
(gh-api-authenticated-request
api 'gh-gist-read-list "GET"
(format "/users/%s/gists" (or username (gh-api-get-username api)))))
(defmethod gh-gist-list-public ((api gh-gist-api))
(gh-api-authenticated-request
api 'gh-gist-read-list "GET" "/gists/public"))
(defmethod gh-gist-list-starred ((api gh-gist-api))
(gh-api-authenticated-request
api 'gh-gist-read-list "GET" "/gists/starred"))
(defmethod gh-gist-get ((api gh-gist-api) gist-or-id)
(let (id transformer)
(if (stringp gist-or-id)
(setq id gist-or-id
transformer 'gh-gist-read)
(setq id (oref gist-or-id :id)
transformer (lambda (g)
(gh-gist-read g gist-or-id))))
(gh-api-authenticated-request
api transformer "GET" (format "/gists/%s" id))))
(defmethod gh-gist-new ((api gh-gist-api) gist-stub)
(gh-api-authenticated-request
api 'gh-gist-read "POST" (format "/users/%s/gists"
(gh-api-get-username api))
(gh-gist-gist-to-obj gist-stub)))
(defmethod gh-gist-edit ((api gh-gist-api) gist)
(gh-api-authenticated-request
api 'gh-gist-read "PATCH" (format "/gists/%s"
(oref gist :id))
(gh-gist-gist-to-obj gist)))
(defmethod gh-gist-set-star ((api gh-gist-api) gist-or-id star)
(let ((id (if (stringp gist-or-id) gist-or-id
(oref gist-or-id :id))))
(gh-api-authenticated-request
api 'ignore (if star "POST" "DELETE")
(format "/gists/%s/star" id))))
(defmethod gh-gist-get-star ((api gh-gist-api) gist-or-id)
(let ((id (if (stringp gist-or-id) gist-or-id
(oref gist-or-id :id))))
(gh-api-authenticated-request
api 'ignore "GET" (format "/gists/%s/star" id))))
(defmethod gh-gist-fork ((api gh-gist-api) gist-or-id)
(let ((id (if (stringp gist-or-id) gist-or-id
(oref gist-or-id :id))))
(gh-api-authenticated-request
api 'gh-gist-read "POST" (format "/gists/%s/fork" id))))
(defmethod gh-gist-delete ((api gh-gist-api) gist-or-id)
(let ((id (if (stringp gist-or-id) gist-or-id
(oref gist-or-id :id))))
(gh-api-authenticated-request
api 'ignore "DELETE" (format "/gists/%s" id))))
(provide 'gh-gist)
;;; gh-gist.el ends here