kintone_rb 1.0.0

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.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.github/dependabot.yml +7 -0
  3. data/.github/workflows/rspec.yml +28 -0
  4. data/.gitignore +20 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +47 -0
  7. data/.ruby-version +1 -0
  8. data/CHANGELOG.md +4 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +361 -0
  12. data/Rakefile +6 -0
  13. data/kintone.gemspec +30 -0
  14. data/lib/kintone/api/guest.rb +44 -0
  15. data/lib/kintone/api.rb +121 -0
  16. data/lib/kintone/command/accessor.rb +109 -0
  17. data/lib/kintone/command/apis.rb +22 -0
  18. data/lib/kintone/command/app.rb +11 -0
  19. data/lib/kintone/command/app_acl.rb +11 -0
  20. data/lib/kintone/command/apps.rb +12 -0
  21. data/lib/kintone/command/bulk_request.rb +12 -0
  22. data/lib/kintone/command/field_acl.rb +11 -0
  23. data/lib/kintone/command/file.rb +15 -0
  24. data/lib/kintone/command/form.rb +11 -0
  25. data/lib/kintone/command/guests.rb +17 -0
  26. data/lib/kintone/command/preview_form.rb +11 -0
  27. data/lib/kintone/command/record.rb +23 -0
  28. data/lib/kintone/command/record_acl.rb +11 -0
  29. data/lib/kintone/command/records.rb +29 -0
  30. data/lib/kintone/command/space.rb +15 -0
  31. data/lib/kintone/command/space_body.rb +11 -0
  32. data/lib/kintone/command/space_guests.rb +11 -0
  33. data/lib/kintone/command/space_members.rb +16 -0
  34. data/lib/kintone/command/space_thread.rb +14 -0
  35. data/lib/kintone/command/template_space.rb +12 -0
  36. data/lib/kintone/command.rb +12 -0
  37. data/lib/kintone/kintone_error.rb +12 -0
  38. data/lib/kintone/query/extension.rb +23 -0
  39. data/lib/kintone/query.rb +152 -0
  40. data/lib/kintone/type/extension/enumerable.rb +5 -0
  41. data/lib/kintone/type/extension/hash.rb +5 -0
  42. data/lib/kintone/type/extension/object.rb +5 -0
  43. data/lib/kintone/type/record.rb +11 -0
  44. data/lib/kintone/type.rb +6 -0
  45. data/lib/kintone/version.rb +3 -0
  46. data/lib/kintone_rb.rb +7 -0
  47. data/spec/kintone/api/guest_spec.rb +289 -0
  48. data/spec/kintone/api_spec.rb +566 -0
  49. data/spec/kintone/command/apis_spec.rb +179 -0
  50. data/spec/kintone/command/app_acl_spec.rb +43 -0
  51. data/spec/kintone/command/app_spec.rb +54 -0
  52. data/spec/kintone/command/apps_spec.rb +90 -0
  53. data/spec/kintone/command/bulk_request_spec.rb +92 -0
  54. data/spec/kintone/command/field_acl_spec.rb +47 -0
  55. data/spec/kintone/command/file_spec.rb +65 -0
  56. data/spec/kintone/command/form_spec.rb +47 -0
  57. data/spec/kintone/command/guests_spec.rb +107 -0
  58. data/spec/kintone/command/preview_form_spec.rb +30 -0
  59. data/spec/kintone/command/record_acl_spec.rb +48 -0
  60. data/spec/kintone/command/record_spec.rb +210 -0
  61. data/spec/kintone/command/records_spec.rb +463 -0
  62. data/spec/kintone/command/space_body_spec.rb +47 -0
  63. data/spec/kintone/command/space_guests_spec.rb +55 -0
  64. data/spec/kintone/command/space_members_spec.rb +117 -0
  65. data/spec/kintone/command/space_spec.rb +86 -0
  66. data/spec/kintone/command/space_thread_spec.rb +77 -0
  67. data/spec/kintone/command/template_space_spec.rb +59 -0
  68. data/spec/kintone/kintone_error_spec.rb +93 -0
  69. data/spec/kintone/query_spec.rb +506 -0
  70. data/spec/kintone/type/record_spec.rb +38 -0
  71. data/spec/spec_helper.rb +4 -0
  72. metadata +250 -0
