qiita-sdk 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "qiita/sdk"
3
+ require 'bundler/setup'
4
+ require 'qiita/sdk'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "qiita/sdk"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
@@ -1,12 +1,12 @@
1
- require "qiita/sdk/version"
1
+ require 'qiita/sdk/version'
2
+ require 'qiita/sdk/api_actions'
2
3
  require 'qiita/sdk/client'
3
4
  require 'qiita/sdk/httpclient'
5
+ require 'pry'
6
+
4
7
 
5
8
  module Qiita
6
9
  module Sdk
7
10
  class Error < StandardError; end
8
- def self.greet
9
- p "hello"
10
- end
11
11
  end
12
12
  end
@@ -0,0 +1,320 @@
1
+ module Qiita
2
+ module Sdk
3
+ module ApiActions
4
+ # 記事につけられた「LGTM!」一覧
5
+ def fetch_item_likes(item_id:)
6
+ path = "/api/v2/items/#{item_id}/likes"
7
+
8
+ get(path)
9
+ end
10
+
11
+ # コメントを削除
12
+ def delete_comment(comment_id:)
13
+ path = "/api/v2/comments/#{comment_id}"
14
+
15
+ delete(path)
16
+ end
17
+
18
+ # コメントを取得
19
+ def fetch_comment(comment_id:)
20
+ path = "/api/v2/comments/#{comment_id}"
21
+
22
+ get(path)
23
+ end
24
+
25
+ # コメントを更新
26
+ def update_comment(comment_id:, body:)
27
+ path = "/api/v2/comments/#{comment_id}"
28
+
29
+ params = {
30
+ body: body
31
+ }
32
+
33
+ patch(path, params)
34
+ end
35
+
36
+ # 投稿に付けられたコメント一覧
37
+ def fetch_item_comments(item_id:)
38
+ path = "/api/v2/items/#{item_id}/comments"
39
+
40
+ get(path)
41
+ end
42
+
43
+ # 記事に対してコメントを投稿
44
+ def post_comment(item_id:, body:)
45
+ path = "/api/v2/items/#{item_id}/comments"
46
+
47
+ params = {
48
+ body: body
49
+ }
50
+
51
+ post(path, params)
52
+ end
53
+
54
+ # タグを取得
55
+ def fetch_tag(tag_id:)
56
+ path = "/api/v2/tags/#{tag_id}"
57
+
58
+ get(path)
59
+ end
60
+
61
+ # ユーザがフォローしているタグ一覧
62
+ def fetch_following_tags(user_id:)
63
+ path = "/api/v2/users/#{user_id}/following_tags"
64
+
65
+ get(path)
66
+ end
67
+
68
+ # タグのフォローを外す
69
+ def delete_tag_following(tag_id:)
70
+ path = "/api/v2/tags/#{tag_id}/following"
71
+
72
+ delete(path)
73
+ end
74
+
75
+ # タグをフォローしているかどうかを調る
76
+ def check_tag_following(tag_id:)
77
+ path = "/api/v2/tags/#{tag_id}/following"
78
+
79
+ get(path)
80
+ end
81
+
82
+ # タグをフォロー
83
+ def follow_tag(tag_id:)
84
+ path = "/api/v2/tags/#{tag_id}/following"
85
+
86
+ put(path, {})
87
+ end
88
+
89
+ # 記事をストックしているユーザ一覧を、ストックした日時の降順で返す
90
+ def fetch_item_stockers(item_id:)
91
+ path = "/api/v2/items/#{item_id}/stockers"
92
+
93
+ get(path)
94
+ end
95
+
96
+ # 全てのユーザの一覧を作成日時の降順で取得
97
+ def fetch_users
98
+ path = '/api/v2/users'
99
+
100
+ get(path)
101
+ end
102
+
103
+ # ユーザを取得
104
+ def fetch_user(user_id:)
105
+ path = "/api/v2/users/#{user_id}"
106
+
107
+ get(path)
108
+ end
109
+
110
+ # ユーザがフォローしているユーザ一覧を取得
111
+ def fetch_followees(user_id:)
112
+ path = "/api/v2/users/#{user_id}/followees"
113
+
114
+ get(path)
115
+ end
116
+
117
+ # ユーザをフォローしているユーザ一覧を取得
118
+ def fetch_followers(user_id:)
119
+ path = "/api/v2/users/#{user_id}/followers"
120
+
121
+ get(path)
122
+ end
123
+
124
+ # ユーザへのフォローを外します。
125
+ def delete_following(user_id:)
126
+ path = "/api/v2/users/#{user_id}/following"
127
+
128
+ delete(path)
129
+ end
130
+
131
+ # ユーザをフォローしている場合に204を返す
132
+ def check_following(user_id:)
133
+ path = "/api/v2/users/#{user_id}/following"
134
+
135
+ get(path)
136
+ end
137
+
138
+ # ユーザをフォロー
139
+ def follow_user(user_id:)
140
+ path = "/api/v2/users/#{user_id}/following"
141
+
142
+ put(path, {})
143
+ end
144
+
145
+ # 認証中のユーザの記事の一覧を作成日時の降順で返す
146
+ def fetch_my_items(per_page: 100, page: 1)
147
+ path = '/api/v2/authenticated_user/items'
148
+
149
+ params = {
150
+ per_page: per_page,
151
+ page: page
152
+ }
153
+
154
+ get(path, params)
155
+ end
156
+
157
+ # 記事の一覧を作成日時の降順で返す
158
+ def fetch_items(per_page: 100, page: 1)
159
+ path = '/api/v2/items'
160
+
161
+ params = {
162
+ per_page: per_page,
163
+ page: page
164
+ }
165
+
166
+ get(path, params)
167
+ end
168
+
169
+ # 新たに記事を作成
170
+ def post_item(title:, body:, tweet: false, tags: [], restricted: false)
171
+ path = '/api/v2/items'
172
+
173
+ params = {
174
+ title: title,
175
+ body: body,
176
+ tweet: tweet,
177
+ tags: tags,
178
+ private: restricted
179
+ }
180
+
181
+ post(path, params)
182
+ end
183
+
184
+ # 記事を削除
185
+ def delete_item(item_id:)
186
+ path = "/api/v2/items/#{item_id}"
187
+
188
+ delete(path)
189
+ end
190
+
191
+ # 記事を取得
192
+ def fetch_item(item_id:)
193
+ path = "/api/v2/items/#{item_id}"
194
+
195
+ get(path)
196
+ end
197
+
198
+ # 記事を更新
199
+ def update_item(item_id:, title: nil, body: nil, restricted: nil, tags: nil)
200
+ path = "/api/v2/items/#{item_id}"
201
+ params = {
202
+ item_id: item_id,
203
+ title: title,
204
+ body: body,
205
+ private: restricted,
206
+ tags: tags
207
+ }
208
+
209
+ patch(path, params)
210
+ end
211
+
212
+ # 記事をストック
213
+ def stock_item(item_id:)
214
+ path = "/api/v2/items/#{item_id}/stock"
215
+
216
+ put(path, {})
217
+ end
218
+
219
+ # 記事をストックから取り除く
220
+ def delete_stock(item_id:)
221
+ path = "/api/v2/items/#{item_id}/stock"
222
+
223
+ delete(path)
224
+ end
225
+
226
+ # 記事をストックしているかどうか調べる
227
+ def check_item_stock(item_id:)
228
+ path = "/api/v2/items/#{item_id}/stock"
229
+
230
+ get(path)
231
+ end
232
+
233
+ # タグの記事一覧
234
+ def fetch_tag_items(tag_id:)
235
+ path = "/api/v2/tags/#{tag_id}/items"
236
+
237
+ get(path)
238
+ end
239
+
240
+ # 指定されたユーザの記事一覧
241
+ def fetch_user_items(user_id:, per_page: 100, page: 1)
242
+ path = "/api/v2/users/#{user_id}/items"
243
+
244
+ params = {
245
+ per_page: per_page,
246
+ page: page
247
+ }
248
+
249
+ get(path, params)
250
+ end
251
+
252
+ # 指定されたユーザがストックした記事一覧
253
+ def fetch_user_stocks(user_id:, per_page: 100, per: 1)
254
+ path = "/api/v2/users/#{user_id}/stocks"
255
+
256
+ params = {
257
+ per_page: per_page,
258
+ page: page
259
+ }
260
+
261
+ get(path, params)
262
+ end
263
+
264
+ # コメントに絵文字リアクションを付ける
265
+ def attach_reaction_to_comment(comment_id:, name:)
266
+ path = "/api/v2/comments/#{comment_id}/reactions"
267
+ params = {
268
+ name: name
269
+ }
270
+
271
+ post(path, params)
272
+ end
273
+
274
+ # 記事に絵文字リアクションを付ける
275
+ def attach_reaction_to_item(item_id:, name:)
276
+ path = "/api/v2/items/#{item_id}/reactions"
277
+ params = {
278
+ name: name
279
+ }
280
+
281
+ post(path, params)
282
+ end
283
+
284
+ # コメントから絵文字リアクションを削除
285
+ def delete_comment_reaction(comment_id:, reaction_name:)
286
+ path = "/api/v2/comments/#{comment_id}/reactions/#{reaction_name}"
287
+
288
+ delete(path)
289
+ end
290
+
291
+ # 記事から絵文字リアクションを削除
292
+ def delete_item_reaction(item_id:, reaction_name:)
293
+ path = "/api/v2/items/#{item_id}/reactions/#{reaction_name}"
294
+
295
+ delete(path)
296
+ end
297
+
298
+ # コメントに付けられた絵文字リアクション一覧
299
+ def fetch_comment_reactions(comment_id:)
300
+ path = "/api/v2/comments/#{comment_id}/reactions"
301
+
302
+ get(path)
303
+ end
304
+
305
+ # 記事に付けられた絵文字リアクション一覧
306
+ def fetch_item_reactions(item_id:)
307
+ path = "/api/v2/items/#{item_id}/reactions"
308
+
309
+ get(path)
310
+ end
311
+
312
+ # アクセストークンに紐付いたユーザを返す
313
+ def fetch_authenticated_user
314
+ path = '/api/v2/authenticated_user'
315
+
316
+ get(path)
317
+ end
318
+ end
319
+ end
320
+ end
@@ -5,366 +5,65 @@ module Qiita
5
5
  require 'uri'
