qiita 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d7aad5b6c927032cc2fd56a07a2fb1a6d288451e
4
+ data.tar.gz: cea7adcd772f8c7ee2dce7fd288a3406d775434b
5
+ SHA512:
6
+ metadata.gz: 9aa059a756fb31e4b11fd02d93f568ee760a912be25c9662ee280dcce42aba26f1fb1501683d9860b82209b892fb8a043c556f99f91d5a4f915c38d3ff66bf83
7
+ data.tar.gz: eade564c0a5a16a20420438dc0a4d5e4641c873a434b7f07537b510a10c0e897320a1086aa5c541e6810847799df9c2467c47e01c41d1ed929ea96643ec969c1
data/.gitignore CHANGED
@@ -7,7 +7,6 @@ Gemfile.lock
7
7
  InstalledFiles
8
8
  _yardoc
9
9
  coverage
10
- doc/
11
10
  lib/bundler/man
12
11
  pkg
13
12
  rdoc
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 1.0.0
2
+ * Support Qiita API v2.
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Hiroshige Umino
1
+ Copyright (c) 2014 Ryo Nakamura
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Makefile ADDED
@@ -0,0 +1,18 @@
1
+ all: test doc
2
+
3
+ doc:
4
+ git grep -h "^\s\+# \?" lib/qiita/response.rb | sed -E 's; +# ?;;' > doc/response.md
5
+ git grep -h "^\s\+# \?" lib/qiita/client.rb | sed -E 's; +# ?;;' > doc/client.md
6
+ git grep -h "^\s\+# \?" lib/qiita/resource_based_methods.rb | sed -E 's; +# ?;;' >> doc/client.md
7
+
8
+ release:
9
+ bundle exec rake release
10
+
11
+ setup:
12
+ gem install bundler
13
+ bundle install
14
+
15
+ test:
16
+ bundle exec rspec
17
+
18
+ .PHONY: doc release setup test
data/README.md CHANGED
@@ -1,70 +1,79 @@
1
1
  # Qiita
2
+ Qiita API v2 client library and CLI tool, written in Ruby.
2
3
 
3
- Ruby wrapper for Qiita API v1.
4
+ ### Install
5
+ Note: requires Ruby 2.0.0 or higher.
4
6
 
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'qiita'
10
-
11
- And then execute:
7
+ ```sh
8
+ gem install qiita
9
+ ```
12
10
 
13
- $ bundle
11
+ ## Library
12
+ See [/doc](/doc) for more details.
14
13
 
15
- Or install it yourself as:
14
+ ```rb
15
+ require "qiita"
16
16
 
17
- $ gem install qiita
17
+ client = Qiita::Client.new(access_token: "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd")
18
+ client.list_items
19
+ client.list_users
20
+ client.get_user_items("r7kamura")
21
+ client.get_user("r7kamura")
22
+ client.get_user("r7kamura").status
23
+ client.get_user("r7kamura").headers
24
+ client.get_user("r7kamura").body
25
+ ```
18
26
 
19
- ## Usage
27
+ ## CLI
28
+ `qiita` executable calls `Qiita::Client`'s methods.
20
29
 
21
- ### Get user's items
22
- ```ruby
23
- Qiita.user_items 'yaotti'
24
30
  ```
