kintone 0.1.1 → 0.1.2

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.
@@ -0,0 +1,12 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::BulkRequest < Kintone::Command
4
+ def self.path
5
+ 'bulkRequest'
6
+ end
7
+
8
+ def request(requests)
9
+ response = @api.post(@url, requests: requests)
10
+ response['results']
11
+ end
12
+ end
@@ -17,7 +17,9 @@ class Kintone::Command::Record < Kintone::Command
17
17
  register(app, record)
18
18
  end
19
19
 
20
- def update(app, id, record)
21
- @api.put(@url, app: app, id: id, record: record.to_kintone)
20
+ def update(app, id, record, revision: nil)
21
+ body = { app: app, id: id, record: record.to_kintone }
22
+ body[:revision] = revision if revision
23
+ @api.put(@url, body)
22
24
  end
23
25
  end
@@ -23,8 +23,9 @@ class Kintone::Command::Records < Kintone::Command
23
23
  @api.put(@url, app: app, records: records.to_kintone)
24
24
  end
25
25
 
26
- def delete(app, ids)
26
+ def delete(app, ids, revisions: nil)
27
27
  params = { app: app, ids: ids }
28
+ params[:revisions] = revisions if revisions
28
29
  @api.delete(@url, params)
29
30
  end
30
31
  end
data/lib/kintone/query.rb CHANGED
@@ -68,6 +68,8 @@ class Kintone::Query
68
68
  to_s
69
69
  end
70
70
 
71
+ alias_method :f, :field
72
+
71
73
  private
72
74
 
73
75
  def function_string(function)
@@ -1,3 +1,3 @@
1
1
  module Kintone
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -176,4 +176,16 @@ describe Kintone::Api::Guest do
176
176
 
177
177
  it { is_expected.to be_a_kind_of(Kintone::Command::Apps) }
178
178
  end
179
+
180
+ describe '#bulk_request' do
181
+ subject { target.bulk_request }
182
+
183
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
184
+ end
185
+
186
+ describe '#bulk' do
187
+ subject { target.bulk }
188
+
189
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
190
+ end
179
191
  end
@@ -242,4 +242,16 @@ describe Kintone::Api do
242
242
 
243
243
  it { is_expected.to be_a_kind_of(Kintone::Command::Apis) }
244
244
  end
245
+
246
+ describe '#bulk_request' do
247
+ subject { target.bulk_request }
248
+
249
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
250
+ end
251
+
252
+ describe '#bulk' do
253
+ subject { target.bulk }
254
+
255
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
256
+ end
245
257
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+ require 'kintone/command/bulk_request'
3
+ require 'kintone/api'
4
+
5
+ describe Kintone::Command::BulkRequest do
6
+ let(:target) { Kintone::Command::BulkRequest.new(api) }
7
+ let(:api) { Kintone::Api.new('example.cybozu.com', 'Administrator', 'cybozu') }
8
+
9
+ describe '#request' do
10
+ before(:each) do
11
+ stub_request(
12
+ :post,
13
+ 'https://example.cybozu.com/k/v1/bulkRequest.json'
14
+ )
15
+ .with(body: { requests: requests }.to_json)
16
+ .to_return(body: { 'results' => results }.to_json, status: 200)
17
+ end
18
+
19
+ subject { target.request(requests) }
20
+
21
+ let(:requests) do
22
+ [
23
+ {
24
+ method: 'POST',
25
+ api: '/k/v1/record.json',
26
+ payload: {
27
+ app: 1972,
28
+ record: {
29
+ '文字列__1行' => {
30
+ value: '文字列__1行を追加します。'
31
+ }
32
+ }
33
+ }
34
+ },
35
+ {
36
+ method: 'PUT',
37
+ api: '/k/v1/record.json',
38
+ payload: {
39
+ app: 1973,
40
+ id: 33,
41
+ revision: 2,
42
+ record: {
43
+ '文字列__1行' => {
44
+ value: '文字列__1行を更新します。'
45
+ }
46
+ }
47
+ }
48
+ },
49
+ {
50
+ method: 'DELETE',
51
+ api: '/k/v1/records.json',
52
+ payload: {
53
+ app: 1974,
54
+ ids: [10, 11],
55
+ revisions: [1, 1]
56
+ }
57
+ }
58
+ ]
59
+ end
60
+
61
+ let(:results) do
62
+ [
63
+ { 'id' => '39', 'revision' => '1' },
64
+ { 'revision' => '34' },
65
+ {}
66
+ ]
67
+ end
68
+
69
+ it { is_expected.to eq results }
70
+ end
71
+ end
@@ -36,39 +36,47 @@ describe Kintone::Command::Record do
36
36
  :post,
