qiita 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/qiita/client.rb CHANGED
@@ -1,88 +1,152 @@
1
- require 'faraday'
2
- require 'faraday_middleware'
3
- require 'faraday/response/raise_qiita_error.rb'
4
- require 'json'
5
- require 'qiita'
6
- require 'qiita/error'
7
- require 'qiita/client/items'
8
- require 'qiita/client/tags'
9
- require 'qiita/client/users'
1
+ require "qiita/resource_based_methods"
2
+ require "qiita/version"
10
3
 
4
+ ## Qiita::Client
5
+ # A class for API client to send HTTP request.
6
+ #
11
7
  module Qiita
12
8
  class Client
13
- ROOT_URL = 'https://qiita.com/'
14
- OPTIONS_KEYS = [:url_name, :password, :token].freeze
9
+ DEFAULT_ACCEPT = "application/json"
15
10
 
16
- attr_accessor *OPTIONS_KEYS
11
+ DEFAULT_HOST = "qiita.com"
17
12
 
18
- def initialize(args)
19
- OPTIONS_KEYS.each do |key|
20
- send("#{key}=", args[key])
21
- end
22
- if token.nil? && url_name && password
23
- login
24
- end
13
+ DEFAULT_USER_AGENT = "Qiita Ruby Gem #{Qiita::VERSION}"
14
+
15
+ DEFAULT_HEADERS = {
16
+ "Accept" => DEFAULT_ACCEPT,
17
+ "User-Agent" => DEFAULT_USER_AGENT,
18
+ }
19
+
20
+ include ResourceBasedMethods
21
+
22
+ # ### Qiita::Client.new(options = {})
23
+ # Creates a new instance of `Qiita::Client` class.
24
+ # `options` can have following key-values:
25
+ #
26
+ # * `access_token` - (String) Access token issued to authenticate and authorize user.
27
+ # * `host` - (String) Hostname where this client accesses to.
28
+ # * `team` - (String) Team name to be used as subdomain.
29
+ #
30
+ # ```rb
31
+ # Qiita::Client.new
32
+ # Qiita::Client.new(access_token: "...")
33
+ # Qiita::Client.new(host: "my-team-name.qiita.com")
34
+ # Qiita::Client.new(team: "my-team-name")
35
+ # ```
36
+ #
37
+ def initialize(access_token: nil, host: nil, team: nil)
38
+ @access_token = access_token
39
+ @host = host
40
+ @team = team
25
41
  end
26
42
 
27
- def rate_limit params={}
28
- get '/rate_limit', params
43
+ # ### Qiita::Client#get(path, params = nil, headers = nil)
44
+ # Sends GET request with given parameters, then returns a `Qiita::Response`.
45
+ # `params` are url-encoded and used as URI query string.
46
+ #
47
+ # ```rb
48
+ # client.get("/api/v2/items", page: 2)
49
+ # ```
50
+ #
51
+ def get(path, params = nil, headers = nil)
52
+ process(:get, path, params, headers)
29
53
  end
30
54
 
31
- include Qiita::Client::Items
32
- include Qiita::Client::Tags
33
- include Qiita::Client::Users
55
+ # ### Qiita::Client#post(path, params = nil, headers = nil)
56
+ # Sends POST request with given parameters, then returns a Qiita::Response.
57
+ # `params` are JSON-encoded and used as request body.
58
+ #
59
+ # ```rb
60
+ # client.post("/api/v2/items", title: "...", body: "...")
61
+ # ```
62
+ #
63
+ def post(path, params = nil, headers = nil)
64
+ process(:post, path, params, headers)
65
+ end
34
66
 
35
- private
67
+ # ### Qiita::Client#patch(path, params = nil, headers = nil)
68
+ # Sends PATCH request with given parameters, then returns a Qiita::Response.
69
+ # `params` are JSON-encoded and used as request body.
70
+ #
71
+ # ```rb
72
+ # client.patch("/api/v2/items/543efd13001e30837319", title: "...", body: "...")
73
+ # ```
74
+ #
75
+ def patch(path, params = nil, headers = nil)
76
+ process(:patch, path, params, headers)
77
+ end
78
+
79
+ # ### Qiita::Client#put(path, params = nil, headers = nil)
80
+ # Sends PUT request, then returns a Qiita::Response.
81
+ # `params` are JSON-encoded and used as request body.
82
+ #
83
+ # ```rb
84
+ # client.put("/api/v2/items/543efd13001e30837319/stock")
85
+ # ```
86
+ #
87
+ def put(path, params = nil, headers = nil)
88
+ process(:put, path, params, headers)
89
+ end
36
90
 
