kintone 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +10 -0
  3. data/.travis.yml +2 -1
  4. data/README.md +166 -6
  5. data/kintone.gemspec +4 -2
  6. data/lib/kintone.rb +1 -0
  7. data/lib/kintone/api.rb +30 -3
  8. data/lib/kintone/api/guest.rb +17 -0
  9. data/lib/kintone/command/apis.rb +22 -0
  10. data/lib/kintone/command/app.rb +11 -0
  11. data/lib/kintone/command/guests.rb +15 -0
  12. data/lib/kintone/command/record.rb +6 -2
  13. data/lib/kintone/command/records.rb +9 -5
  14. data/lib/kintone/command/space_guests.rb +11 -0
  15. data/lib/kintone/command/space_members.rb +16 -0
  16. data/lib/kintone/command/space_thread.rb +14 -0
  17. data/lib/kintone/query.rb +146 -0
  18. data/lib/kintone/query/extension.rb +23 -0
  19. data/lib/kintone/type.rb +6 -0
  20. data/lib/kintone/type/extension/enumerable.rb +5 -0
  21. data/lib/kintone/type/extension/hash.rb +5 -0
  22. data/lib/kintone/type/extension/object.rb +5 -0
  23. data/lib/kintone/type/record.rb +11 -0
  24. data/lib/kintone/version.rb +1 -1
  25. data/spec/kintone/api/guest_spec.rb +41 -2
  26. data/spec/kintone/api_spec.rb +83 -20
  27. data/spec/kintone/command/apis_spec.rb +113 -0
  28. data/spec/kintone/command/app_spec.rb +33 -0
  29. data/spec/kintone/command/guests_spec.rb +65 -0
  30. data/spec/kintone/command/record_spec.rb +55 -37
  31. data/spec/kintone/command/records_spec.rb +50 -18
  32. data/spec/kintone/command/space_guests_spec.rb +34 -0
  33. data/spec/kintone/command/space_members_spec.rb +80 -0
  34. data/spec/kintone/command/space_spec.rb +1 -1
  35. data/spec/kintone/command/space_thread_spec.rb +52 -0
  36. data/spec/kintone/query_spec.rb +294 -0
  37. data/spec/kintone/type/record_spec.rb +38 -0
  38. data/spec/spec_helper.rb +1 -0
  39. metadata +67 -10
@@ -6,22 +6,26 @@ class Kintone::Command::Records < Kintone::Command
6
6
  end
7
7
 
8
8
  def get(app, query, fields)
9
- params = { app: app, query: query }
9
+ params = { app: app, query: query.to_s }
10
10
  fields.each_with_index { |v, i| params["fields[#{i}]"] = v }
11
11
  @api.get(@url, params)
12
12
  end
13
13
 
14
+ def register(app, records)
15
+ @api.post(@url, app: app, records: records.to_kintone)
16
+ end
17
+
14
18
  def create(app, records)
15
- @api.post(@url, app: app, records: records)
19
+ register(app, records)
16
20
  end
17
21
 
18
22
  def update(app, records)
19
- @api.put(@url, app: app, records: records)
23
+ @api.put(@url, app: app, records: records.to_kintone)
20
24
  end
21
25
 
22
26
  def delete(app, ids)
23
- params = { app: app }
24
- ids.each_with_index { |v, i| params["ids[#{i}]"] = v }
27
+ params = { app: app, ids: ids }
28
+ # ids.each_with_index { |v, i| params["ids[#{i}]"] = v }
25
29
  @api.delete(@url, params)
26
30
  end
27
31
  end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::SpaceGuests < Kintone::Command