25
-
26
- ### Get tag's items
27
- ```ruby
28
- Qiita.tag_items 'ruby'
31
+ $ qiita <method> <arguments> [headers|params] [options]
32
+ | | | | |
33
+ | | | | `-- -H, --host
34
+ | | | | -a, --access-token
35
+ | | | | -c, --color
36
+ | | | | -h, --help
37
+ | | | | --header
38
+ | | | | --no-body
39
+ | | | | -t, --team
40
+ | | | |
41
+ | | | `------------ key=value or key:=value
42
+ | | |
43
+ | | `------------------- Key:value
44
+ | |
45
+ | `------------------------------ required arguments for the method
46
+ |
47
+ `----------------------------------------- method name
48
+
49
+ $ qiita list_users
50
+ $ qiita get_user r7kamura
51
+ $ qiita list_user_items r7kamura
29
52
  ```
30
53
 
31
- ### Get a specified item with comments and raw markdown content
32
- ```ruby
33
- item_uuid = '1234567890abcdefg'
34
- Qiita.item item_uuid
35
- ```
54
+ ### Method and Arguments
55
+ Pass [Qiita::Client's method name](doc/client.md) and required arguments.
36
56
 
57
+ ### Access token
58
+ Accepts access token via `-a, --access-token` or `QIITA_ACCESS_TOKEN` environment variable.
37
59
 
38
- ## Authenticated requests
60
+ ### Headers
61
+ To set custom request headers, use `Key:value` syntax.
39
62
 
40
- ### Login with "username & password" or "token"
41
- ```
42
- qiita = Qiita.new url_name: 'yaotti', password: 'mysecret' # => contains token
43
- # or
44
- qiita = Qiita.new token: 'myauthtoken'
45
63
  ```
46
-
47
- ### Get my items
48
- ```ruby
49
- qiita.user_items
64
+ $ qiita list_items "Authorization:Bearer 1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd"
50
65
  ```
51
66
 
52
- ### Post/Update/Delete an item
53
- ```ruby
54
- # post
55
- item = qiita.post_item title: 'Hello', body: 'markdown text', tags: [{ name: 'ruby', versions: %w[1.9.2 1.9.3] }], private: false
67
+ ### Params
68
+ Params are used for query string in GET method, or for request body in other methods.
69
+ You can set params by `key=value` or `key:=value` syntax.
70
+ `key=value` is parsed into String value,
71
+ while `key:=value` is parsed into JSON value (e.g. key:=17 will be `{"key":17}`).
72
+ `qiita` also accepts params via STDIN.
56
73
 
57
- # update
58
- qiita.update_item item.uuid, title: 'modified'
59
-
60
- # delete
61
- qiita.delete_item item.uuid
74
+ ```
75
+ $ qiita list_items page=2 per_page=10
76
+ $ qiita create_item < params.json
62
77
  ```
63
78
 