37
37
  'https://www.example.com/k/v1/record.json'
38
38
  )
39
- .with(body: { 'app' => 7, 'record' => hash_record }.to_json)
40
- .to_return(body: "{\"id\":\"100\"}", status: 200)
39
+ .with(body: request_body.to_json)
40
+ .to_return(body: response_body.to_json, status: 200)
41
41
  end
42
42
 
43
43
  subject { target.register(app, record) }
44
44
 
45
45
  let(:app) { 7 }
46
-
47
- def hash_record
46
+ let(:request_body) do
48
47
  {
49
- 'number' => { 'value' => '123456' },
50
- 'rich_editor' => { 'value' => 'testtest' },
51
- 'user_select' => { 'value' => [{ 'code' => 'sato' }] }
48
+ 'app' => 7,
49
+ 'record' => {
50
+ 'number' => { 'value' => '123456' },
51
+ 'rich_editor' => { 'value' => 'testtest' },
52
+ 'user_select' => { 'value' => [{ 'code' => 'sato' }] }
53
+ }
52
54
  }
53
55
  end
54
-
55
- def record_record
56
- Kintone::Type::Record.new(
57
- number: '123456',
58
- rich_editor: 'testtest',
59
- user_select: [{ 'code' => 'sato' }]
60
- )
56
+ let(:response_body) { { 'id' => '100', 'revision' => '1' } }
57
+
58
+ context 'use hash' do
59
+ let(:record) do
60
+ {
61
+ 'number' => { 'value' => '123456' },
62
+ 'rich_editor' => { 'value' => 'testtest' },
63
+ 'user_select' => { 'value' => [{ 'code' => 'sato' }] }
64
+ }
65
+ end
66
+
67
+ it { expect(subject).to eq response_body }
61
68
  end
62
69
 
63
- where(:record, :result) do
64
- [
65
- [hash_record, { 'id' => '100' }],
66
- [record_record, { 'id' => '100' }]
67
- ]
68
- end
70
+ context 'use record' do
71
+ let(:record) do
72
+ Kintone::Type::Record.new(
73
+ number: '123456',
74
+ rich_editor: 'testtest',
75
+ user_select: [{ 'code' => 'sato' }]
76
+ )
77
+ end
69
78
 
70
- with_them do
71
- it { expect(subject).to eq result }
79
+ it { expect(subject).to eq response_body }
72
80
  end
73
81
  end
74
82
 
@@ -78,34 +86,51 @@ describe Kintone::Command::Record do
78
86
  :put,
79
87
  'https://www.example.com/k/v1/record.json'
80
88
  )
81
- .with(body: { 'app' => 4, 'id' => 1, 'record' => hash_record }.to_json)
82
- .to_return(body: '{}', status: 200)
89
+ .with(body: request_body.to_json)
90
+ .to_return(body: response_body.to_json, status: 200)
83
91
  end
84
92
 
85
93
  subject { target.update(app, id, record) }
86
94
 
87
95
  let(:app) { 4 }
88
96
  let(:id) { 1 }
97
+ let(:hash_record) { { 'string_multi' => { 'value' => 'character string is changed' } } }
98
+ let(:record_record) { Kintone::Type::Record.new(string_multi: 'character string is changed') }
99
+ let(:response_body) { { revision: '2' } }
89
100
 
90
- def hash_record
91
- {
92
- 'string_multi' => { 'value' => 'character string is changed' }
93
- }
94
- end
101
+ context 'without revision' do
102
+ let(:request_body) { { 'app' => 4, 'id' => 1, 'record' => hash_record } }
95
103
 
96
- def record_record
97
- Kintone::Type::Record.new(string_multi: 'character string is changed')
98
- end
104
+ context 'use hash' do
105
+ let(:record) { hash_record }
99
106
 
100
- where(:record, :result) do
101
- [
102
- [hash_record, {}],
103
- [record_record, {}]
104
- ]
107
+ it { expect(subject).to match 'revision' => '2' }
108
+ end
109
+
110
+ context 'use record' do
111
+ let(:record) { record_record }
112
+
113
+ it { expect(subject).to match 'revision' => '2' }
114
+ end
105
115
  end
106
116
 