4
+ def self.path
5
+ 'space/guests'
6
+ end
7
+
8
+ def update(id, guests)
9
+ @api.put(@url, id: id, guests: guests)
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::SpaceMembers < Kintone::Command
4
+ def self.path
5
+ 'space/members'
6
+ end
7
+
8
+ def get(id)
9
+ response = @api.get(@url, id: id)
10
+ response['members']
11
+ end
12
+
13
+ def update(id, members)
14
+ @api.put(@url, id: id, members: members)
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::SpaceThread < Kintone::Command
4
+ def self.path
5
+ 'space/thread'
6
+ end
7
+
8
+ def update(id, name: nil, body: nil)
9
+ request_body = { id: id }
10
+ request_body[:name] = name if name
11
+ request_body[:body] = body if body
12
+ @api.put(@url, request_body)
13
+ end
14
+ end
@@ -0,0 +1,146 @@
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 now
32
+ function_string('NOW()')
33
+ end
34
+
35
+ def today
36
+ function_string('TODAY()')
37
+ end
38
+
39
+ def this_month
40
+ function_string('THIS_MONTH()')
41
+ end
42
+
43
+ def last_month
44
+ function_string('LAST_MONTH()')
45
+ end
46
+
47
+ def this_year
48
+ function_string('THIS_YEAR()')
49
+ end
50
+
51
+ def order_by(field, sort = :asc)
52
+ @query << "order by #{field} #{sort}"
53
+ end
54
+
55
+ def limit(count)
56
+ @query << "limit #{count}"
57
+ end
58
+
59
+ def offset(index)
60
+ @query << "offset #{index}"
61
+ end
62
+
63
+ def to_s
64
+ @query.map(&:to_s).join(' ')
65
+ end
66
+
67
+ def inspect
68
+ to_s
69
+ end
70
+
71
+ private
72
+
73
+ def function_string(function)
74
+ function.instance_eval do
75
+ class << self
76
+ define_method :query_format, proc { self }
77
+ end
78
+ end
79
+ function
80
+ end
81
+
82
+ class Field
83
+ using Kintone::Query::Extention
84
+
85
+ def initialize(code)
86
+ @code = code.to_s
87
+ end
88
+
89
+ def ==(other)
90
+ save('=', other.query_format)
91
+ end
92
+
93
+ def !=(other)
94
+ save('!=', other.query_format)
95
+ end
96
+
97
+ def >(other)
98
+ save('>', other.query_format)
99
+ end
100
+
101
+ def <(other)
102
+ save('<', other.query_format)
103
+ end
104
+
105
+ def >=(other)
106
+ save('>=', other.query_format)
107
+ end
108
+
109
+ def <=(other)
110
+ save('<=', other.query_format)
111
+ end
112
+
113
+ def in(other)
114
+ other = "(#{other.map { |v| v.query_format }.join(', ')})" if other.is_a?(Array)
115
+ save('in', other)
116
+ end
117
+
118
+ def not_in(other)
119
+ other = "(#{other.map { |v| v.query_format }.join(', ')})" if other.is_a?(Array)
120
+ save('not in', other)
121
+ end
122
+
123
+ def like(other)
124
+ save('like', other.query_format)
125
+ end
126
+
127
+ def not_like(other)
128
+ save('not like', other.query_format)
129
+ end
130
+
131
+ def to_s
132
+ "#{@code} #{@condition} #{@other}"
133
+ end
134
+
135
+ def inspect
136
+ to_s
137
+ end
138
+
139
+ private
140
+
141
+ def save(condition, other)
142
+ @condition ||= condition
143
+ @other ||= other
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,23 @@
1
+ class Kintone::Query
2
+ module Extention
3
+ refine Object do
4
+ def query_format
5
+ self
6
+ end
7
+ end
8
+
9
+ refine Symbol do
10
+ def query_format
11
+ "\"#{self}\""
12
+ end
13
+ end
14
+
15
+ refine String do
16
+ def query_format
17
+ if self =~ /\A".+"\z/ then self
18
+ else "\"#{self}\""
19
+ end
20
+ end
21
+ end
22
+ end
23
+ 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,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
@@ -1,3 +1,3 @@
1
1
  module Kintone
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -82,9 +82,12 @@ describe Kintone::Api::Guest do
82
82
  before(:each) do
83
83
  stub_request(
84
84
  :delete,
85
- 'https://example.cybozu.com/k/guest/1/v1/path.json?p1=abc&p2=def'
85
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
86
86
  )