64
- ## Contributing
65
-
66
- 1. Fork it
67
- 2. Create your feature branch (`git checkout -b my-new-feature`)
68
- 3. Commit your changes (`git commit -am 'Add some feature'`)
69
- 4. Push to the branch (`git push origin my-new-feature`)
70
- 5. Create new Pull Request
79
+ ![](images/cli.png)
data/bin/qiita ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
3
+ require "qiita"
4
+
5
+ builder = Qiita::CommandBuilder.new(ARGV)
6
+ command = builder.call
7
+ command.call
data/doc/client.md ADDED
@@ -0,0 +1,171 @@
1
+ ### Qiita::Client.new(options = {})
2
+ Creates a new instance of `Qiita::Client` class.
3
+ `options` can have following key-values:
4
+
5
+ * `access_token` - (String) Access token issued to authenticate and authorize user.
6
+ * `host` - (String) Hostname where this client accesses to.
7
+
8
+ ```rb
9
+ Qiita::Client.new
10
+ Qiita::Client.new(access_token: "...")
11
+ Qiita::Client.new(host: "my-team-name.qiita.com")
12
+ ```
13
+
14
+ ### Qiita::Client#get(path, params = nil, headers = nil)
15
+ Sends GET request with given parameters, then returns a `Qiita::Response`.
16
+ `params` are url-encoded and used as URI query string.
17
+
18
+ ```rb
19
+ client.get("/api/v2/items", page: 2)
20
+ ```
21
+
22
+ ### Qiita::Client#post(path, params = nil, headers = nil)
23
+ Sends POST request with given parameters, then returns a Qiita::Response.
24
+ `params` are JSON-encoded and used as request body.
25
+
26
+ ```rb
27
+ client.post("/api/v2/items", title: "...", body: "...")
28
+ ```
29
+
30
+ ### Qiita::Client#patch(path, params = nil, headers = nil)
31
+ Sends PATCH request with given parameters, then returns a Qiita::Response.
32
+ `params` are JSON-encoded and used as request body.
33
+
34
+ ```rb
35
+ client.patch("/api/v2/items/543efd13001e30837319", title: "...", body: "...")
36
+ ```
37
+
38
+ ### Qiita::Client#put(path, params = nil, headers = nil)
39
+ Sends PUT request, then returns a Qiita::Response.
40
+ `params` are JSON-encoded and used as request body.
41
+
42
+ ```rb
43
+ client.put("/api/v2/items/543efd13001e30837319/stock")
44
+ ```
45
+
46
+ ### Qiita::Client#delete(path, params = nil, headers = nil)
47
+ Sends DELETE request, then returns a Qiita::Response.
48
+ `params` are url-encoded and used as URI query string.
49
+
50
+ ```rb
51
+ client.delete("/api/v2/items/543efd13001e30837319/stock")
52
+ ```
53
+
54
+ ### Qiita::Client#connection
55
+ Returns a Faraday::Connection to customize by your favorite middlewares.
56
+
57
+ ```rb
58
+ client.connection.response :logger
59
+ ```
60
+
61
+ ### Qiita::Client#create_access_token(params = nil, headers = nil)
62
+ 与えられた認証情報をもとに新しいアクセストークンを発行します。
63
+
64
+ ### Qiita::Client#delete_access_token(token, params = nil, headers = nil)
65
+ 与えられたアクセストークンを失効させ、それ以降利用されないようにします。
66
+
67
+ ### Qiita::Client#delete_comment(id, params = nil, headers = nil)
68
+ 特定のコメントを削除します。
69
+
70
+ ### Qiita::Client#update_comment(id, params = nil, headers = nil)
71
+ 特定のコメントを更新します。
72
+
73
+ ### Qiita::Client#list_item_comments(item_id, params = nil, headers = nil)
74
+ 特定の投稿に寄せられたコメント一覧を返します。
75
+
76
+ ### Qiita::Client#create_item_comment(item_id, params = nil, headers = nil)
77
+ 特定の投稿にコメントを付けます。
78
+
79
+ ### Qiita::Client#thank_comment(comment_id, params = nil, headers = nil)
80
+ 特定のコメントにThankを付けます。
81
+
82
+ ### Qiita::Client#unthank_comment(comment_id, params = nil, headers = nil)
83
+ 特定のコメントからThankを外します。
84
+
85
+ ### Qiita::Client#list_items(params = nil, headers = nil)
86
+ 新着順に全ての投稿一覧を返します。
87
+
88
+ ### Qiita::Client#create_item(params = nil, headers = nil)
89
+ 新たに投稿を作成します。
90
+
91
+ ### Qiita::Client#get_item(id, params = nil, headers = nil)
92
+ 特定の投稿を返します。
93
+
94
+ ### Qiita::Client#update_item(id, params = nil, headers = nil)
95
+ 特定の投稿を編集します。
96
+
97
+ ### Qiita::Client#delete_item(id, params = nil, headers = nil)
98
+ 特定の投稿を削除します。
99
+
100
+ ### Qiita::Client#list_tag_items(id, params = nil, headers = nil)
101
+ 特定のタグが付けられた投稿一覧を返します。
102
+
103
+ ### Qiita::Client#list_user_items(user_id, params = nil, headers = nil)
104
+ 特定のユーザの投稿一覧を返します。
105
+
106
+ ### Qiita::Client#list_user_stocks(user_id, params = nil, headers = nil)
107
+ 特定のユーザがストックした投稿一覧を返します。
108
+
109
+ ### Qiita::Client#stock_item(item_id, params = nil, headers = nil)
110
+ 特定の投稿をストックします。
111
+
112
+ ### Qiita::Client#unstock_item(item_id, params = nil, headers = nil)
113
+ 特定の投稿をストックから取り除きます。
114
+
115
+ ### Qiita::Client#lgtm_item(item_id, params = nil, headers = nil)
116
+ 特定の投稿に「いいね!」を付けます。
117
+
118
+ ### Qiita::Client#unlgtm_item(item_id, params = nil, headers = nil)
119
+ 特定の投稿への「いいね!」を取り消します。
120
+
121
+ ### Qiita::Client#list_projects(params = nil, headers = nil)
122
+ チーム内に存在するプロジェクト一覧を返します。
123
+
124
+ ### Qiita::Client#get_project(id, params = nil, headers = nil)
125
+ 特定のプロジェクトを返します。
126
+
127
+ ### Qiita::Client#create_project(params = nil, headers = nil)
128
+ プロジェクトを新たに作成します。
129
+
130
+ ### Qiita::Client#delete_project(id, params = nil, headers = nil)
131
+ 特定のプロジェクトを削除します。
132
+
133
+ ### Qiita::Client#update_project(id, params = nil, headers = nil)
134
+ 特定のプロジェクトを編集します。
135
+
136
+ ### Qiita::Client#list_tags(params = nil, headers = nil)
137
+ 全てのタグ一覧を返します。
138
+
139
+ ### Qiita::Client#get_tag(id, params = nil, headers = nil)
140
+ 特定のタグを返します。
141
+
142
+ ### Qiita::Client#list_user_following_tags(user_id, params = nil, headers = nil)
143
+ 特定のユーザがフォローしているタグ一覧を返します。
144
+
145
+ ### Qiita::Client#list_templates(params = nil, headers = nil)
146
+ 全てのテンプレート一覧を返します。
147
+
148
+ ### Qiita::Client#delete_template(id, params = nil, headers = nil)
149
+ 特定のテンプレートを削除します。
150
+
151
+ ### Qiita::Client#create_template(params = nil, headers = nil)
152
+ 新しくテンプレートを作成します。
153
+
154
+ ### Qiita::Client#update_template(id, params = nil, headers = nil)
155
+ 特定のテンプレートを編集します。
156
+
157
+ ### Qiita::Client#list_users(params = nil, headers = nil)
158
+ 全てのユーザの一覧を返します。
159
+
160
+ ### Qiita::Client#get_user(id, params = nil, headers = nil)
161
+ 特定のユーザを返します。
162
+
163
+ ### Qiita::Client#get_authenticated_user(params = nil, headers = nil)
164
+ アクセストークンに紐付いたユーザを返します。
165
+
166
+ ### Qiita::Client#list_user_followees(user_id, params = nil, headers = nil)
167
+ 特定のユーザがフォローしているユーザ一覧を返します。
168
+
169
+ ### Qiita::Client#list_user_followers(user_id, params = nil, headers = nil)
170
+ 特定のユーザをフォローしているユーザ一覧を返します。
171
+
data/doc/response.md ADDED
@@ -0,0 +1,33 @@
1
+ ### Qiita::Response#body
2
+ Returns response body returned from API as a `Hash` or an `Array` of `Hash`.
3
+
4
+ ```rb
5
+ response.body #=> { ... }
6
+ ```
7
+
8
+ ### Qiita::Response#first_page_url
9
+ Returns first page URL or nil.
10
+
11
+ ### Qiita::Response#last_page_url
12
+ Returns last page URL or nil.
13
+
14
+ ### Qiita::Response#next_page_url
15
+ Returns next page URL or nil.
16
+
17
+ ### Qiita::Response#previous_page_url
18
+ Returns previous page URL or nil.
19
+
20
+ ### Qiita::Response#headers
21
+ Returns response headers returned from API as a `Hash`.
22
+
23
+ ```rb
24
+ response.headers #=> { "Content-Type" => "application/json" }
25
+ ```
26
+
27
+ ### Qiita::Response#status
28
+ Returns response status code returned from API as a `Fixnum`.
29
+
30
+ ```rb
31
+ response.status #=> 200
32
+ ```
33
+
data/images/cli.png ADDED
Binary file
data/lib/qiita.rb CHANGED
@@ -1,16 +1,21 @@
1
+ require "active_support/core_ext/object/blank"
2
+ require "active_support/core_ext/string/inflections"
3
+ require "active_support/core_ext/string/strip"
4
+ require "faraday"
5
+ require "faraday_middleware"
6
+ require "json"
7
+ require "rack/utils"
8
+ require "rainbow"
9
+ require "rouge"
10
+ require "slop"
11
+
12
+ require "qiita/arguments"
1
13
  require "qiita/client"