107
- with_them do
108
- it { expect(subject).to match result }
117
+ context 'with revision' do
118
+ subject { target.update(app, id, record, revision: revision) }
119
+
120
+ let(:revision) { 1 }
121
+ let(:request_body) { { 'app' => 4, 'id' => 1, 'record' => hash_record, 'revision' => 1 } }
122
+
123
+ context 'use hash' do
124
+ let(:record) { hash_record }
125
+
126
+ it { expect(subject).to match 'revision' => '2' }
127
+ end
128
+
129
+ context 'use record' do
130
+ let(:record) { record_record }
131
+
132
+ it { expect(subject).to match 'revision' => '2' }
133
+ end
109
134
  end
110
135
  end
111
136
  end
@@ -92,108 +92,181 @@ describe Kintone::Command::Records do
92
92
  end
93
93
 
94
94
  describe '#register' do
95
- subject { target.register(app, records) }
95
+ before(:each) do
96
+ stub_request(
97
+ :post,
98
+ 'https://example.cybozu.com/k/v1/records.json'
99
+ )
100
+ .with(body: request_body.to_json)
101
+ .to_return(body: response_body.to_json, status: 200)
102
+ end
96
103
 
97
- context '' do
98
- before(:each) do
99
- stub_request(
100
- :post,
101
- 'https://example.cybozu.com/k/v1/records.json'
102
- )
103
- .with(body: { 'app' => 7, 'records' => hash_data }.to_json)
104
- .to_return(body: "{\"ids\":[\"100\", \"101\"]}", status: 200)
105
- end
104
+ subject { target.register(app, records) }
106
105
 
107
- let(:app) { 7 }
106
+ let(:app) { 7 }
107
+ let(:request_body) do
108
+ {
109
+ 'app' => 7,
110
+ 'records' => [
111
+ { 'rich_editor' => { 'value' => 'testtest' } },
112
+ { 'user_select' => { 'value' => [{ 'code' => 'suzuki' }] } }
113
+ ]
114
+ }
115
+ end
116
+ let(:response_body) { { 'ids' => ['100', '101'], 'revisions' => ['1', '1'] } }
108
117
 
109
- def hash_data
118
+ context 'use hash' do
119
+ let(:records) do
110
120
  [
111
- {
112
- 'rich_editor' => { 'value' => 'testtest' }
113
- },
114
- {
115
- 'user_select' => { 'value' => [{ 'code' => 'suzuki' }] }
116
- }
121
+ { 'rich_editor' => { 'value' => 'testtest' } },
122
+ { 'user_select' => { 'value' => [{ 'code' => 'suzuki' }] } }
117
123
  ]
118
124
  end
119
125
 
120
- def record_data
126
+ it { expect(subject).to eq response_body }
127
+ end
128
+
129
+ context 'use record' do
130
+ let(:records) do
121
131
  [
122
132
  Kintone::Type::Record.new(rich_editor: 'testtest'),
123
133
  Kintone::Type::Record.new(user_select: [{ code: 'suzuki' }])
124
134
  ]
125
135
  end
126
136
 
127
- where(:records, :result) do
128
- [
129
- [hash_data, { 'ids' => %w(100 101) }],
130
- [record_data, { 'ids' => %w(100 101) }]
131
- ]
132
- end
133
-
134
- with_them do
135
- it { expect(subject).to eq result }
136
- end
137
+ it { expect(subject).to eq response_body }
137
138
  end
138
139
  end
139
140
 
140
141
  describe '#update' do
142
+ before(:each) do
143
+ stub_request(
144
+ :put,
145
+ 'https://example.cybozu.com/k/v1/records.json'
146
+ )
147
+ .with(body: request_body.to_json)
148
+ .to_return(body: response_body.to_json, status: 200)
149
+ end
150
+
141
151
  subject { target.update(app, records) }
142
152
 
143
- context '' do
144
- before(:each) do
145
- stub_request(
146
- :put,
147
- 'https://example.cybozu.com/k/v1/records.json'
148
- )
149
- .with(body: { 'app' => 4, 'records' => hash_data }.to_json)
150
- .to_return(body: '{}', status: 200)
153
+ let(:app) { 4 }
154
+ let(:response_body) do
155
+ { 'records' => [{ 'id' => '1', 'revision' => '2' }, { 'id' => '2', 'revision' => '2' }] }
156
+ end
157
+
158
+ context 'without revision' do
159
+ let(:request_body) do
160
+ {
161
+ 'app' => 4,
162
+ 'records' => [
163
+ { 'id' => 1, 'record' => { 'string_1' => { 'value' => 'abcdef' } } },
164
+ { 'id' => 2, 'record' => { 'string_multi' => { 'value' => 'opqrstu' } } }
165
+ ]
166
+ }
151
167
  end
152
168
 