6
6
  require 'json'
7
7
 
8
- API_BASE_URL = 'https://qiita.com'
8
+ include Qiita::Sdk::ApiActions
9
9
 
10
- DEFAULT_HEADERS = {
11
- "Accept" => "application/json",
12
- "Content-Type" => "application/json"
13
- }
10
+ # @return [String]
11
+ attr_accessor :access_token
14
12
 
15
- def initialize(access_token: nil)
16
- @access_token = access_token
13
+ API_BASE_URL = 'https://qiita.com'.freeze
14
+
15
+ def initialize(options = {})
16
+ options.each do |key, value|
17
+ instance_variable_set("@#{key}", value)
18
+ end
19
+ yield(self) if block_given?
20
+ end
21
+
22
+ def credential
23
+ "Bearer #{@access_token}" if @access_token
17
24
  end
18
25
 
19
- def default_headers
20
- headers = DEFAULT_HEADERS.clone
21
- headers["Authorization"] = "Bearer #{@access_token}" if @access_token
26
+ def headers
27
+ headers = {
28
+ 'Accept' => 'application/json',
29
+ 'Content-Type' => 'application/json'
30
+ }
31
+ headers['Authorization'] = credential if credential
22
32
  headers
23
33
  end
24
34
 
25
- def get(url)
35
+ def get(path, params)
36
+ url = endpoint + path
26
37
  httpclient = HTTPClient.new
