kintone-client 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06e0fafa79ca0b5769cd0179135d5dd6d655dde3
4
- data.tar.gz: 16c00fb2ef6d8e7f7d907ffc08ad5fd690939a55
3
+ metadata.gz: 951cf372212f60ae0f4d23e5082ef2d3f4d45d04
4
+ data.tar.gz: f8e03d313b23c9056e5176d0b250ee091a82ff3a
5
5
  SHA512:
6
- metadata.gz: d54c24e2d01c6dda8b976c03c2772d1ca64c1bf5c0cf2352dd0b050a9bb0c4ebd67dacb6010afef8a3b93437fe4f25c3bd18463ba1ec2e0d4cf150d5dcdb10a7
7
- data.tar.gz: 92a196e4c3dbb3c0afaef293ef980bf1c10f16f1804dedafdac3632915664d12b116bc3d356a6626b4e1a872169b17b5b556a8139019cae017390ab44472b538
6
+ metadata.gz: a5ec1cbee49c0444bf05e002d5316ace64c18edba60cf6f6e0c7d5f2eee5757fbe570415dca7643eb6304b7ad00f2ca8c07733bb1d2a3ef944f55d32807ceee2
7
+ data.tar.gz: db3afefc783cc053c54c7539f5970daf07845432a304bb09d77809b7ca037d77507bf3450e6614d20e77566d574bdd0395a45fc52bfb4c96d65b3dd1396fe749
@@ -1,4 +1,6 @@
1
1
  require 'base64'
2
+ require 'json'
3
+
2
4
  require 'faraday'
3
5
  require 'faraday_middleware'
4
6
 
@@ -6,4 +8,5 @@ module Kintone; end
6
8
  require 'kintone/client/client'
7
9
  require 'kintone/client/error'
8
10
  require 'kintone/client/middleware/form'
11
+ require 'kintone/client/middleware/record'
9
12
  require 'kintone/client/version'
@@ -20,8 +20,9 @@ class Kintone::Client
20
20
 
21
21
  @conn = Faraday.new(options) do |faraday|
22
22
  faraday.request :url_encoded
23
- faraday.response :form, :content_type => /\bjson$/ # must set before :json
24
- faraday.response :json, :content_type => /\bjson$/
23
+ faraday.request :record, :content_type => /\bjson$/
24
+ faraday.response :form, :content_type => /\bjson$/ # must set before :json
25
+ faraday.response :json, :content_type => /\bjson$/
25
26
 
26
27
  yield(faraday) if block_given?
27
28
 
@@ -54,11 +55,19 @@ class Kintone::Client
54
55
 
55
56
  private
56
57
 
57
- def request(method_name, params)
58
+ def request(method_name, params, options = {})
58
59
  response = @conn.send(method_name) do |req|
59
60
  req.url BASE_PATH + '/' + @path + '.json'
60
- req.params = expand_params_array(params || {})
61
+
62
+ if options[:json]
63
+ req.body = JSON.dump(params)
64
+ req.headers['Content-Type'] = 'application/json'
65
+ else
66
+ req.params = expand_params_array(params || {})
67
+ end
68
+
61
69
  authorize(req)
70
+
62
71
  yield(req) if block_given?
63
72
  end
64
73
 
@@ -117,7 +126,9 @@ class Kintone::Client
117
126
  end
118
127
 
119
128
  def method_missing(method_name, *args, &block)
120
- if [:get, :post, :put, :delete].include?(method_name)
129
+ method_name = method_name.to_s
130
+
131
+ if %w(get post put delete post_json put_json delete_json).include?(method_name)
121
132
  case args.length
122
133
  when 0
123
134
  args = nil
@@ -125,7 +136,14 @@ class Kintone::Client
125
136
  args = args.first
126
137
  end
127
138
 
128
- request(method_name, args, &block)
139
+ options = {}
140
+
141
+ if method_name =~ /_json\z/
142
+ method_name.sub!(/_json\z/, '')
143
+ options[:json] = true
144
+ end
145
+
146
+ request(method_name, args, options, &block)
129
147
  else
130
148
  unless args.length.zero?
131
149
  raise ArgumentError, "wrong number of arguments (#{args.length} for 0)"
