mt-data_api-client 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +54 -10
- data/lib/mt/data_api/client.rb +12 -3
- data/lib/mt/data_api/client/api_request.rb +27 -1
- data/lib/mt/data_api/client/endpoint.rb +17 -2
- data/lib/mt/data_api/client/endpoint_manager.rb +1 -2
- data/lib/mt/data_api/client/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 11f005aa0a6d381cde026e230bb02af3b5bf3e9861668cccd804e4e9e694732c
|
4
|
+
data.tar.gz: b15ec76d87923c99699b77329d08bc8ef4dde0136ce3c1d18899f68dc2e8771d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5faa4a7416e05baf735938bfc0829175569a0aa0fb787b8aec0a701a47cd520c99968cdf726e7e14984c490cb28582bd2b87bb2b6e9601fffe0116f971f6812
|
7
|
+
data.tar.gz: 433aab34562e93dd30aa873cc47e03c567e271b0020efbb0752f579af348f07f0baeab59e1e9ea00d0f011841ffbfe6e5a78c62484b589f997ae1607d962bc5d
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# MT::DataAPI::Client
|
2
2
|
|
3
|
-
Movable Type Data API client for Ruby.
|
3
|
+
Movable Type Data API client for Ruby.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,22 +18,67 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install mt-data_api-client
|
20
20
|
|
21
|
-
## Usage
|
21
|
+
## Usage samples
|
22
22
|
|
23
23
|
```ruby
|
24
24
|
require 'mt/data_api/client'
|
25
25
|
|
26
|
+
# Set URL to your mt-data-api.cgi script.
|
26
27
|
client = MT::DataAPI::Client.new(
|
27
|
-
base_url: 'http://localhost/mt/mt-data-api.cgi'
|
28
|
-
client_id: 'mt-ruby'
|
28
|
+
base_url: 'http://localhost/mt/mt-data-api.cgi'
|
29
29
|
)
|
30
30
|
|
31
|
-
#
|
32
|
-
|
31
|
+
# Set endpoint ID and its arguments.
|
32
|
+
#
|
33
|
+
# * You can see the list of endpoints by "client.call(:list_endpoints)"
|
34
|
+
# * There is documentation of arguments at https://www.movabletype.jp/developers/data-api/.
|
35
|
+
res = client.call(:list_endpoints)
|
36
|
+
```
|
37
|
+
|
38
|
+
### With block
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'mt/data_api/client'
|
42
|
+
|
43
|
+
client = MT::DataAPI::Client.new(
|
44
|
+
base_url: 'http://localhost/mt/mt-data-api.cgi'
|
45
|
+
)
|
33
46
|
|
34
|
-
|
35
|
-
|
36
|
-
|
47
|
+
client.call(:list_entries, site_id: 1) do |res|
|
48
|
+
# ...
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
### Use authentication
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
require 'mt/data_api/client'
|
56
|
+
|
57
|
+
client = MT::DataAPI::Client.new(
|
58
|
+
base_url: 'http://loalhost/mt/mt-data-api.cgi'
|
59
|
+
)
|
60
|
+
|
61
|
+
res_auth = client.call(:authenticate, username: 'user', password: 'pass')
|
62
|
+
|
63
|
+
if res_auth && !res_auth.key?("error")
|
64
|
+
res_list_plugins = client.call(:list_plugins)
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### Use upload_asset endpoint
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require 'mt/data_api/client'
|
72
|
+
|
73
|
+
client = MT::DataAPI::Client.new(
|
74
|
+
base_url: 'http://mt.test:5000/mt-data-api.cgi'
|
75
|
+
)
|
76
|
+
|
77
|
+
res_auth = client.call(:authenticate, username: 'user', password: 'pass')
|
78
|
+
|
79
|
+
if res_auth && !res_auth.key?("error")
|
80
|
+
jpeg_file = File.open('sample.jpg', 'rb')
|
81
|
+
res = client.call(:upload_asset, site_id: 1, file: jpeg_file)
|
37
82
|
end
|
38
83
|
```
|
39
84
|
|
@@ -46,4 +91,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
46
91
|
## Contributing
|
47
92
|
|
48
93
|
Bug reports and pull requests are welcome on GitHub at https://github.com/masiuchi/mt-data_api-client.
|
49
|
-
|
data/lib/mt/data_api/client.rb
CHANGED
@@ -10,9 +10,12 @@ module MT
|
|
10
10
|
attr_accessor :access_token
|
11
11
|
|
12
12
|
def initialize(opts)
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
opts_sym = opts.symbolize_keys
|
14
|
+
unless opts_sym.key?(:client_id)
|
15
|
+
opts_sym[:client_id] = default_client_id
|
16
|
+
end
|
17
|
+
@access_token = opts_sym.delete(:access_token)
|
18
|
+
@endpoint_manager = EndpointManager.new(opts_sym)
|
16
19
|
end
|
17
20
|
|
18
21
|
def call(id, args = {})
|
@@ -28,6 +31,12 @@ module MT
|
|
28
31
|
def endpoints
|
29
32
|
@endpoint_manager.endpoints
|
30
33
|
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def default_client_id
|
38
|
+
'mt-data_api-client version ' + VERSION
|
39
|
+
end
|
31
40
|
end
|
32
41
|
end
|
33
42
|
end
|
@@ -30,17 +30,43 @@ module MT
|
|
30
30
|
if access_token
|
31
31
|
req['X-MT-Authorization'] = "MTAuth accessToken=#{access_token}"
|
32
32
|
end
|
33
|
-
req
|
33
|
+
set_form(req, args) if post_or_put?
|
34
34
|
req
|
35
35
|
end
|
36
36
|
|
37
|
+
def contains_file_object?(args)
|
38
|
+
args.values.any? { |v| file?(v) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def file?(file)
|
42
|
+
file.is_a?(File) && File.file?(file.path)
|
43
|
+
end
|
44
|
+
|
37
45
|
def http_request_class
|
38
46
|
Object.const_get("Net::HTTP::#{@endpoint.verb.capitalize}")
|
39
47
|
end
|
40
48
|
|
49
|
+
def multipart_args(args)
|
50
|
+
args.map do |k, v|
|
51
|
+
if file?(v)
|
52
|
+
[k.to_s, v, { filename: File.basename(v.path) }]
|
53
|
+
else
|
54
|
+
[k.to_s, v.to_s]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
41
59
|
def post_or_put?
|
42
60
|
%w[POST PUT].include? @endpoint.verb
|
43
61
|
end
|
62
|
+
|
63
|
+
def set_form(req, args)
|
64
|
+
if contains_file_object?(args)
|
65
|
+
req.set_form(multipart_args(args), 'multipart/form-data')
|
66
|
+
else
|
67
|
+
req.set_form_data(args)
|
68
|
+
end
|
69
|
+
end
|
44
70
|
end
|
45
71
|
end
|
46
72
|
end
|
@@ -10,7 +10,7 @@ module MT
|
|
10
10
|
# Send request to endpoint.
|
11
11
|
class Endpoint
|
12
12
|
class << self
|
13
|
-
attr_writer :api_url
|
13
|
+
attr_writer :api_url, :client_id
|
14
14
|
end
|
15
15
|
|
16
16
|
attr_reader :verb
|
@@ -28,7 +28,10 @@ module MT
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def call(access_token = nil, args = {})
|
31
|
-
res = APIRequest.new(self).send(
|
31
|
+
res = APIRequest.new(self).send(
|
32
|
+
access_token,
|
33
|
+
client_id_hash.clone.merge(args)
|
34
|
+
)
|
32
35
|
return nil if res.body.nil?
|
33
36
|
JSON.parse(res.body)
|
34
37
|
end
|
@@ -41,6 +44,18 @@ module MT
|
|
41
44
|
|
42
45
|
private
|
43
46
|
|
47
|
+
def auth_id?
|
48
|
+
%w[authenticate authorize].include?(@id)
|
49
|
+
end
|
50
|
+
|
51
|
+
def client_id_hash
|
52
|
+
if auth_id?
|
53
|
+
{ clientId: self.class.instance_variable_get(:@client_id) }
|
54
|
+
else
|
55
|
+
{}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
44
59
|
def route(args = {})
|
45
60
|
route = @route.dup
|
46
61
|
route.scan(%r{:[^:/]+(?=/|$)}).each do |m|
|
@@ -16,13 +16,13 @@ module MT
|
|
16
16
|
}.freeze
|
17
17
|
|
18
18
|
attr_accessor :endpoints
|
19
|
-
attr_reader :access_token
|
20
19
|
|
21
20
|
def initialize(opts)
|
22
21
|
raise parameter_should_be_hash unless opts.is_a? Hash
|
23
22
|
initialize_parameters(opts.symbolize_keys)
|
24
23
|
raise invalid_parameter unless @base_url && @client_id
|
25
24
|
Endpoint.api_url = api_url
|
25
|
+
Endpoint.client_id = @client_id
|
26
26
|
end
|
27
27
|
|
28
28
|
def find_endpoint(id)
|
@@ -37,7 +37,6 @@ module MT
|
|
37
37
|
@client_id = opts[:client_id]
|
38
38
|
@api_version = opts[:api_version] || DEFAULT_API_VERSION
|
39
39
|
@endpoints = opts[:endpoints] if opts.key? :endpoints
|
40
|
-
@access_token = opts[:access_token] if opts.key? :access_token
|
41
40
|
end
|
42
41
|
|
43
42
|
def parameter_should_be_hash
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mt-data_api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masahiro Iuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
179
179
|
version: '0'
|
180
180
|
requirements: []
|
181
181
|
rubyforge_project:
|
182
|
-
rubygems_version: 2.
|
182
|
+
rubygems_version: 2.7.3
|
183
183
|
signing_key:
|
184
184
|
specification_version: 4
|
185
185
|
summary: Movable Type Data API client for Ruby.
|