27
- httpclient.get(url, default_headers)
38
+ httpclient.get(url, params, headers)
28
39
  end
29
40
 
30
- def patch(url, params)
41
+ def patch(path, params)
42
+ url = endpoint + path
31
43
  httpclient = HTTPClient.new
32
- httpclient.patch(url, params.to_json, default_headers)
44
+ httpclient.patch(url, params.to_json, headers)
33
45
  end
34
46
 
35
- def put(url)
47
+ def put(path, params)
48
+ url = endpoint + path
36
49
  httpclient = HTTPClient.new
37
- httpclient.put(url)
50
+ httpclient.put(url, params)
38
51
  end
39
52
 
40
- def post(url, params)
53
+ def post(path, params)
54
+ url = endpoint + path
41
55
  httpclient = HTTPClient.new
42
- httpclient.post(url, params.to_json, default_headers)
56
+ httpclient.post(url, params.to_json, headers)
43
57
  end
44
58
 
45
- def delete(url)
59
+ def delete(path)
60
+ url = endpoint + path
46
61
  httpclient = HTTPClient.new
47
62
  httpclient.delete(url)
48
63
  end
49
64
 
50
- # 全てのユーザの一覧
51
- def fetch_users
52
- path = '/api/v2/users'
53
- url = API_BASE_URL + path
54
-
55
- get(url)
56
- end
57
-
58
- # 記事につけられた「LGTM!」一覧
59
- def fetch_item_likes(item_id:)
60
- path = "/api/v2/items/#{item_id}/likes"
61
- url = API_BASE_URL + path
62
-
63
- get(url)
64
- end
65
-
66
- # コメントを削除
67
- def delete_comment(comment_id:)
68
- path = "/api/v2/comments/#{comment_id}"
69
- url = API_BASE_URL + path
70
-
71
- delete(url)
72
- end
73
-
74
- # コメントを取得
75
- def fetch_comment(comment_id:)
76
- path = "/api/v2/comments/#{comment_id}"
77
- url = API_BASE_URL + path
78
-
79
- get(url)
80
- end
81
-
82
- # コメントを更新
83
- def update_comment(comment_id:, body:)
84
- path = "/api/v2/comments/#{comment_id}"
85
- url = API_BASE_URL + path
86
-
87
- params = {
88
- body: body
89
- }
90
-
91
- patch(url, params)
92
- end
93
-
94
- # 投稿に付けられたコメント一覧
95
- def fetch_item_comments(item_id:)
96
- path = "/api/v2/items/#{item_id}/comments"
97
- url = API_BASE_URL + path
98
-
99
- get(url)
100
- end
101
-
102
- # 記事に対してコメントを投稿
103
- def post_comment(item_id:, body:)
104
- path = "/api/v2/items/#{item_id}/comments"
105
- url = API_BASE_URL + path
106
-
107
- params = {
108
- body: body
109
- }
110
-
111
- post(url, params)
112
- end
113
-
114
- # タグを取得
115
- def fetch_tag(tag_id:)
116
- path = "/api/v2/tags/#{tag_id}"
117
- url = API_BASE_URL + path
118
-
119
- get(url)
120
- end
121
-
122
- # ユーザがフォローしているタグ一覧
123
- def fetch_following_tags(user_id:)
124
- path = "/api/v2/users/#{user_id}/following_tags"
125
- url = API_BASE_URL + path
126
-
127
- get(url)
128
- end
129
-
130
- # タグのフォローを外す
131
- def delete_following(tag_id:)
132
- path = "/api/v2/tags/#{tag_id}/following"
133
- url = API_BASE_URL + path
134
-
135
- delete(url)
136
- end
137
-
138
- # タグをフォローしているかどうかを調る
139
- def check_tag_following(tag_id:)
140
- path = "/api/v2/tags/#{tag_id}/following"
141
- url = API_BASE_URL + path
142
-
143
- get(url)
144
- end
145
-
146
- # タグをフォロー
147
- def follow_tag(tag_id:)
148
- path = "/api/v2/tags/#{tag_id}/following"
149
- url = API_BASE_URL + path
150
-
151
- put(url)
152
- end
153
-
154
- # 記事をストックしているユーザ一覧を、ストックした日時の降順で返す
155
- def fetch_item_stockers(item_id:)
156
- path = "/api/v2/items/#{item_id}/stockers"
157
- url = API_BASE_URL + path
158
-
159
- get(url)
160
- end
161
-
162
- # 全てのユーザの一覧を作成日時の降順で取得
163
- def fetch_users
164
- path = "/api/v2/users"
165
- url = API_BASE_URL + path
166
-
167
- get(url)
168
- end
169
-
170
- # ユーザを取得
171
- def fetch_user(user_id:)
172
- path = "/api/v2/users/#{user_id}"
173
- url = API_BASE_URL + path
174
-
175
- get(url)
176
- end
177
-
178
- # ユーザがフォローしているユーザ一覧を取得
179
- def fetch_followees(user_id:)
180
- path = "/api/v2/users/#{user_id}/followees"
181
- url = API_BASE_URL + path
182
-
183
- get(url)
184
- end
185
-
186
- # ユーザをフォローしているユーザ一覧を取得
187
- def fetch_followers(user_id:)
188
- path = "/api/v2/users/#{user_id}/followers"
189
- url = API_BASE_URL + path
190
-
191
- get(url)
192
- end
193
-
194
- # ユーザへのフォローを外します。
195
- def delete_following(user_id:)
196
- path = "/api/v2/users/#{user_id}/following"
197
- url = API_BASE_URL + path
198
-
199
- delete(url)
200
- end
201
-
202
- # ユーザをフォローしている場合に204を返す
203
- def check_following(user_id:)
204
- path = "/api/v2/users/#{user_id}/following"
205
- url = API_BASE_URL + path
206
-
207
- get(url)
208
- end
209
-
210
- # ユーザをフォロー
211
- def follow_user(user_id:)
212
- path = "/api/v2/users/#{user_id}/following"
213
- url = API_BASE_URL + path
214
-
215
- put(url)
216
- end
217
-
218
- # 認証中のユーザの記事の一覧を作成日時の降順で返す
219
- def fetch_my_items
220
- path = "/api/v2/authenticated_user/items"
221
- url = API_BASE_URL + path
222
-
223
- get(url)
224
- end
225
-
226
- # 記事の一覧を作成日時の降順で返す
227
- def fetch_items
228
- path = '/api/v2/items'
229
- url = API_BASE_URL + path
230
-
231
- get(url)
232
- end
233
-
234
- # 新たに記事を作成
235
- def post_item(params:)
236
- path = "/api/v2/items"
237
- url = API_BASE_URL + path
238
-
239
- post(url, params)
240
- end
241
-
242
- # 記事を削除
243
- def delete_item(item_id:)
244
- path = "/api/v2/items/#{item_id}"
245
- url = API_BASE_URL + path
246
-
247
- delete(url)
248
- end
249
-
250
- # 記事を取得
251
- def fetch_item(item_id:)
252
- path = "/api/v2/items/#{item_id}"
253
- url = API_BASE_URL + path
254
-
255
- get(url)
256
- end
257
-
258
- # 記事を更新
259
- def update_item(item_id:, params:)
260
- path = "/api/v2/items/#{item_id}"
261
- url = API_BASE_URL + path
262
-
263
- patch(url, params)
264
- end
265
-
266
- # 記事をストック
267
- def stock_item(item_id:)
268
- path = "/api/v2/items/#{item_id}"
269
- url = API_BASE_URL + path
270
-
271
- put(url)
272
- end
273
-
274
- # 記事をストックから取り除く
275
- def delete_stock(item_id:)
276
- path = "/api/v2/items/#{item_id}"
277
- url = API_BASE_URL + path
278
-
279
- delete(url)
280
- end
281
-
282
- # 記事をストックしているかどうか調べる
283
- def check_item_stock(item_id:)
284
- path = "/api/v2/items/#{item_id}/stock"
285
- url = API_BASE_URL + path
286
-
287
- get(url)
288
- end
289
-
290
- # タグの記事一覧
291
- def fetch_tag_items(tag_id:)
292
- path = "/api/v2/tags/#{tag_id}/items"
293
- url = API_BASE_URL + path
294
-
295
- get(url)
296
- end
297
-
298
- # 指定されたユーザの記事一覧
299
- def fetch_user_items(user_id:)
300
- path = "/api/v2/tags/#{user_id}/items"
301
- url = API_BASE_URL + path
302
-
303
- get(url)
304
- end
305
-
306
- # 指定されたユーザがストックした記事一覧
307
- def fetch_user_stocks(user_id:)
308
- path = "/api/v2/users/#{user_id}/stocks"
309
- url = API_BASE_URL + path
310
-
311
- get(url)
312
- end
313
-
314
- # コメントに絵文字リアクションを付ける
315
- def attach_reaction_to_comment(comment_id:, params)
316
- path = "/api/v2/comments/#{comment_id}/reactions"
317
- url = API_BASE_URL + path
318
-
319
- post(url, params)
320
- end
321
-
322
- # 記事に絵文字リアクションを付ける
323
- def attach_reaction_to_item(item_id: params)
324
- path = "/api/v2/items/#{item_id}/reactions"
325
- url = API_BASE_URL + path
326
-
327
- post(url, params)
328
- end
329
-
330
- # コメントから絵文字リアクションを削除
331
- def delete_comment_reaction(comment_id:, reaction_name:)
332
- path = "/api/v2/comments/#{comment_id}/reactions/#{reaction_name}"
333
- url = API_BASE_URL + path
334
-
335
- delete(url)
336
- end
337
-
338
- # 記事から絵文字リアクションを削除
339
- def delete_item_reaction(item_id:, reaction_name:)
340
- path = "/api/v2/items/#{item_id}/reactions/#{reaction_name}"
341
- url = API_BASE_URL + path
342
-
343
- delete(url)
344
- end
345
-
346
- # コメントに付けられた絵文字リアクション一覧
347
- def fetch_comment_reactions(comment_id:)
348
- path = "/api/v2/comments/#{comment_id}/reactions"
349
- url = API_BASE_URL + path
350
-
351
- get(url)
352
- end
353
-
354
- # 記事に付けられた絵文字リアクション一覧
355
- def fetch_item_reactions(item_id:)
356
- path = "/api/v2/items/#{item_id}/reactions"
357
- url = API_BASE_URL + path
358
-
359
- get(url)
360
- end
361
-
362
- # アクセストークンに紐付いたユーザを返す
363
- def fetch_authenticated_user
364
- path = "/api/v2/authenticated_user"
365
- url = API_BASE_URL + path
366
-
367
- get(url)
65
+ def endpoint
66
+ @endpoint ||= API_BASE_URL
368
67
  end
369
68
  end
370
69
  end