@@ -0,0 +1,152 @@
1
+ require 'kintone/query/extension'
2
+
3
+ class Kintone::Query
4
+ def initialize(&block)
5
+ @query = []
6
+ instance_eval(&block) if block_given?
7
+ end
8
+
9
+ def field(code)
10
+ condition = Field.new(code)
11
+ @query << condition
12
+ condition
13
+ end
14
+
15
+ def and!
16
+ @query << 'and'
17
+ end
18
+
19
+ def or!
20
+ @query << 'or'
21
+ end
22
+
23
+ def precede(&block)
24
+ @query << "(#{Kintone::Query.new(&block)})" if block_given?
25
+ end
26
+
27
+ def login_user
28
+ function_string('LOGINUSER()')
29
+ end
30
+
31
+ def primary_organization
32
+ function_string('PRIMARY_ORGANIZATION()')
33
+ end
34
+
35
+ def now
36
+ function_string('NOW()')
37
+ end
38
+
39
+ def today
40
+ function_string('TODAY()')
41
+ end
42
+
43
+ def this_month
44
+ function_string('THIS_MONTH()')
45
+ end
46
+
47
+ def last_month
48
+ function_string('LAST_MONTH()')
49
+ end
50
+
51
+ def this_year
52
+ function_string('THIS_YEAR()')
53
+ end
54
+
55
+ def order_by(field, sort = :asc)
56
+ @query << "order by #{field} #{sort}"
57
+ end
58
+
59
+ def limit(count)
60
+ @query << "limit #{count}"
61
+ end
62
+
63
+ def offset(index)
64
+ @query << "offset #{index}"
65
+ end
66
+
67
+ def to_s
68
+ @query.map(&:to_s).join(' ')
69
+ end
70
+
71
+ def inspect
72
+ to_s
73
+ end
74
+
75
+ alias f field
76
+
77
+ private
78
+
79
+ def function_string(function)
80
+ function.instance_eval do
81
+ class << self
82
+ define_method :query_format, proc { self }
83
+ end
84
+ end
85
+ function
86
+ end
87
+
88
+ class Field
89
+ using Kintone::Query::Extention
90
+
91
+ def initialize(code)
92
+ @code = code.to_s
93
+ end
94
+
95
+ def ==(other)
96
+ save('=', other.query_format)
97
+ end
98
+
99
+ def !=(other)
100
+ save('!=', other.query_format)
101
+ end
102
+
103
+ def >(other)
104
+ save('>', other.query_format)
105
+ end
106
+
107
+ def <(other)
108
+ save('<', other.query_format)
109
+ end
110
+
111
+ def >=(other)
112
+ save('>=', other.query_format)
113
+ end
114
+
115
+ def <=(other)
116
+ save('<=', other.query_format)
117
+ end
118
+
119
+ def in(other)
120
+ other = "(#{other.map { |v| v.query_format }.join(', ')})" if other.is_a?(Array)
121
+ save('in', other)
122
+ end
123
+
124
+ def not_in(other)
125
+ other = "(#{other.map { |v| v.query_format }.join(', ')})" if other.is_a?(Array)
126
+ save('not in', other)
127
+ end
128
+
129
+ def like(other)
130
+ save('like', other.query_format)
131
+ end
132
+
133
+ def not_like(other)
134
+ save('not like', other.query_format)
135
+ end
136
+
137
+ def to_s
138
+ "#{@code} #{@condition} #{@other}"
139
+ end
140
+
141
+ def inspect
142
+ to_s
143
+ end
144
+
145
+ private
146
+
147
+ def save(condition, other)
148
+ @condition ||= condition
149
+ @other ||= other
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,5 @@
1
+ module Enumerable
2
+ def to_kintone
3
+ map(&:to_kintone)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def to_kintone
3
+ map { |k, v| [k, v.to_kintone] }.to_h
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def to_kintone
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module Kintone::Type
2
+ class Record < Hash
3
+ def initialize(default = nil)
4
+ default.each { |k, v| store(k, v) } if default.is_a?(Hash)
5
+ end
6
+
7
+ def to_kintone
8
+ map { |k, v| [k, { value: v }] }.to_h
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ require 'kintone/type/extension/object'
2
+ require 'kintone/type/extension/enumerable'
3
+ require 'kintone/type/extension/hash'
4
+ require 'kintone/type/record'
5
+
6
+ module Kintone::Type; end
@@ -0,0 +1,3 @@
1
+ module Kintone
2
+ VERSION = '1.0.0'.freeze
3
+ end
data/lib/kintone_rb.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'kintone/version'
2
+ require 'kintone/api'
3
+ require 'kintone/type'
4
+
5
+ module Kintone
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,289 @@
1
+ require 'spec_helper'
2
+ require 'kintone/api/guest'
3
+ require 'kintone/api'
4
+
5
+ describe Kintone::Api::Guest do
6
+ let(:target) { Kintone::Api::Guest.new(space, api) }
7
+ let(:space) { 1 }
8
+ let(:api) { Kintone::Api.new('example.cybozu.com', 'Administrator', 'cybozu') }
9
+
10
+ describe '#get_url' do
11
+ subject { target.get_url(command) }
12
+
13
+ let(:command) { 'path' }
14
+
15
+ it { expect(subject).to eq('/k/guest/1/v1/path.json') }
16
+ end
17
+
18
+ describe '#get' do
19
+ before(:each) do
20
+ stub_request(
21
+ :get,
22
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
23
+ )
24
+ .with(
25
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
26
+ body: params.to_json
27
+ )
28
+ .to_return(
29
+ body: '{"abc":"def"}',
30
+ status: 200,
31
+ headers: { 'Content-type' => 'application/json' }
32
+ )
33
+ end
34
+
35
+ subject { target.get(path, params) }
36
+
37
+ let(:path) { '/k/guest/1/v1/path.json' }
38
+ let(:params) { { p1: 'abc', p2: 'def' } }
39
+
40
+ it { expect(subject).to eq 'abc' => 'def' }
41
+
42
+ context 'fail to request' do
43
+ before(:each) do
44
+ stub_request(
45
+ :get,
46
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
47
+ )
48
+ .with(
49
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
50
+ body: params.to_json
51
+ )
52
+ .to_return(
53
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
54
+ status: 500
55
+ )
56
+ end
57
+
58
+ it { expect { subject }.to raise_error Kintone::KintoneError }
59
+ end
60
+ end
61
+
62
+ describe '#post' do
63
+ before(:each) do
64
+ stub_request(
65
+ :post,
66
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
67
+ )
68
+ .with(
69
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
70
+ body: '{"p1":"abc","p2":"def"}'
71
+ )
72
+ .to_return(
73
+ body: '{"abc":"def"}',
74
+ status: 200,
75
+ headers: { 'Content-type' => 'application/json' }
76
+ )
77
+ end
78
+
79
+ subject { target.post(path, body) }
80
+
81
+ let(:path) { '/k/guest/1/v1/path.json' }
82
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
83
+
84
+ it { expect(subject).to eq 'abc' => 'def' }
85
+
86
+ context 'fail to request' do
87
+ before(:each) do
88
+ stub_request(
89
+ :post,
90
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
91
+ )
92
+ .with(
93
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
94
+ body: '{"p1":"abc","p2":"def"}'
95
+ )
96
+ .to_return(
97
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
98
+ status: 500
99
+ )
100
+ end
101
+
102
+ it { expect { subject }.to raise_error Kintone::KintoneError }
103
+ end
104
+ end
105
+
106
+ describe '#put' do
107
+ before(:each) do
108
+ stub_request(
109
+ :put,
110
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
111
+ )
112
+ .with(
113
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
114
+ body: '{"p1":"abc","p2":"def"}'
115
+ )
116
+ .to_return(
117
+ body: '{"abc":"def"}',
118
+ status: 200,
119
+ headers: { 'Content-type' => 'application/json' }
120
+ )
121
+ end
122
+
123
+ subject { target.put(path, body) }
124
+
125
+ let(:path) { '/k/guest/1/v1/path.json' }
126
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
127
+
128
+ it { expect(subject).to eq 'abc' => 'def' }
129
+
130
+ context 'fail to request' do
131
+ before(:each) do
132
+ stub_request(
133
+ :put,
134
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
135
+ )
136
+ .with(
137
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
138
+ body: '{"p1":"abc","p2":"def"}'
139
+ )
140
+ .to_return(
141
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
142
+ status: 500
143
+ )
144
+ end
145
+
146
+ it { expect { subject }.to raise_error Kintone::KintoneError }
147
+ end
148
+ end
149
+
150
+ describe '#delete' do
151
+ before(:each) do
152
+ stub_request(
153
+ :delete,
154
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
155
+ )
156
+ .with(
157
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
158
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
159
+ )
160
+ .to_return(
161
+ body: '{"abc":"def"}',
162
+ status: 200,
163
+ headers: { 'Content-type' => 'application/json' }
164
+ )
165
+ end
166
+
167
+ subject { target.delete(path, params) }
168
+
169
+ let(:path) { '/k/guest/1/v1/path.json' }
170
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
171
+
172
+ it { expect(subject).to eq 'abc' => 'def' }
173
+
174
+ context 'fail to request' do
175
+ before(:each) do
176
+ stub_request(
177
+ :delete,
178
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
179
+ )
180
+ .with(
181
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
182
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
183
+ )
184
+ .to_return(
185
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
186
+ status: 500
187
+ )
188
+ end
189
+
190
+ it { expect { subject }.to raise_error Kintone::KintoneError }
191
+ end
192
+ end
193
+
194
+ describe '#record' do
195
+ subject { target.record }
196
+
197
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Record) }
198
+ end
199
+
200
+ describe '#records' do
201
+ subject { target.records }
202
+
203
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Records) }
204
+ end
205
+
206
+ describe '#form' do
207
+ subject { target.form }
208
+
209
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Form) }
210
+ end
211
+
212
+ describe '#app_acl' do
213
+ subject { target.app_acl }
214
+
215
+ it { expect(subject).to be_a_kind_of(Kintone::Command::AppAcl) }
216
+ end
217
+
218
+ describe '#record_acl' do
219
+ subject { target.record_acl }
220
+
221
+ it { expect(subject).to be_a_kind_of(Kintone::Command::RecordAcl) }
222
+ end
223
+
224
+ describe '#field_acl' do
225
+ subject { target.field_acl }
226
+
227
+ it { expect(subject).to be_a_kind_of(Kintone::Command::FieldAcl) }
228
+ end
229
+
230
+ describe '#space' do
231
+ subject { target.space }
232
+
233
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Space) }
234
+ end
235
+
236
+ describe '#space_body' do
237
+ subject { target.space_body }
238
+
239
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceBody) }
240
+ end
241
+
242
+ describe '#space_thread' do
243
+ subject { target.space_thread }
244
+
245
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceThread) }
246
+ end
247
+
248
+ describe '#space_members' do
249
+ subject { target.space_members }
250
+
251
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceMembers) }
252
+ end
253
+
254
+ describe '#space_guests' do
255
+ subject { target.space_guests }
256
+
257
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceGuests) }
258
+ end
259
+
260
+ describe '#app' do
261
+ subject { target.app }
262
+
263
+ it { is_expected.to be_a_kind_of(Kintone::Command::App) }
264
+ end
265
+
266
+ describe '#apps' do
267
+ subject { target.apps }
268
+
269
+ it { is_expected.to be_a_kind_of(Kintone::Command::Apps) }
270
+ end
271
+
272
+ describe '#bulk_request' do
273
+ subject { target.bulk_request }
274
+
275
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
276
+ end
277
+
278
+ describe '#bulk' do
279
+ subject { target.bulk }
280
+
281
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
282
+ end
283
+
284
+ describe '#preview_form' do
285
+ subject { target.preview_form }
286
+
287
+ it { is_expected.to be_a_kind_of(Kintone::Command::PreviewForm) }
288
+ end
289
+ end