37
- def login
38
- json = post '/auth', { :url_name => @url_name, :password => @password }
39
- @token = json['token']
91
+ # ### Qiita::Client#delete(path, params = nil, headers = nil)
92
+ # Sends DELETE request, then returns a Qiita::Response.
93
+ # `params` are url-encoded and used as URI query string.
94
+ #
95
+ # ```rb
96
+ # client.delete("/api/v2/items/543efd13001e30837319/stock")
97
+ # ```
98
+ #
99
+ def delete(path, params = nil, headers = nil)
100
+ process(:delete, path, params, headers)
40
101
  end
41
102
 
103
+ # ### Qiita::Client#connection
104
+ # Returns a Faraday::Connection to customize by your favorite middlewares.
105
+ #
106
+ # ```rb
107
+ # client.connection.response :logger
108
+ # ```
109
+ #
42
110
  def connection
43
- options = {
44
- :url => ROOT_URL,
45
- :ssl => { :verify => false }
46
- }
47
- @connection ||= Faraday.new(options) do |faraday|
48
- faraday.request :json
49
- faraday.adapter Faraday.default_adapter
50
- faraday.use Faraday::Response::RaiseQiitaError
51
- faraday.use FaradayMiddleware::Mashify
52
- faraday.use FaradayMiddleware::ParseJson
111
+ @connection ||= Faraday.new(headers: default_headers, url: url_prefix) do |connection|
112
+ connection.request :json
113
+ connection.response :json
114
+ connection.adapter Faraday.default_adapter
53
115
  end
54
116
  end
55
117
 
56
- def get(path, params={})
57
- request(:get, path, params)
58
- end
118
+ private
59
119
 
60
- def delete(path, params={})
61
- request(:delete, path, params)
120
+ def default_headers
121
+ headers = DEFAULT_HEADERS.clone
122
+ headers["Authorization"] = "Bearer #{@access_token}" if @access_token
123
+ headers
62
124
  end
63
125
 
64
- def post(path, params={})
65
- request(:post, path, params)
126
+ def host
127
+ case
128
+ when @host
129
+ @host
130
+ when @team
131
+ "#{@team}.#{DEFAULT_HOST}"
132
+ else
133
+ DEFAULT_HOST
134
+ end
66
135
  end
67
136
 
68
- def put(path, params={})
69
- request(:put, path, params)
137
+ def process(request_method, path, params, headers)
138
+ Qiita::Response.new(
139
+ connection.send(
140
+ request_method,
141
+ path,
142
+ params,
143
+ headers,
144
+ )
145
+ )
70
146
  end
71
147
 
72
- def request(method, path, params)
73
- path = "/api/v1/#{path}"
74
- params.merge!(:token => token) if token
75
- response = connection.send(method) do |req|
76
- req.headers['Content-Type'] = 'application/json'
77
- case method
78
- when :get, :delete
79
- req.url path, params
80
- when :post, :put
81
- req.path = path
82
- req.body = params.to_json unless params.empty?
83
- end
84
- end
85
- response.body
148
+ def url_prefix
149
+ "https://#{host}"
86
150
  end
87
151
  end
88
152
  end