14
+ require "qiita/command_builder"
15
+ require "qiita/commands/base"
16
+ require "qiita/commands/error"
17
+ require "qiita/commands/request"
18
+ require "qiita/resource_based_methods"
19
+ require "qiita/response"
20
+ require "qiita/response_renderer"
2
21
  require "qiita/version"
3
-
4
- module Qiita
5
- class << self
6
- def new options={}
7
- Qiita::Client.new options
8
- end
9
-
10
- # Delegate to Qiita::Client.new
11
- def method_missing(method, *args, &block)
12
- return super unless new.respond_to?(method)
13
- new.send(method, *args, &block)
14
- end
15
- end
16
- end
@@ -0,0 +1,138 @@
1
+ module Qiita
2
+ class Arguments
3
+ def initialize(argv)
4
+ @argv = argv
5
+ end
6
+
7
+ def access_token
8
+ slop_options["access-token"] || ENV["QIITA_ACCESS_TOKEN"]
9
+ end
10
+
11
+ def arguments
12
+ parsed_argv_data[:arguments]
13
+ end
14
+
15
+ def color
16
+ slop_options["color"]
17
+ end
18
+
19
+ def error_message
20
+ slop_options.to_s
21
+ end
22
+
23
+ def headers
24
+ parsed_argv_data[:headers]
25
+ end
26
+
27
+ def host
28
+ slop_options["host"]
29
+ end
30
+
31
+ def method_name
32
+ ARGV[0]
33
+ end
34
+
35
+ def params
36
+ params_from_stdin.merge(parsed_argv_data[:params])
37
+ end
38
+
39
+ def show_body
40
+ !slop_options["no-body"]
41
+ end
42
+
43
+ def show_header
44
+ slop_options["header"]
45
+ end
46
+
47
+ def team
48
+ slop_options["team"]
49
+ end
50
+
51
+ def valid?
52
+ has_valid_slop_options? && has_valid_method_name? && has_valid_arguments? && !has_invalid_json_input?
53
+ end
54
+
55
+ private
56
+
57
+ def has_input_from_stdin?
58
+ has_pipe_input? || has_redirect_input?
59
+ end
60
+
61
+ def has_invalid_json_input?
62
+ params_from_stdin
63
+ false
64
+ rescue JSON::ParserError
65
+ true
66
+ end
67
+
68
+ def has_pipe_input?
69
+ File.pipe?(STDIN)
70
+ end
71
+
72
+ def has_redirect_input?
73
+ File.select([STDIN], [], [], 0) != nil
74
+ end
75
+
76
+ def has_valid_arguments?
77
+ -(Client.instance_method(method_name).arity) - 1 == arguments.length
78
+ end
79
+
80
+ def has_valid_method_name?
81
+ !method_name.nil? && Client.instance_methods.include?(method_name.to_sym)
82
+ end
83
+
84
+ def has_valid_slop_options?
85
+ !slop_options["help"]
86
+ rescue
87
+ false
88
+ end
89
+
90
+ def params_from_stdin
91
+ @params_from_stdin ||= begin
92
+ if has_input_from_stdin?
93
+ JSON.parse(STDIN.read)
94
+ else
95
+ {}
96
+ end
97
+ end
98
+ end
99
+
100
+ def parsed_argv_data
101
+ @parsed_argv_data ||= begin
102
+ params = {}
103
+ headers = {}
104
+ arguments = []
105
+ ARGV[1..-1].each do |section|
106
+ case
107
+ when /(?<key>.+):(?<value>[^=]+)/ =~ section
108
+ headers[key] = value
109
+ when /(?<key>.+):=(?<value>.+)/ =~ section
110
+ params[key] = JSON.parse(%<{"key":#{value}}>)["key"]
111
+ when /(?<key>.+)=(?<value>.+)/ =~ section
112
+ params[key] = value
113
+ else
114
+ arguments << section
115
+ end
116
+ end
117
+ {
118
+ arguments: arguments,
119
+ headers: headers,
120
+ params: params,
121
+ }
122
+ end
123
+ end
124
+
125
+ def slop_options
126
+ @slop_options ||= Slop.parse!(@argv) do
127
+ banner "Usage: qiita <method> [arguments] [headers|params] [options]"
128
+ on "H", "host=", "Change API server's host"
129
+ on "a", "access-token=", "Access token"
130
+ on "c", "color", "Color output"
131
+ on "h", "help", "Display help message"
132
+ on "header", "Show response header"
133
+ on "no-body", "Hide response body"
134
+ on "t", "team=", "Team name to be used as subdomain"
135
+ end
136
+ end
137
+ end
138
+ end