153
- let(:app) { 4 }
169
+ context 'use hash' do
170
+ let(:records) do
171
+ [
172
+ { 'id' => 1, 'record' => { 'string_1' => { 'value' => 'abcdef' } } },
173
+ { 'id' => 2, 'record' => { 'string_multi' => { 'value' => 'opqrstu' } } }
174
+ ]
175
+ end
154
176
 
155
- def hash_data
156
- [
157
- { 'id' => 1, 'record' => { 'string_1' => { 'value' => 'abcdef' } } },
158
- { 'id' => 2, 'record' => { 'string_multi' => { 'value' => 'opqrstu' } } }
159
- ]
177
+ it { expect(subject).to eq response_body }
160
178
  end
161
179
 
162
- def record_data
163
- [
164
- { id: 1, record: Kintone::Type::Record.new(string_1: 'abcdef') },
165
- { id: 2, record: Kintone::Type::Record.new(string_multi: 'opqrstu') }
166
- ]
180
+ context 'use record' do
181
+ let(:records) do
182
+ [
183
+ { id: 1, record: Kintone::Type::Record.new(string_1: 'abcdef') },
184
+ { id: 2, record: Kintone::Type::Record.new(string_multi: 'opqrstu') }
185
+ ]
186
+ end
187
+
188
+ it { expect(subject).to eq response_body }
189
+ end
190
+ end
191
+
192
+ context 'with revision' do
193
+ let(:request_body) do
194
+ {
195
+ 'app' => 4,
196
+ 'records' => [
197
+ {
198
+ 'id' => 1,
199
+ 'revision' => 1,
200
+ 'record' => { 'string_1' => { 'value' => 'abcdef' } }
201
+ },
202
+ {
203
+ 'id' => 2,
204
+ 'revision' => 1,
205
+ 'record' => { 'string_multi' => { 'value' => 'opqrstu' } }
206
+ }
207
+ ]
208
+ }
167
209
  end
168
210
 
169
- where(:records, :result) do
170
- [
171
- [hash_data, {}],
172
- [record_data, {}]
173
- ]
211
+ context 'use hash' do
212
+ let(:records) do
213
+ [
214
+ {
215
+ 'id' => 1,
216
+ 'revision' => 1,
217
+ 'record' => { 'string_1' => { 'value' => 'abcdef' } }
218
+ },
219
+ {
220
+ 'id' => 2,
221
+ 'revision' => 1,
222
+ 'record' => { 'string_multi' => { 'value' => 'opqrstu' } }
223
+ }
224
+ ]
225
+ end
226
+
227
+ it { expect(subject).to eq response_body }
174
228
  end
175
229
 
176
- with_them do
177
- it { expect(subject).to eq result }
230
+ context 'use record' do
231
+ let(:records) do
232
+ [
233
+ { id: 1, revision: 1, record: Kintone::Type::Record.new(string_1: 'abcdef') },
234
+ { id: 2, revision: 1, record: Kintone::Type::Record.new(string_multi: 'opqrstu') }
235
+ ]
236
+ end
237
+
238
+ it { expect(subject).to eq response_body }
178
239
  end
179
240
  end
180
241
  end
181
242
 
182
243
  describe '#delete' do
183
- subject { target.delete(app, ids) }
244
+ before(:each) do
245
+ stub_request(
246
+ :delete,
247
+ 'https://example.cybozu.com/k/v1/records.json'
248
+ )
249
+ .with(body: request_body.to_json)
250
+ .to_return(body: '{}', status: 200)
251
+ end
184
252
 
185
- context '' do
186
- before(:each) do
187
- stub_request(
188
- :delete,
189
- 'https://example.cybozu.com/k/v1/records.json'
190
- )
191
- .with(body: { app: app, ids: ids }.to_json)
192
- .to_return(body: '{}', status: 200)
193
- end
253
+ context 'without revisions' do
254
+ subject { target.delete(app, ids) }
255
+
256
+ let(:app) { 1 }
257
+ let(:ids) { [100, 80] }
258
+ let(:request_body) { { app: app, ids: ids } }
259
+
260
+ it { expect(subject).to eq({}) }
261
+ end
262
+
263
+ context 'with revisions' do
264
+ subject { target.delete(app, ids, revisions: revisions) }
194
265
 
195
266
  let(:app) { 1 }
196
267
  let(:ids) { [100, 80] }
268
+ let(:revisions) { [1, 4] }
269
+ let(:request_body) { { app: app, ids: ids, revisions: revisions } }
197
270
 
198
271
  it { expect(subject).to eq({}) }
199
272
  end