mt-data_api-client 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 141d6b47bae13a42e500beef930f5c917c3131c6
4
- data.tar.gz: 831b7963bd09646853d89913814e5006ad4175e9
2
+ SHA256:
3
+ metadata.gz: 11f005aa0a6d381cde026e230bb02af3b5bf3e9861668cccd804e4e9e694732c
4
+ data.tar.gz: b15ec76d87923c99699b77329d08bc8ef4dde0136ce3c1d18899f68dc2e8771d
5
5
  SHA512:
6
- metadata.gz: 7e17c5284d00a12494e772f58e2d805dfdb3d6c346f5532863b3282f1c1e3a622b19b87eb0e5a77694c53ecbfde226e0a10d7eac949ca8a2006122d17cd10230
7
- data.tar.gz: 297990eba92223fa81b5d7744bae4a3624e9d5756239d536ea5d96860e73ed63c96e11048cf9022fd0c11e7fe121f5acbfad7a2464513aa5eb9c9059464cc230
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
- # without block
32
- json = client.call(:list_entries, site_id: 1)
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
- # with block
35
- client.call(:list_sites) do |res|
36
- json = res
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
-
@@ -10,9 +10,12 @@ module MT
10
10
  attr_accessor :access_token
11
11
 
12
12
  def initialize(opts)
13
- opts = opts.symbolize_keys
14
- @access_token = opts.delete(:access_token)
15
- @endpoint_manager = EndpointManager.new(opts)
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.set_form_data(args) if post_or_put?
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(access_token, args)
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
@@ -1,7 +1,7 @@
1
1
  module MT
2
2
  module DataAPI
3
3
  class Client
4
- VERSION = '0.0.4'.freeze
4
+ VERSION = '0.0.5'.freeze
5
5
  end
6
6
  end
7
7
  end
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
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-04 00:00:00.000000000 Z
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.4.5.4
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.