crowi-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ require 'crowi/client/client'
@@ -0,0 +1,134 @@
1
+ require_relative 'api_request_base'
2
+
3
+ # 添付ファイル一覧リクエスト用クラス
4
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/attachment.js
5
+ class CPApiRequestAttachmentsList < CPApiRequestBase
6
+
7
+ # コンストラクタ
8
+ # @override
9
+ # @param [Hash] param APIリクエストのパラメータ
10
+ def initialize(param = {})
11
+ super('/_api/attachments.list', METHOD_GET, { page_id: param[:page_id] })
12
+ end
13
+
14
+ # リクエストを実行する
15
+ # @override
16
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
17
+ # @return [CrowiPage] リクエスト実行結果
18
+ def execute(entry_point)
19
+ if invalid?
20
+ return validation_msg
21
+ end
22
+ ret = JSON.parse RestClient.get entry_point, params: @param
23
+ if (ret['ok'] == false)
24
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
25
+ end
26
+ attachments = []
27
+ ret['attachments'].each do |attachment|
28
+ attachments.push(CrowiAttachment.new(attachment))
29
+ end
30
+ return CPApiReturn.new(ok: ret['ok'], data: attachments)
31
+ end
32
+
33
+ protected
34
+
35
+ # バリデーションエラーを取得する
36
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
37
+ def _invalid
38
+ if ! (@param[:page_id])
39
+ CPInvalidRequest.new 'Parameter page_id is required.'
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ # 添付ファイル追加リクエスト用クラス
46
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/attachment.js
47
+ class CPApiRequestAttachmentsAdd < CPApiRequestBase
48
+
49
+ # コンストラクタ
50
+ # @override
51
+ # @param [Hash] param APIリクエストのパラメータ
52
+ def initialize(param = {})
53
+ super('/_api/attachments.add', METHOD_POST,
54
+ { page_id: param[:page_id], file: param[:file] })
55
+ end
56
+
57
+ # リクエストを実行する
58
+ # @override
59
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
60
+ # @return [String] リクエスト実行結果(JSON形式)
61
+ def execute(entry_point)
62
+ if invalid?
63
+ return validation_msg
64
+ end
65
+ req = RestClient::Request.new(
66
+ method: :post,
67
+ url: entry_point,
68
+ payload: {
69
+ access_token: @param[:access_token],
70
+ page_id: @param[:page_id],
71
+ file: @param[:file]
72
+ }
73
+ )
74
+ ret = JSON.parse req.execute
75
+ if (ret['ok'] == false)
76
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
77
+ end
78
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
79
+ end
80
+
81
+ protected
82
+
83
+ # バリデーションエラーを取得する
84
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
85
+ def _invalid
86
+ if ! (@param[:file] && @param[:page_id])
87
+ CPInvalidRequest.new 'Parameters file and page_id are required.'
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+
94
+ # 添付ファイル削除リクエスト用クラス
95
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/attachment.js
96
+ class CPApiRequestAttachmentsRemove < CPApiRequestBase
97
+
98
+ # コンストラクタ
99
+ # @override
100
+ # @param [Hash] param APIリクエストのパラメータ
101
+ def initialize(param = {})
102
+ super('/_api/attachments.remove', METHOD_POST,
103
+ { attachment_id: param[:attachment_id] })
104
+ end
105
+
106
+
107
+ # リクエストを実行する
108
+ # @override
109
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
110
+ # @return [CrowiPage] リクエスト実行結果
111
+ def execute(entry_point)
112
+ if invalid?
113
+ return validation_msg
114
+ end
115
+ ret = JSON.parse RestClient.post entry_point, @param.to_json,
116
+ { content_type: :json, accept: :json }
117
+ if (ret['ok'] == false)
118
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
119
+ end
120
+ return CPApiReturn.new(ok: ret['ok'], data: nil)
121
+ end
122
+
123
+ protected
124
+
125
+ # バリデーションエラーを取得する
126
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
127
+ def _invalid
128
+ if ! (@param[:attachment_id])
129
+ CPInvalidRequest.new 'Parameter attachment_id is required.'
130
+ end
131
+ end
132
+
133
+ end
134
+
@@ -0,0 +1,104 @@
1
+ # APIリクエストのバリデーションエラー
2
+ # @note rescue する必要はないので Exception クラスは継承しない
3
+ class CPInvalidRequest
4
+ attr_reader :msg
5
+
6
+ # コンストラクタ
7
+ # @param [String] msg エラーメッセージ(原因と対策が分かるとよい)
8
+ def initialize(msg)
9
+ @msg = msg
10
+ end
11
+
12
+ # Convert string
13
+ # @return [String] Message
14
+ def to_s
15
+ return @msg
16
+ end
17
+ end
18
+
19
+ # APIリクエストの応答基本クラス
20
+ class CPApiReturn
21
+ attr_accessor :ok, :data
22
+
23
+ # Constractor
24
+ # @param [String] ok Result of API
25
+ def initialize(params = {})
26
+ if (! params[:ok].is_a?(TrueClass) && ! params[:ok].is_a?(FalseClass))
27
+ raise ArgumentError.new('Parameter ok is needed true or false.')
28
+ end
29
+ @ok = params[:ok]
30
+ @data = params[:data]
31
+ end
32
+
33
+ end
34
+
35
+ # APIリクエストの基本クラス
36
+ class CPApiRequestBase
37
+ METHOD_GET = "GET"
38
+ METHOD_POST = "POST"
39
+ attr_reader :entry_point, :method, :param
40
+
41
+ # コンストラクタ
42
+ # @param [String] entry_point APIリクエストのエントリポイントとなるURLパス(ex. '/page.list')
43
+ # @param [Enumerator] method APIリクエストのタイプ
44
+ # @param [Hash] param APIリクエストのパラメータ
45
+ def initialize(entry_point, method, param = {})
46
+ @entry_point = entry_point
47
+ @method = method
48
+ @param = param.reject { |k, v| !v }
49
+ end
50
+
51
+ # パラメータを代入
52
+ # @param [Hash] param APIリクエストのパラメータ
53
+ def param=(param = {})
54
+ @param = param
55
+ end
56
+
57
+ # パラメータのバリデーションチェックを行う
58
+ # @return [true/false] バリデーションチェック結果
59
+ def valid?
60
+ return (!_invalid)
61
+ end
62
+
63
+ # パラメータのバリデーションチェックを行う
64
+ # @return [true/false] バリデーションチェック結果
65
+ def invalid?
66
+ return (_invalid)
67
+ end
68
+
69
+ # バリデーションエラーの説明
70
+ # @return [String] バリデーションエラーの説明
71
+ def validation_msg
72
+ return _invalid&.to_s
73
+ end
74
+
75
+ # リクエストを実行する
76
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
77
+ # @return [String] リクエスト実行結果(CPApiReturnオブジェクト)
78
+ def execute(entry_point)
79
+
80
+ if invalid?
81
+ return validation_msg
82
+ end
83
+
84
+ case @method
85
+ when 'GET'
86
+ ret_json = RestClient.get entry_point, params: @param
87
+ when 'POST'
88
+ ret_json = RestClient.post entry_point, @param.to_json,
89
+ { content_type: :json, accept: :json }
90
+ end
91
+ ret = JSON.parse(ret_json)
92
+ return CPApiReturn.new(ok: ret['ok'], data: ret.reject { |k,v| k == 'ok' })
93
+ end
94
+
95
+ protected
96
+
97
+ # バリデーションエラーを取得する
98
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
99
+ def _invalid
100
+ return CPInvalidRequest "Invalid API request.";
101
+ end
102
+
103
+ end
104
+
@@ -0,0 +1,389 @@
1
+ require_relative 'api_request_base'
2
+ require 'crowi/client/model/crowi_page'
3
+
4
+ # ページ一覧リクエスト用クラス
5
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
6
+ class CPApiRequestPagesList < CPApiRequestBase
7
+
8
+ # コンストラクタ
9
+ # @override
10
+ # @param [Hash] param APIリクエストのパラメータ
11
+ def initialize(param = {})
12
+ super('/_api/pages.list', METHOD_GET,
13
+ { path: param[:path_exp], user: param[:user] })
14
+ end
15
+
16
+ # リクエストを実行する
17
+ # @override
18
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
19
+ # @return [Array] リクエスト実行結果
20
+ def execute(entry_point)
21
+
22
+ if invalid?
23
+ return validation_msg
24
+ end
25
+
26
+ ret = JSON.parse RestClient.get entry_point, params: @param
27
+ if (ret['ok'] == false)
28
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
29
+ end
30
+ pages = []
31
+ ret['pages'].each do |page|
32
+ pages.push(CrowiPage.new(page))
33
+ end
34
+ return CPApiReturn.new(ok: ret['ok'], data: pages)
35
+ end
36
+
37
+ protected
38
+
39
+ # バリデーションエラーを取得する
40
+ # @override
41
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
42
+ def _invalid
43
+ if ! (@param[:path] || @param[:user])
44
+ return CPInvalidRequest.new 'Parameter path or page_id is required.'
45
+ end
46
+ end
47
+
48
+ end
49
+
50
+
51
+ # ページ取得リクエスト用クラス
52
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
53
+ class CPApiRequestPagesGet < CPApiRequestBase
54
+
55
+ # コンストラクタ
56
+ # @override
57
+ # @param [Hash] param APIリクエストのパラメータ
58
+ def initialize(param = {})
59
+ super('/_api/pages.get', METHOD_GET,
60
+ { page_id: param[:page_id],
61
+ path: param[:path], revision_id: param[:revision_id] })
62
+ end
63
+
64
+ # リクエストを実行する
65
+ # @override
66
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
67
+ # @return [CrowiPage] リクエスト実行結果
68
+ def execute(entry_point)
69
+
70
+ if invalid?
71
+ return validation_msg
72
+ end
73
+ ret = JSON.parse RestClient.get entry_point, params: @param
74
+ if (ret['ok'] == false)
75
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
76
+ end
77
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
78
+ end
79
+
80
+ protected
81
+
82
+ # バリデーションエラーを取得する
83
+ # @override
84
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
85
+ def _invalid
86
+ if ! (@param[:path] || @param[:page_id])
87
+ return CPInvalidRequest.new 'Parameter path or page_id is required.'
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+
94
+ # ページ作成リクエスト用クラス
95
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
96
+ class CPApiRequestPagesCreate < CPApiRequestBase
97
+
98
+ # コンストラクタ
99
+ # @override
100
+ # @param [Hash] param APIリクエストのパラメータ
101
+ def initialize(param = {})
102
+ super('/_api/pages.create', METHOD_POST,
103
+ { body: param[:body], path: param[:path], grant: param[:grant] })
104
+ end
105
+
106
+ # リクエストを実行する
107
+ # @override
108
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
109
+ # @return [CrowiPage] リクエスト実行結果
110
+ def execute(entry_point)
111
+
112
+ if invalid?
113
+ return validation_msg
114
+ end
115
+ ret = JSON.parse RestClient.post entry_point, @param.to_json,
116
+ { content_type: :json, accept: :json }
117
+ if (ret['ok'] == false)
118
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
119
+ end
120
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
121
+ end
122
+
123
+ protected
124
+
125
+ # バリデーションエラーを取得する
126
+ # @override
127
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
128
+ def _invalid
129
+ if ! (@param[:body] && @param[:path])
130
+ return CPInvalidRequest.new 'Parameters body and path are required.'
131
+ end
132
+ end
133
+
134
+ end
135
+
136
+ # ページ更新リクエスト用クラス
137
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
138
+ class CPApiRequestPagesUpdate < CPApiRequestBase
139
+
140
+ # コンストラクタ
141
+ # @override
142
+ # @param [Hash] param APIリクエストのパラメータ
143
+ def initialize(param = {})
144
+ super('/_api/pages.update', METHOD_POST,
145
+ { body: param[:body], page_id: param[:page_id],
146
+ revision_id: param[:revision_id], grant: param[:grant] })
147
+ end
148
+
149
+ # リクエストを実行する
150
+ # @override
151
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
152
+ # @return [CrowiPage] リクエスト実行結果
153
+ def execute(entry_point)
154
+
155
+ if invalid?
156
+ return validation_msg
157
+ end
158
+ ret = JSON.parse RestClient.post entry_point, @param.to_json,
159
+ { content_type: :json, accept: :json }
160
+ if (ret['ok'] == false)
161
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
162
+ end
163
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
164
+ end
165
+
166
+ protected
167
+
168
+ # バリデーションエラーを取得する
169
+ # @override
170
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
171
+ def _invalid
172
+ if ! (@param[:page_id] && @param[:body])
173
+ return CPInvalidRequest.new 'Parameters page_id and body are required.'
174
+ end
175
+ end
176
+
177
+ end
178
+
179
+ # ページ閲覧済マークを付与リクエスト用クラス
180
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
181
+ # @note 詳細不明
182
+ class CPApiRequestPagesSeen < CPApiRequestBase
183
+
184
+ # コンストラクタ
185
+ # @override
186
+ # @param [Hash] param APIリクエストのパラメータ
187
+ def initialize(param = {})
188
+ super('/_api/pages.seen', METHOD_POST, { page_id: param[:page_id] })
189
+ end
190
+
191
+ # リクエストを実行する
192
+ # @override
193
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
194
+ # @return [CrowiPage] リクエスト実行結果
195
+ def execute(entry_point)
196
+
197
+ if invalid?
198
+ return validation_msg
199
+ end
200
+ ret = JSON.parse RestClient.post entry_point, @param.to_json,
201
+ { content_type: :json, accept: :json }
202
+ if (ret['ok'] == false)
203
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
204
+ end
205
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['seenUser']))
206
+ end
207
+
208
+ protected
209
+
210
+ # バリデーションエラーを取得する
211
+ # @override
212
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
213
+ def _invalid
214
+ if ! (@param[:page_id])
215
+ return CPInvalidRequest.new 'Parameter page_id required.'
216
+ end
217
+ end
218
+
219
+ end
220
+
221
+ # ライクページ指定リクエスト用クラス
222
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
223
+ class CPApiRequestLikesAdd < CPApiRequestBase
224
+
225
+ # コンストラクタ
226
+ # @override
227
+ # @param [Hash] param APIリクエストのパラメータ
228
+ def initialize(param = {})
229
+ super('/_api/likes.add', METHOD_POST, { page_id: param[:page_id] })
230
+ end
231
+
232
+ # リクエストを実行する
233
+ # @override
234
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
235
+ # @return [CrowiPage] リクエスト実行結果
236
+ def execute(entry_point)
237
+
238
+ if invalid?
239
+ return validation_msg
240
+ end
241
+ ret = JSON.parse RestClient.post entry_point, @param.to_json,
242
+ { content_type: :json, accept: :json }
243
+ if (ret['ok'] == false)
244
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
245
+ end
246
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
247
+ end
248
+
249
+ protected
250
+
251
+ # バリデーションエラーを取得する
252
+ # @override
253
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
254
+ def _invalid
255
+ if ! (@param[:page_id])
256
+ return CPInvalidRequest.new 'Parameter page_id required.'
257
+ end
258
+ end
259
+
260
+ end
261
+
262
+ # ライクページ指定解除リクエスト用クラス
263
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
264
+ class CPApiRequestLikesRemove < CPApiRequestBase
265
+
266
+ # コンストラクタ
267
+ # @override
268
+ # @param [Hash] param APIリクエストのパラメータ
269
+ def initialize(param = {})
270
+ super('/_api/likes.remove', METHOD_POST, { page_id: param[:page_id] })
271
+ end
272
+
273
+ # リクエストを実行する
274
+ # @override
275
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
276
+ # @return [CrowiPage] リクエスト実行結果
277
+ def execute(entry_point)
278
+
279
+ if invalid?
280
+ return validation_msg
281
+ end
282
+ ret = JSON.parse RestClient.post entry_point, @param.to_json,
283
+ { content_type: :json, accept: :json }
284
+ if (ret['ok'] == false)
285
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
286
+ end
287
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
288
+ end
289
+
290
+ protected
291
+
292
+ # バリデーションエラーを取得する
293
+ # @override
294
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
295
+ def _invalid
296
+ if ! (@param[:page_id])
297
+ return CPInvalidRequest.new 'Parameter page_id required.'
298
+ end
299
+ end
300
+
301
+ end
302
+
303
+ # 更新ページ一覧リクエスト用クラス
304
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
305
+ # @note notification for 3rd party tool (like Slack)
306
+ class CPApiRequestPagesUpdatePost < CPApiRequestBase
307
+
308
+ # コンストラクタ
309
+ # @override
310
+ # @param [Hash] param APIリクエストのパラメータ
311
+ def initialize(param = {})
312
+ super('/_api/pages.updatePost', METHOD_GET, { path: param[:path] })
313
+ end
314
+
315
+ # リクエストを実行する
316
+ # @override
317
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
318
+ # @return [CrowiPage] リクエスト実行結果
319
+ def execute(entry_point)
320
+
321
+ if invalid?
322
+ return validation_msg
323
+ end
324
+ ret = JSON.parse RestClient.get entry_point, params: @param
325
+ if (ret['ok'] == false)
326
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
327
+ end
328
+ posts = []
329
+ ret['updatePost'].each do |post|
330
+ pages.push(CrowiPage.new(post))
331
+ end
332
+ return CPApiReturn.new(ok: ret['ok'], data: posts)
333
+ end
334
+
335
+ protected
336
+
337
+ # バリデーションエラーを取得する
338
+ # @override
339
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
340
+ def _invalid
341
+ if ! (@param[:path])
342
+ return CPInvalidRequest.new 'Parameter path required.'
343
+ end
344
+ end
345
+
346
+ end
347
+
348
+ # ページ削除リクエスト用クラス(API利用不可)
349
+ # @ref https://github.com/crowi/crowi/blob/master/lib/routes/page.js
350
+ class CPApiRequestPagesRemove < CPApiRequestBase
351
+
352
+ # コンストラクタ
353
+ # @override
354
+ # @param [Hash] param APIリクエストのパラメータ
355
+ def initialize(param = {})
356
+ raise Exception, 'API of pages.remove is forbidden'
357
+ super('/_api/pages.remove', METHOD_GET,
358
+ { page_id: param[:page_id], revision_id: param[:revision_id] })
359
+ end
360
+
361
+ # リクエストを実行する
362
+ # @override
363
+ # @param [String] entry_point APIのエントリーポイントとなるURL(ex. http://localhost:3000/_api/pages.list)
364
+ # @return [CrowiPage] リクエスト実行結果
365
+ def execute(entry_point)
366
+
367
+ if invalid?
368
+ return validation_msg
369
+ end
370
+ ret = JSON.parse RestClient.get entry_point, params: @param
371
+ if (ret['ok'] == false)
372
+ return CPInvalidRequest.new "API return false with msg: #{ret['msg']}"
373
+ end
374
+ return CPApiReturn.new(ok: ret['ok'], data: CrowiPage.new(ret['page']))
375
+ end
376
+
377
+ protected
378
+
379
+ # バリデーションエラーを取得する
380
+ # @override
381
+ # @return [nil/CPInvalidRequest] バリデーションエラー結果
382
+ def _invalid
383
+ if ! (@param[:page_id] || @param[:revision_id])
384
+ return CPInvalidRequest.new 'Parameter page_id or revision_id is required.'
385
+ end
386
+ end
387
+
388
+ end
389
+