@@ -0,0 +1,76 @@
1
+ module Kintone::Client::Middleware
2
+ class Record < Faraday::Middleware
3
+ def initialize(app, options = {})
4
+ super(app)
5
+ @options = options
6
+ end
7
+
8
+ def call(env)
9
+ match_content_type(env) do
10
+ env[:body] = expand_body(env[:body])
11
+ end
12
+
13
+ @app.call env
14
+ end
15
+
16
+ private
17
+
18
+ def match_content_type(env)
19
+ content_type = env[:request_headers]['Content-Type']
20
+
21
+ return unless content_type
22
+
23
+ content_type = content_type.split(';').first.strip
24
+ opt_content_type = @options[:content_type] || /.*/
25
+
26
+ if env[:body] and opt_content_type =~ content_type
27
+ yield
28
+ end
29
+ end
30
+
31
+ def expand_body(body)
32
+ body = JSON.parse(body)
33
+
34
+ if body['record']
35
+ body['record'] = expand_record(body['record'])
36
+ elsif body['requests']
37
+ if_has_record(body['requests']) do |request|
38
+ record = request['payload']['record']
39
+ next unless record
40
+ request['payload']['record'] = expand_record(record)
41
+ end
42
+ end
43
+
44
+ JSON.dump(body)
45
+ end
46
+
47
+ def expand_record(record)
48
+ expanded = {}
49
+
50
+ record.each do |key, value|
51
+ case value
52
+ when Array, Hash
53
+ expanded[key] = value
54
+ else
55
+ expanded[key] = {
56
+ 'value' => value.to_s
57
+ }
58
+ end
59
+ end
60
+
61
+ expanded
62
+ end
63
+
64
+ def if_has_record(requests)
65
+ if requests[0]['payload']['record']
66
+ requests.each do |request|
67
+ yield(request)
68
+ end
69
+ end
70
+ rescue
71
+ # nothing to do
72
+ end
73
+ end
74
+ end
75
+
76
+ Faraday::Request.register_middleware :record => Kintone::Client::Middleware::Record
@@ -1,5 +1,5 @@
1
1
  module Kintone