@@ -0,0 +1,35 @@
1
+ # ## Qiita::CommandBuilder
2
+ # Creates a new command object from given ARGV.
3
+ #
4
+ # ```ruby
5
+ # builder = Qiita::CommandBuilder.new(ARGV)
6
+ # builder.call
7
+ # ```
8
+ #
9
+ module Qiita
10
+ class CommandBuilder
11
+ ### Qiita::CommandBuilder.new(argv)
12
+ # Creates a new instance of Qiita::CommandBuilder from given command line arguments.
13
+ #
14
+ def initialize(argv)
15
+ @argv = argv
16
+ end
17
+
18
+ ### Qiita::CommandBuilder#call
19
+ # Returns a new instance of command class that inherited from Qiita::Commands::Base.
20
+ #
21
+ def call
22
+ if arguments.valid?
23
+ Qiita::Commands::Request.new(arguments)
24
+ else
25
+ Qiita::Commands::Error.new(arguments)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def arguments
32
+ @arguments ||= Arguments.new(@argv)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ module Qiita
2
+ module Commands
3
+ class Base
4
+ def initialize(arguments)
5
+ @arguments = arguments
6
+ end
7
+
8
+ def call
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Qiita
2
+ module Commands
3
+ class Error < Base
4
+ def call
5
+ abort(@arguments.error_message)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ module Qiita
2
+ module Commands
3
+ class Request < Base
4
+ def call
5
+ response = client.send(
6
+ @arguments.method_name,
7
+ *@arguments.arguments,
8
+ @arguments.params,
9
+ @arguments.headers,
10
+ )
11
+ print ResponseRenderer.new(
12
+ response,
13
+ color: @arguments.color,
14
+ show_body: @arguments.show_body,
15
+ show_header: @arguments.show_header,
16
+ )
17
+ end
18
+
19
+ private
20
+
21
+ def client
22
+ Client.new(
23
+ access_token: @arguments.access_token,
24
+ host: @arguments.host,
25
+ team: @arguments.team,
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,283 @@
1
+ module Qiita
2
+ module ResourceBasedMethods
3
+ # ### Qiita::Client#create_access_token(params = nil, headers = nil)
4
+ # 与えられた認証情報をもとに新しいアクセストークンを発行します。
5
+ #
6
+ def create_access_token(params = nil, headers = nil)
7
+ post("/api/v2/access_tokens", params, headers)
8
+ end
9
+
10
+ # ### Qiita::Client#delete_access_token(token, params = nil, headers = nil)
11
+ # 与えられたアクセストークンを失効させ、それ以降利用されないようにします。
12
+ #
13
+ def delete_access_token(token, params = nil, headers = nil)
14
+ delete("/api/v2/access_tokens/#{token}", params, headers)
15
+ end
16
+
17
+ # ### Qiita::Client#get_comment(id, params = nil, headers = nil)
18
+ # 特定のコメントを返します。
19
+ #
20
+ def get_comment(id, params = nil, headers = nil)
21
+ get("/api/v2/comments/#{id}", params, headers)
22
+ end
23
+
24
+ # ### Qiita::Client#delete_comment(id, params = nil, headers = nil)
25
+ # 特定のコメントを削除します。
26
+ #
27
+ def delete_comment(id, params = nil, headers = nil)
28
+ delete("/api/v2/comments/#{id}", params, headers)
29
+ end
30
+
31
+ # ### Qiita::Client#update_comment(id, params = nil, headers = nil)
32
+ # 特定のコメントを更新します。
33
+ #
34
+ def update_comment(id, params = nil, headers = nil)
35
+ patch("/api/v2/comments/#{id}", params, headers)
36
+ end
37
+
38
+ # ### Qiita::Client#list_item_comments(item_id, params = nil, headers = nil)
39
+ # 特定の投稿に寄せられたコメント一覧を返します。
40
+ #
41
+ def list_item_comments(item_id, params = nil, headers = nil)
42
+ get("/api/v2/items/#{item_id}/comments", params, headers)
43
+ end
44
+
45
+ # ### Qiita::Client#create_item_comment(item_id, params = nil, headers = nil)
46
+ # 特定の投稿にコメントを付けます。
47
+ #
48
+ def create_item_comment(item_id, params = nil, headers = nil)
49
+ post("/api/v2/items/#{item_id}/comments", params, headers)
50
+ end
51
+
52
+ # ### Qiita::Client#thank_comment(comment_id, params = nil, headers = nil)
53
+ # 特定のコメントにThankを付けます。
54
+ #
55
+ def thank_comment(comment_id, params = nil, headers = nil)
56
+ put("/api/v2/comments/#{comment_id}/thank", params, headers)
57
+ end
58
+
59
+ # ### Qiita::Client#unthank_comment(comment_id, params = nil, headers = nil)
60
+ # 特定のコメントからThankを外します。
61
+ #
62
+ def unthank_comment(comment_id, params = nil, headers = nil)
63
+ delete("/api/v2/comments/#{comment_id}/thank", params, headers)
64
+ end
65
+
66
+ # ### Qiita::Client#list_items(params = nil, headers = nil)
67
+ # 新着順に全ての投稿一覧を返します。
68
+ #
69
+ def list_items(params = nil, headers = nil)
70
+ get("/api/v2/items", params, headers)
71
+ end
72
+
73
+ # ### Qiita::Client#create_item(params = nil, headers = nil)
74
+ # 新たに投稿を作成します。
75
+ #
76
+ def create_item(params = nil, headers = nil)
77
+ post("/api/v2/items", params, headers)
78
+ end
79
+
80
+ # ### Qiita::Client#get_item(id, params = nil, headers = nil)
81
+ # 特定の投稿を返します。
82
+ #
83
+ def get_item(id, params = nil, headers = nil)
84
+ get("/api/v2/items/#{id}", params, headers)
85
+ end
86
+
87
+ # ### Qiita::Client#update_item(id, params = nil, headers = nil)
88
+ # 特定の投稿を編集します。
89
+ #
90
+ def update_item(id, params = nil, headers = nil)
91
+ patch("/api/v2/items/#{id}", params, headers)
92
+ end
93
+
94
+ # ### Qiita::Client#delete_item(id, params = nil, headers = nil)
95
+ # 特定の投稿を削除します。
96
+ #
97
+ def delete_item(id, params = nil, headers = nil)
98
+ delete("/api/v2/items/#{id}", params, headers)
99
+ end
100
+
101
+ # ### Qiita::Client#list_tag_items(id, params = nil, headers = nil)
102
+ # 特定のタグが付けられた投稿一覧を返します。
103
+ #
104
+ def list_tag_items(id, params = nil, headers = nil)
105
+ get("/api/v2/tags/#{id}/items", params, headers)
106
+ end
107
+
108
+ # ### Qiita::Client#list_user_items(user_id, params = nil, headers = nil)
109
+ # 特定のユーザの投稿一覧を返します。
110
+ #
111
+ def list_user_items(user_id, params = nil, headers = nil)
112
+ get("/api/v2/users/#{user_id}/items", params, headers)
113
+ end
114
+
115
+ # ### Qiita::Client#list_user_stocks(user_id, params = nil, headers = nil)
116
+ # 特定のユーザがストックした投稿一覧を返します。
117
+ #
118
+ def list_user_stocks(user_id, params = nil, headers = nil)
119
+ get("/api/v2/users/#{user_id}/stocks", params, headers)
120
+ end
121
+
122
+ # ### Qiita::Client#stock_item(item_id, params = nil, headers = nil)
123
+ # 特定の投稿をストックします。
124
+ #
125
+ def stock_item(item_id, params = nil, headers = nil)
126
+ put("/api/v2/items/#{item_id}/stock", params, headers)
127
+ end
128
+
129
+ # ### Qiita::Client#unstock_item(item_id, params = nil, headers = nil)
130
+ # 特定の投稿をストックから取り除きます。
131
+ #
132
+ def unstock_item(item_id, params = nil, headers = nil)
133
+ delete("/api/v2/items/#{item_id}/stock", params, headers)
134
+ end
135
+
136
+ # ### Qiita::Client#lgtm_item(item_id, params = nil, headers = nil)
137
+ # 特定の投稿に「いいね!」を付けます。
138
+ #
139
+ def lgtm_item(item_id, params = nil, headers = nil)
140
+ put("/api/v2/items/#{item_id}/lgtm", params, headers)
141
+ end
142
+
143
+ # ### Qiita::Client#unlgtm_item(item_id, params = nil, headers = nil)
144
+ # 特定の投稿への「いいね!」を取り消します。
145
+ #
146
+ def unlgtm_item(item_id, params = nil, headers = nil)
147
+ delete("/api/v2/items/#{item_id}/lgtm", params, headers)
148
+ end
149
+
150
+ # ### Qiita::Client#list_projects(params = nil, headers = nil)
151
+ # チーム内に存在するプロジェクト一覧を返します。
152
+ #
153
+ def list_projects(params = nil, headers = nil)
154
+ get("/api/v2/projects", params, headers)
155
+ end
156
+
157
+ # ### Qiita::Client#get_project(id, params = nil, headers = nil)
158
+ # 特定のプロジェクトを返します。
159
+ #
160
+ def get_project(id, params = nil, headers = nil)
161
+ get("/api/v2/projects/#{id}", params, headers)
162
+ end
163
+
164
+ # ### Qiita::Client#create_project(params = nil, headers = nil)
165
+ # プロジェクトを新たに作成します。
166
+ #
167
+ def create_project(params = nil, headers = nil)
168
+ post("/api/v2/projects", params, headers)
169
+ end
170
+
171
+ # ### Qiita::Client#delete_project(id, params = nil, headers = nil)
172
+ # 特定のプロジェクトを削除します。
173
+ #
174
+ def delete_project(id, params = nil, headers = nil)
175
+ delete("/api/v2/projects/#{id}", params, headers)
176
+ end
177
+
178
+ # ### Qiita::Client#update_project(id, params = nil, headers = nil)
179
+ # 特定のプロジェクトを編集します。
180
+ #
181
+ def update_project(id, params = nil, headers = nil)
182
+ patch("/api/v2/projects/#{id}", params, headers)
183
+ end
184
+
185
+ # ### Qiita::Client#create_expanded_template(params = nil, headers = nil)
186
+ # 受け取ったテンプレート用文字列の変数を展開して返します。
187
+ #
188
+ def create_expanded_template(params = nil, headers = nil)
189
+ post("/api/v2/expanded_templates", params, headers)
190
+ end
191
+
192
+ # ### Qiita::Client#list_tags(params = nil, headers = nil)
193
+ # 全てのタグ一覧を返します。
194
+ #
195
+ def list_tags(params = nil, headers = nil)
196
+ get("/api/v2/tags", params, headers)
197
+ end
198
+
199
+ # ### Qiita::Client#get_tag(id, params = nil, headers = nil)
200
+ # 特定のタグを返します。
201
+ #
202
+ def get_tag(id, params = nil, headers = nil)
203
+ get("/api/v2/tags/#{id}", params, headers)
204
+ end
205
+
206
+ # ### Qiita::Client#list_user_following_tags(user_id, params = nil, headers = nil)
207
+ # 特定のユーザがフォローしているタグ一覧を返します。
208
+ #
209
+ def list_user_following_tags(user_id, params = nil, headers = nil)
210
+ get("/api/v2/users/#{user_id}/following_tags", params, headers)
211
+ end
212
+
213
+ # ### Qiita::Client#list_templates(params = nil, headers = nil)
214
+ # 全てのテンプレート一覧を返します。
215
+ #
216
+ def list_templates(params = nil, headers = nil)
217
+ get("/api/v2/templates", params, headers)
218
+ end
219
+
220
+ # ### Qiita::Client#get_template(id, params = nil, headers = nil)
221
+ # 特定のテンプレートを返します。
222
+ #
223
+ def get_template(id, params = nil, headers = nil)
224
+ get("/api/v2/templates/#{id}", params, headers)
225
+ end
226
+
227
+ # ### Qiita::Client#delete_template(id, params = nil, headers = nil)
228
+ # 特定のテンプレートを削除します。
229
+ #
230
+ def delete_template(id, params = nil, headers = nil)
231
+ delete("/api/v2/templates/#{id}", params, headers)
232
+ end
233
+
234
+ # ### Qiita::Client#create_template(params = nil, headers = nil)
235
+ # 新しくテンプレートを作成します。
236
+ #
237
+ def create_template(params = nil, headers = nil)
238
+ post("/api/v2/templates", params, headers)
239
+ end
240
+
241
+ # ### Qiita::Client#update_template(id, params = nil, headers = nil)
242
+ # 特定のテンプレートを編集します。
243
+ #
244
+ def update_template(id, params = nil, headers = nil)
245
+ patch("/api/v2/templates/#{id}", params, headers)
246
+ end
247
+
248
+ # ### Qiita::Client#list_users(params = nil, headers = nil)
249
+ # 全てのユーザの一覧を返します。
250
+ #
251
+ def list_users(params = nil, headers = nil)
252
+ get("/api/v2/users", params, headers)
253
+ end
254
+
255
+ # ### Qiita::Client#get_user(id, params = nil, headers = nil)
256
+ # 特定のユーザを返します。
257
+ #
258
+ def get_user(id, params = nil, headers = nil)
259
+ get("/api/v2/users/#{id}", params, headers)
260
+ end
261
+
262
+ # ### Qiita::Client#get_authenticated_user(params = nil, headers = nil)
263
+ # アクセストークンに紐付いたユーザを返します。
264
+ #
265
+ def get_authenticated_user(params = nil, headers = nil)
266
+ get("/api/v2/authenticated_user", params, headers)
267
+ end
268
+
269
+ # ### Qiita::Client#list_user_followees(user_id, params = nil, headers = nil)
270
+ # 特定のユーザがフォローしているユーザ一覧を返します。
271
+ #
272
+ def list_user_followees(user_id, params = nil, headers = nil)
273
+ get("/api/v2/users/#{user_id}/followees", params, headers)
274
+ end
275
+
276
+ # ### Qiita::Client#list_user_followers(user_id, params = nil, headers = nil)
277
+ # 特定のユーザをフォローしているユーザ一覧を返します。
278
+ #
279
+ def list_user_followers(user_id, params = nil, headers = nil)
280
+ get("/api/v2/users/#{user_id}/followers", params, headers)
281
+ end
282
+ end
283
+ end