87
- .with(headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' })
87
+ .with(
88
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
89
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
90
+ )
88
91
  .to_return(body: "{\"abc\":\"def\"}", status: 200)
89
92
  end
90
93
 
@@ -131,4 +134,40 @@ describe Kintone::Api::Guest do
131
134
 
132
135
  it { expect(subject).to be_a_kind_of(Kintone::Command::FieldAcl) }
133
136
  end
137
+
138
+ describe '#space' do
139
+ subject { target.space }
140
+
141
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Space) }
142
+ end
143
+
144
+ describe '#space_body' do
145
+ subject { target.space_body }
146
+
147
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceBody) }
148
+ end
149
+
150
+ describe '#space_thread' do
151
+ subject { target.space_thread }
152
+
153
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceThread) }
154
+ end
155
+
156
+ describe '#space_members' do
157
+ subject { target.space_members }
158
+
159
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceMembers) }
160
+ end
161
+
162
+ describe '#space_guests' do
163
+ subject { target.space_guests }
164
+
165
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceGuests) }
166
+ end
167
+
168
+ describe '#app' do
169
+ subject { target.app }
170
+
171
+ it { is_expected.to be_a_kind_of(Kintone::Command::App) }
172
+ end
134
173
  end
@@ -15,7 +15,7 @@ describe Kintone::Api do
15
15
 
16
16
  context '' do
17
17
  let(:command) { 'path' }
18
- it { expect(subject).to eq('/k/v1/path.json') }
18
+ it { is_expected.to eq('/k/v1/path.json') }
19
19
  end
20
20
  end
21
21
 
@@ -25,7 +25,7 @@ describe Kintone::Api do
25
25
  context '引数が数値の1の時' do
26
26
  let(:space) { 1 }
27
27
 
28
- it { expect(subject).to be_a_kind_of(Kintone::Api::Guest) }
28
+ it { is_expected.to be_a_kind_of(Kintone::Api::Guest) }
29
29
  it { expect(subject.instance_variable_get(:@guest_path)).to eq('/k/guest/1/v1/') }
30
30
  end
31
31
 
@@ -44,17 +44,47 @@ describe Kintone::Api do
44
44
  before(:each) do
45
45
  stub_request(
46
46
  :get,
47
- 'https://www.example.com/k/v1/path?p1=abc&p2=def'
47
+ 'https://www.example.com/k/v1/path'
48
48
  )
