-
Notifications
You must be signed in to change notification settings - Fork 74
/
gh-gist.el
171 lines (138 loc) · 6.22 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
;;; gh-gist.el --- gist module for gh.el -*- lexical-binding: t; -*-
;; Copyright (C) 2011 Yann Hodique
;; Author: Yann Hodique <[email protected]>
;; 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)
(defclass gh-gist-api (gh-api-v3)
((gist-cls :allocation :class :initform gh-gist-gist))
"Gist API")
(gh-defclass gh-gist-gist-stub (gh-object)
((files :initarg :files :type list :initform nil :marshal-type (list gh-gist-gist-file))
(public :initarg :public :marshal-type bool)
(description :initarg :description))
"Class for user-created gist objects")
(gh-defclass gh-gist-history-change (gh-object)
((total :initarg :total)
(additions :initarg :additions)
(deletions :initarg :deletions)))
(gh-defclass gh-gist-history-entry (gh-object)
((user :initarg :user :initform nil :marshal-type gh-user)
(version :initarg :version)
(committed :initarg :committed :marshal ((alist . committed_at)))
(change :initarg :change :marshal ((alist . change_status))
:marshal-type gh-gist-history-change)
(url :initarg :url)))
(gh-defclass gh-gist-fork-entry (gh-ref-object)
((user :initarg :user :initform nil :marshal-type gh-user)
(created :initarg :created :marshal ((alist . created_at)))
(updated :initarg :updated :marshal ((alist . updated_at)))))
(gh-defclass gh-gist-gist (gh-ref-object gh-gist-gist-stub)
((date :initarg :date :marshal ((alist . created_at)))
(update :initarg :update :marshal ((alist . updated_at)))
(push-url :initarg :push-url :marshal ((alist . git_push_url)))
(pull-url :initarg :pull-url :marshal ((alist . git_pull_url)))
(comments :initarg :comments)
(user :initarg :user :initform nil :marshal-type gh-user :marshal ((alist . owner)))
(history :initarg :history :initform nil :type list :marshal-type (list gh-gist-history-entry))
(forks :initarg :forks :initform nil :type list :marshal-type (list gh-gist-fork-entry)))
"Gist object")
(gh-defclass gh-gist-gist-file (gh-object)
((filename :initarg :filename)
(size :initarg :size)
(url :initarg :url :marshal ((alist . raw_url)))
(content :initarg :content)))
(cl-defmethod make-instance ((cls (subclass gh-gist-gist-file)) &rest args)
(let ((obj (cl-call-next-method)))
(when (oref obj :content)
(oset obj :content (gh-sanitize-content (oref obj :content))))
obj))
(cl-defmethod gh-gist-gist-to-obj ((gist gh-gist-gist-stub))
(let ((files (mapcar #'gh-gist-gist-file-to-obj (oref gist :files))))
`(("description" . ,(oref gist :description))
("public" . ,(oref gist :public))
,@(and files (list (cons "files" files))))))
(cl-defmethod gh-gist-gist-has-files ((gist gh-gist-gist-stub))
(not (memq nil (mapcar (lambda (f)
(oref f :content)) (oref gist :files)))))
(cl-defmethod gh-gist-gist-file-to-obj ((file gh-gist-gist-file))
(let* ((filename (oref file :filename))
(content (oref file :content))
(file (if content
`(("filename" . ,filename)
("content" . ,content))
nil)))
(cons filename file)))
(cl-defmethod gh-gist-list ((api gh-gist-api) &optional username)
(gh-api-authenticated-request
api (gh-object-list-reader (oref api gist-cls)) "GET"
(format "/users/%s/gists" (or username (gh-api-get-username api)))))
(cl-defmethod gh-gist-list-public ((api gh-gist-api))
(gh-api-authenticated-request
api (gh-object-list-reader (oref api gist-cls)) "GET" "/gists/public"))
(cl-defmethod gh-gist-list-starred ((api gh-gist-api))
(gh-api-authenticated-request
api (gh-object-list-reader (oref api gist-cls)) "GET" "/gists/starred"))
(cl-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-object-reader (oref api gist-cls)))
(setq id (oref gist-or-id :id)
transformer (gh-object-reader gist-or-id)))
(gh-api-authenticated-request
api transformer "GET" (format "/gists/%s" id))))
(cl-defmethod gh-gist-new ((api gh-gist-api) gist-stub)
(gh-api-authenticated-request
api (gh-object-reader (oref api gist-cls)) "POST" "/gists"
(gh-gist-gist-to-obj gist-stub)))
(cl-defmethod gh-gist-edit ((api gh-gist-api) gist)
(gh-api-authenticated-request
api (gh-object-reader (oref api gist-cls)) "PATCH"
(format "/gists/%s"
(oref gist :id))
(gh-gist-gist-to-obj gist)))
(cl-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 "PUT" "DELETE")
(format "/gists/%s/star" id))))
(cl-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))))
(cl-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-object-reader (oref api gist-cls)) "POST"
(format "/gists/%s/forks" id))))
(cl-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
;; Local Variables:
;; indent-tabs-mode: nil
;; End: