qiita 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +1 -0
- data/lib/faraday/response/raise_qiita_error.rb +32 -0
- data/lib/qiita.rb +16 -0
- data/lib/qiita/client.rb +84 -0
- data/lib/qiita/client/items.rb +33 -0
- data/lib/qiita/client/tags.rb +13 -0
- data/lib/qiita/client/users.rb +19 -0
- data/lib/qiita/error.rb +8 -0
- data/lib/qiita/version.rb +3 -0
- data/qiita.gemspec +26 -0
- metadata +109 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Hiroshige Umino
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Qiita
|
2
|
+
|
3
|
+
Ruby wrapper for Qiita API v1.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'qiita'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install qiita
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Get user's items
|
22
|
+
```ruby
|
23
|
+
Qiita.user_items 'yaotti'
|
24
|
+
```
|
25
|
+
|
26
|
+
### Get tag's items
|
27
|
+
```ruby
|
28
|
+
Qiita.tag_items 'ruby'
|
29
|
+
```
|
30
|
+
|
31
|
+
### Get a specified item with comments and raw markdown content
|
32
|
+
```ruby
|
33
|
+
item_uuid = '1234567890abcdefg'
|
34
|
+
Qiita.item item_uuid
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
## Authenticated requests
|
39
|
+
|
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
|
+
```
|
46
|
+
|
47
|
+
### Get my items
|
48
|
+
```ruby
|
49
|
+
qiita.user_items
|
50
|
+
```
|
51
|
+
|
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
|
56
|
+
|
57
|
+
# update
|
58
|
+
qiita.update_item item.uuid, title: 'modified'
|
59
|
+
|
60
|
+
# delete
|
61
|
+
qiita.delete_item item.uuid
|
62
|
+
```
|
63
|
+
|
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
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Response::RaiseQiitaError < Response::Middleware
|
5
|
+
def on_complete(response)
|
6
|
+
case response[:status]
|
7
|
+
when 400
|
8
|
+
raise Qiita::BadRequest, error_message(response)
|
9
|
+
when 401
|
10
|
+
raise Qiita::Unauthorized, error_message(response)
|
11
|
+
when 403
|
12
|
+
raise Qiita::Forbidden, error_message(response)
|
13
|
+
when 404
|
14
|
+
raise Qiita::NotFound, error_message(response)
|
15
|
+
when 406
|
16
|
+
raise Qiita::NotAcceptable, error_message(response)
|
17
|
+
when 422
|
18
|
+
raise Qiita::UnprocessableEntity, error_message(response)
|
19
|
+
when 500
|
20
|
+
raise Qiita::InternalServerError, error_message(response)
|
21
|
+
when 503
|
22
|
+
raise Qiita::ServiceUnavailable, error_message(response)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def error_message(response)
|
27
|
+
message = response[:body]['error']
|
28
|
+
return message unless message.empty?
|
29
|
+
"#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/qiita.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "qiita/client"
|
2
|
+
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
|
data/lib/qiita/client.rb
ADDED
@@ -0,0 +1,84 @@
|
|
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'
|
10
|
+
|
11
|
+
module Qiita
|
12
|
+
class Client
|
13
|
+
ROOT_URL = 'https://qiita.com/'
|
14
|
+
OPTIONS_KEYS = [:url_name, :password, :token].freeze
|
15
|
+
|
16
|
+
attr_accessor *OPTIONS_KEYS
|
17
|
+
|
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
|
25
|
+
end
|
26
|
+
|
27
|
+
def rate_limit params={}
|
28
|
+
get '/rate_limit', params
|
29
|
+
end
|
30
|
+
|
31
|
+
include Qiita::Client::Items
|
32
|
+
include Qiita::Client::Tags
|
33
|
+
include Qiita::Client::Users
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def login
|
38
|
+
json = post '/auth', { :url_name => @url_name, :password => @password }
|
39
|
+
@token = json['token']
|
40
|
+
end
|
41
|
+
|
42
|
+
def connection
|
43
|
+
@connection ||= Faraday.new(:url => ROOT_URL) do |faraday|
|
44
|
+
faraday.request :json
|
45
|
+
faraday.adapter Faraday.default_adapter
|
46
|
+
faraday.use Faraday::Response::RaiseQiitaError
|
47
|
+
faraday.use FaradayMiddleware::Mashify
|
48
|
+
faraday.use FaradayMiddleware::ParseJson
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def get(path, params={})
|
53
|
+
request(:get, path, params)
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete(path, params={})
|
57
|
+
request(:delete, path, params)
|
58
|
+
end
|
59
|
+
|
60
|
+
def post(path, params={})
|
61
|
+
request(:post, path, params)
|
62
|
+
end
|
63
|
+
|
64
|
+
def put(path, params={})
|
65
|
+
request(:put, path, params)
|
66
|
+
end
|
67
|
+
|
68
|
+
def request(method, path, params)
|
69
|
+
path = "/api/v1/#{path}"
|
70
|
+
params.merge!(:token => token) if token
|
71
|
+
response = connection.send(method) do |req|
|
72
|
+
req.headers['Content-Type'] = 'application/json'
|
73
|
+
case method
|
74
|
+
when :get, :delete
|
75
|
+
req.url path, params
|
76
|
+
when :post, :put
|
77
|
+
req.path = path
|
78
|
+
req.body = params.to_json unless params.empty?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
response.body
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Qiita
|
2
|
+
class Client
|
3
|
+
module Items
|
4
|
+
def post_item(params)
|
5
|
+
post '/items', params
|
6
|
+
end
|
7
|
+
|
8
|
+
def update_item(uuid, params)
|
9
|
+
put "/items/#{uuid}", params
|
10
|
+
end
|
11
|
+
|
12
|
+
def delete_item(uuid)
|
13
|
+
delete "/items/#{uuid}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def item(uuid)
|
17
|
+
get "/items/#{uuid}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def search_items(query, params)
|
21
|
+
get "/search", params.merge(:q => query)
|
22
|
+
end
|
23
|
+
|
24
|
+
def stock_item(uuid)
|
25
|
+
put "/items/#{uuid}/stock"
|
26
|
+
end
|
27
|
+
|
28
|
+
def unstock_item(uuid)
|
29
|
+
delete "/items/#{uuid}/stock"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Qiita
|
2
|
+
class Client
|
3
|
+
module Users
|
4
|
+
def user_items(url_name=nil, params={})
|
5
|
+
path = url_name ? "/users/#{url_name}/items" : '/items'
|
6
|
+
get path, params
|
7
|
+
end
|
8
|
+
|
9
|
+
def user_stocks(url_name=nil, params={})
|
10
|
+
path = url_name ? "/users/#{url_name}/stocks" : '/stocks'
|
11
|
+
get path, params
|
12
|
+
end
|
13
|
+
|
14
|
+
def user(url_name)
|
15
|
+
get "/users/#{url_name}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/qiita/error.rb
ADDED
data/qiita.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qiita/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "qiita"
|
8
|
+
gem.version = Qiita::VERSION
|
9
|
+
gem.authors = ["Hiroshige Umino"]
|
10
|
+
gem.email = ["yaotti@qiita.com"]
|
11
|
+
gem.description = <<desc
|
12
|
+
Gets some tag's or user's items at qiita.com.
|
13
|
+
Creates, updates, deletes and stocks items at Qiita.
|
14
|
+
desc
|
15
|
+
gem.summary = "Ruby wrapper for Qiita API v1."
|
16
|
+
gem.homepage = "http://github.com/yaotti/qiita-rb"
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
|
23
|
+
gem.add_dependency 'faraday', '~> 0.8'
|
24
|
+
gem.add_dependency 'faraday_middleware', '~> 0.8'
|
25
|
+
gem.add_dependency 'json', '~> 1.7'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qiita
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hiroshige Umino
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.8'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.8'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: faraday_middleware
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0.8'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0.8'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: json
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.7'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
description: ! " Gets some tag's or user's items at qiita.com.\n Creates, updates,
|
63
|
+
deletes and stocks items at Qiita.\n"
|
64
|
+
email:
|
65
|
+
- yaotti@qiita.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/faraday/response/raise_qiita_error.rb
|
76
|
+
- lib/qiita.rb
|
77
|
+
- lib/qiita/client.rb
|
78
|
+
- lib/qiita/client/items.rb
|
79
|
+
- lib/qiita/client/tags.rb
|
80
|
+
- lib/qiita/client/users.rb
|
81
|
+
- lib/qiita/error.rb
|
82
|
+
- lib/qiita/version.rb
|
83
|
+
- qiita.gemspec
|
84
|
+
homepage: http://github.com/yaotti/qiita-rb
|
85
|
+
licenses: []
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.23
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Ruby wrapper for Qiita API v1.
|
108
|
+
test_files: []
|
109
|
+
has_rdoc:
|