49
- .with(headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' })
49
+ .with(
50
+ query: query,
51
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
52
+ )
50
53
  .to_return(body: "{\"abc\":\"def\"}", status: 200)
51
54
  end
52
55
 
53
56
  subject { target.get(path, params) }
57
+
54
58
  let(:path) { '/k/v1/path' }
55
- let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
56
59
 
57
- it { expect(subject).to eq 'abc' => 'def' }
60
+ context 'with some params' do
61
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
62
+ let(:query) { 'p1=abc&p2=def' }
63
+
64
+ it { is_expected.to eq 'abc' => 'def' }
65
+ end
66
+
67
+ context 'with empty params' do
68
+ let(:params) { {} }
69
+ let(:query) { nil }
70
+
71
+ it { is_expected.to eq 'abc' => 'def' }
72
+ end
73
+
74
+ context 'with nil' do
75
+ let(:params) { nil }
76
+ let(:query) { nil }
77
+
78
+ it { expect { subject }.to raise_error NoMethodError }
79
+ end
80
+
81
+ context 'with no params' do
82
+ subject { target.get(path) }
83
+
84
+ let(:query) { nil }
85
+
86
+ it { is_expected.to eq 'abc' => 'def' }
87
+ end
58
88
  end
59
89
 
60
90
  describe '#post' do
@@ -77,7 +107,7 @@ describe Kintone::Api do
77
107
  let(:path) { '/k/v1/path' }
78
108
  let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
79
109
 
80
- it { expect(subject).to eq 'abc' => 'def' }
110
+ it { is_expected.to eq 'abc' => 'def' }
81
111
  end
82
112
 
83
113
  describe '#put' do
@@ -100,16 +130,19 @@ describe Kintone::Api do
100
130
  let(:path) { '/k/v1/path' }
101
131
  let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
102
132
 
103
- it { expect(subject).to eq 'abc' => 'def' }
133
+ it { is_expected.to eq 'abc' => 'def' }
104
134
  end
105
135
 
106
136
  describe '#delete' do
107
137
  before(:each) do
108
138
  stub_request(
109
139
  :delete,
110
- 'https://www.example.com/k/v1/path?p1=abc&p2=def'
140
+ 'https://www.example.com/k/v1/path'
111
141
  )
112
- .with(headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' })
142
+ .with(
143
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
144
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
145
+ )
113
146
  .to_return(body: "{\"abc\":\"def\"}", status: 200)
114
147
  end
115
148
 
@@ -117,60 +150,90 @@ describe Kintone::Api do
117
150
  let(:path) { '/k/v1/path' }
118
151
  let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
119
152
 
120
- it { expect(subject).to eq 'abc' => 'def' }
153
+ it { is_expected.to eq 'abc' => 'def' }
121
154
  end
122
155
 
123
156
  describe '#record' do
124
157
  subject { target.record }
125
158
 
126
- it { expect(subject).to be_a_kind_of(Kintone::Command::Record) }
159
+ it { is_expected.to be_a_kind_of(Kintone::Command::Record) }
127
160
  end
128
161
 
129
162
  describe '#records' do
130
163
  subject { target.records }
131
164
 
132
- it { expect(subject).to be_a_kind_of(Kintone::Command::Records) }
165
+ it { is_expected.to be_a_kind_of(Kintone::Command::Records) }
133
166
  end
134
167
 
135
168
  describe '#form' do
136
169
  subject { target.form }
137
170
 
138
- it { expect(subject).to be_a_kind_of(Kintone::Command::Form) }
171
+ it { is_expected.to be_a_kind_of(Kintone::Command::Form) }
139
172
  end
140
173
 
141
174
  describe '#app_acl' do
142
175
  subject { target.app_acl }
143
176
 
144
- it { expect(subject).to be_a_kind_of(Kintone::Command::AppAcl) }
177
+ it { is_expected.to be_a_kind_of(Kintone::Command::AppAcl) }
145
178
  end
146
179
 
147
180
  describe '#record_acl' do
148
181
  subject { target.record_acl }
149
182
 
150
- it { expect(subject).to be_a_kind_of(Kintone::Command::RecordAcl) }
183
+ it { is_expected.to be_a_kind_of(Kintone::Command::RecordAcl) }
151
184
  end
152
185
 
153
186
  describe '#field_acl' do
154
187
  subject { target.field_acl }
155
188
 
156
- it { expect(subject).to be_a_kind_of(Kintone::Command::FieldAcl) }
189
+ it { is_expected.to be_a_kind_of(Kintone::Command::FieldAcl) }
157
190
  end
158
191
 
159
192
  describe '#template_space' do
160
193
  subject { target.template_space }
161
194
 
162
- it { expect(subject).to be_a_kind_of(Kintone::Command::TemplateSpace) }
195
+ it { is_expected.to be_a_kind_of(Kintone::Command::TemplateSpace) }
163
196
  end
164
197
 
165
198
  describe '#space' do
166
199
  subject { target.space }
167
200
 
168
- it { expect(subject).to be_a_kind_of(Kintone::Command::Space) }
201
+ it { is_expected.to be_a_kind_of(Kintone::Command::Space) }
169
202
  end
170
203
 
171
204
  describe '#space_body' do
172
205
  subject { target.space_body }
173
206
 
174
- it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceBody) }
207
+ it { is_expected.to be_a_kind_of(Kintone::Command::SpaceBody) }
208
+ end
209
+
210
+ describe '#space_thread' do
211
+ subject { target.space_thread }
212
+
213
+ it { is_expected.to be_a_kind_of(Kintone::Command::SpaceThread) }
214
+ end
215
+
216
+ describe '#space_members' do
217
+ subject { target.space_members }
218
+
219
+ it { is_expected.to be_a_kind_of(Kintone::Command::SpaceMembers) }
220
+ end
221
+
222
+ describe '#guests' do
223
+ subject { target.guests }
224
+
225
+ it { is_expected.to be_a_kind_of(Kintone::Command::Guests) }
226
+ end
227
+
228
+ describe '#app' do
229
+ subject { target.app }
230
+
231
+ it { is_expected.to be_a_kind_of(Kintone::Command::App) }
232
+ end
233
+
234
+ describe '#apis' do
235
+ subject { target.apis }
236
+
237
+ it { is_expected.to be_a_kind_of(Kintone::Command::Apis) }
175
238
  end
176
239
  end