2
2
  class Client
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -0,0 +1,72 @@
1
+ # https://cybozudev.zendesk.com/hc/ja/articles/201941814-%E8%A4%87%E6%95%B0%E3%82%A2%E3%83%97%E3%83%AA%E3%81%B8%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E4%B8%80%E6%8B%AC%E5%87%A6%E7%90%86
2
+ describe Kintone::Client do
3
+ describe '複数アプリへのレコード一括処理' do
4
+ let(:request) do
5
+ {"requests"=>
6
+ [{"method"=>"POST",
7
+ "api"=>"/k/v1/record.json",
8
+ "payload"=>
9
+ {"app"=>1972, "record"=>{"文字列__1行"=>{"value"=>"文字列__1行を追加します。"}}}},
10
+ {"method"=>"PUT",
11
+ "api"=>"/k/v1/record.json",
12
+ "payload"=>
13
+ {"app"=>1973,
14
+ "id"=>33,
15
+ "revision"=>2,
16
+ "record"=>{"文字列__1行"=>{"value"=>"文字列__1行を更新します。"}}}},
17
+ {"method"=>"DELETE",
18
+ "api"=>"/k/v1/records.json",
19
+ "payload"=>{"app"=>1974, "ids"=>[10, 11], "revisions"=>[1, 1]}}]}
20
+ end
21
+
22
+ let(:unexpanded_request) do
23
+ {"requests"=>
24
+ [{"method"=>"POST",
25
+ "api"=>"/k/v1/record.json",
26
+ "payload"=>
27
+ {"app"=>1972, "record"=>{"文字列__1行"=>"文字列__1行を追加します。"}}},
28
+ {"method"=>"PUT",
29
+ "api"=>"/k/v1/record.json",
30
+ "payload"=>
31
+ {"app"=>1973,
32
+ "id"=>33,
33
+ "revision"=>2,
34
+ "record"=>{"文字列__1行"=>"文字列__1行を更新します。"}}},
35
+ {"method"=>"DELETE",
36
+ "api"=>"/k/v1/records.json",
37
+ "payload"=>{"app"=>1974, "ids"=>[10, 11], "revisions"=>[1, 1]}}]}
38
+ end
39
+
40
+ let(:response) do
41
+ {"results"=>[{"id"=>"39", "revision"=>"1"}, {"revision"=>"34"}, {}]}
42
+ end
43
+
44
+ it do
45
+ client = kintone_client do |stub|
46
+ stub.post('/k/v1/bulkRequest.json') do |env|
47
+ expect(env.body).to eq JSON.dump(request)
48
+ expect(env.request_headers['X-Cybozu-Authorization']).to eq TEST_AUTH_HEADER
49
+ expect(env.request_headers['Content-Type']).to eq 'application/json'
50
+ [200, {'Content-Type' => 'json'}, JSON.dump(response)]
51
+ end
52
+ end
53
+
54
+ result = client.bulkRequest.post_json(request)
55
+ expect(result).to eq response
56
+ end
57
+
58
+ it do
59
+ client = kintone_client do |stub|
60
+ stub.post('/k/v1/bulkRequest.json') do |env|
61
+ expect(env.body).to eq JSON.dump(request)
62
+ expect(env.request_headers['X-Cybozu-Authorization']).to eq TEST_AUTH_HEADER
63
+ expect(env.request_headers['Content-Type']).to eq 'application/json'
64
+ [200, {'Content-Type' => 'json'}, JSON.dump(response)]
65
+ end
66
+ end
67
+
68
+ result = client.bulkRequest.post_json(unexpanded_request)
69
+ expect(result).to eq response
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,64 @@
1
+ # https://cybozudev.zendesk.com/hc/ja/articles/202166160-%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E7%99%BB%E9%8C%B2-POST-
2
+ describe Kintone::Client do
3
+ describe 'レコードの登録(1件)' do
4
+ let(:request) do
5
+ {"app"=>1972,
6
+ "record"=>
7
+ {"文字列__1行"=>{"value"=>"テスト"},
8
+ "文字列__複数行"=>{"value"=>"テスト\nテスト2"},
9
+ "数値"=>{"value"=>"20"},
10
+ "日時"=>{"value"=>"2014-02-16T08:57:00Z"},
11
+ "チェックボックス"=>{"value"=>["sample1", "sample2"]},
12
+ "ユーザー選択"=>{"value"=>[{"code"=>"sato"}]},
13
+ "ドロップダウン"=>{"value"=>"sample1"},
14
+ "リンク_ウェブ"=>{"value"=>"https://www.cybozu.com"},
15
+ "Table"=>{"value"=>[{"value"=>{"テーブル文字列"=>{"value"=>"テスト"}}}]}}}
16
+ end
17
+
18
+ let(:unexpanded_request) do
19
+ {"app"=>1972,
20
+ "record"=>
21
+ {"文字列__1行"=>"テスト",
22
+ "文字列__複数行"=>"テスト\nテスト2",
23
+ "数値"=>20,
24
+ "日時"=>"2014-02-16T08:57:00Z",
25
+ "チェックボックス"=>{"value"=>["sample1", "sample2"]},
26
+ "ユーザー選択"=>{"value"=>[{"code"=>"sato"}]},
27
+ "ドロップダウン"=>"sample1",
28
+ "リンク_ウェブ"=>"https://www.cybozu.com",
29
+ "Table"=>{"value"=>[{"value"=>{"テーブル文字列"=>{"value"=>"テスト"}}}]}}}
30
+ end
31
+
32
+ let(:response) do
33
+ {"ids"=>["100", "101"], "revisions"=>["1", "1"]}
34
+ end
35
+
36
+ it do
37
+ client = kintone_client do |stub|
38
+ stub.post('/k/v1/record.json') do |env|
39
+ expect(env.body).to eq JSON.dump(request)
40
+ expect(env.request_headers['X-Cybozu-Authorization']).to eq TEST_AUTH_HEADER
41
+ expect(env.request_headers['Content-Type']).to eq 'application/json'
42
+ [200, {'Content-Type' => 'json'}, JSON.dump(response)]
43
+ end
44
+ end
45
+
46
+ result = client.record.post_json(request)
47
+ expect(result).to eq response
48
+ end
49
+
50
+ it do
51
+ client = kintone_client do |stub|
52
+ stub.post('/k/v1/record.json') do |env|
53
+ expect(env.body).to eq JSON.dump(request)
54
+ expect(env.request_headers['X-Cybozu-Authorization']).to eq TEST_AUTH_HEADER
55
+ expect(env.request_headers['Content-Type']).to eq 'application/json'
56
+ [200, {'Content-Type' => 'json'}, JSON.dump(response)]
57
+ end
58
+ end
59
+
60
+ result = client.record.post_json(unexpanded_request)
61
+ expect(result).to eq response
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kintone-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
@@ -112,9 +112,12 @@ files:
112
112
  - lib/kintone/client/client.rb
113
113
  - lib/kintone/client/error.rb
114
114
  - lib/kintone/client/middleware/form.rb
115
+ - lib/kintone/client/middleware/record.rb
115
116
  - lib/kintone/client/version.rb
116
117
  - spec/kintone/client/app_spec.rb
117
118
  - spec/kintone/client/apps_spec.rb
119
+ - spec/kintone/client/bulkRequest_spec.rb
120
+ - spec/kintone/client/record_post_spec.rb
118
121
  - spec/kintone/client/record_spec.rb
119
122
  - spec/kintone/client/records_spec.rb
120
123
  - spec/kintone/client/space_spec.rb
@@ -146,6 +149,8 @@ summary: It is a simple client of cybozu kintone.
146
149
  test_files:
147
150
  - spec/kintone/client/app_spec.rb
148
151
  - spec/kintone/client/apps_spec.rb
152
+ - spec/kintone/client/bulkRequest_spec.rb
153
+ - spec/kintone/client/record_post_spec.rb
149
154
  - spec/kintone/client/record_spec.rb
150
155
  - spec/kintone/client/records_spec.rb
151
156
  - spec/kintone/client/space_spec.rb