kintone_rb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,121 @@
1
+ require 'faraday'
2
+ require 'faraday/multipart'
3
+ require 'base64'
4
+ require 'json'
5
+ require 'kintone/command/accessor'
6
+ require 'kintone/api/guest'
7
+ require 'kintone/query'
8
+ require 'kintone/kintone_error'
9
+
10
+ class Kintone::Api
11
+ BASE_PATH = '/k/v1/'.freeze
12
+ COMMAND = '%s.json'.freeze
13
+ ACCESSIBLE_COMMAND = [
14
+ :record, :records, :form, :app_acl, :record_acl,
15
+ :field_acl, :template_space, :space, :space_body, :space_thread,
16
+ :space_members, :guests, :app, :apps, :apis,
17
+ :bulk_request, :bulk, :file, :preview_form
18
+ ].freeze
19
+
20
+ def initialize(domain, user, password = nil)
21
+ @connection =
22
+ Faraday.new(url: "https://#{domain}", headers: build_headers(user, password)) do |builder|
23
+ builder.request :url_encoded
24
+ builder.request :multipart
25
+ builder.response :json, content_type: /\bjson$/
26
+ builder.adapter :net_http
27
+ end
28
+
29
+ yield(@connection) if block_given?
30
+ end
31
+
32
+ def get_url(command)
33
+ BASE_PATH + (COMMAND % command)
34
+ end
35
+
36
+ def guest(space_id)
37
+ Kintone::Api::Guest.new(space_id, self)
38
+ end
39
+
40
+ def get(url, params = {})
41
+ response =
42
+ @connection.get do |request|
43
+ request.url url
44
+ # NOTE: Request URI Too Large 対策
45
+ request.headers['Content-Type'] = 'application/json'
46
+ request.body = params.to_h.to_json
47
+ end
48
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
49
+ response.body
50
+ end
51
+
52
+ def post(url, body)
53
+ response =
54
+ @connection.post do |request|
55
+ request.url url
56
+ request.headers['Content-Type'] = 'application/json'
57
+ request.body = body.to_json
58
+ end
59
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
60
+ response.body
61
+ end
62
+
63
+ def put(url, body)
64
+ response =
65
+ @connection.put do |request|
66
+ request.url url
67
+ request.headers['Content-Type'] = 'application/json'
68
+ request.body = body.to_json
69
+ end
70
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
71
+ response.body
72
+ end
73
+
74
+ def delete(url, body = nil)
75
+ response =
76
+ @connection.delete do |request|
77
+ request.url url
78
+ request.headers['Content-Type'] = 'application/json'
79
+ request.body = body.to_json
80
+ end
81
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
82
+ response.body
83
+ end
84
+
85
+ def post_file(url, path, content_type, original_filename)
86
+ response =
87
+ @connection.post do |request|
88
+ request.url url
89
+ request.headers['Content-Type'] = 'multipart/form-data'
90
+ request.body = { file: Faraday::UploadIO.new(path, content_type, original_filename) }
91
+ end
92
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
93
+ response.body['fileKey']
94
+ end
95
+
96
+ def method_missing(name, *args)
97
+ if ACCESSIBLE_COMMAND.include?(name)
98
+ CommandAccessor.send(name, self)
99
+ else
100
+ super
101
+ end
102
+ end
103
+
104
+ def respond_to_missing?(name, *args)
105
+ ACCESSIBLE_COMMAND.include?(name) || super
106
+ end
107
+
108
+ class CommandAccessor
109
+ extend Kintone::Command::Accessor
110
+ end
111
+
112
+ private
113
+
114
+ def build_headers(user, password)
115
+ if password # パスワード認証
116
+ { 'X-Cybozu-Authorization' => Base64.strict_encode64("#{user}:#{password}") }
117
+ else # APIトークン認証
118
+ { 'X-Cybozu-API-Token' => user }
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,109 @@
1
+ class Kintone::Command
2
+ # common
3
+ autoload :Record, 'kintone/command/record'
4
+ autoload :Records, 'kintone/command/records'
5
+ autoload :BulkRequest, 'kintone/command/bulk_request'
6
+ autoload :Form, 'kintone/command/form'
7
+ autoload :PreviewForm, 'kintone/command/preview_form'
8
+ autoload :App, 'kintone/command/app'
9
+ autoload :Apps, 'kintone/command/apps'
10
+ autoload :AppAcl, 'kintone/command/app_acl'
11
+ autoload :RecordAcl, 'kintone/command/record_acl'
12
+ autoload :FieldAcl, 'kintone/command/field_acl'
13
+ autoload :Space, 'kintone/command/space'
14
+ autoload :SpaceBody, 'kintone/command/space_body'
15
+ autoload :SpaceThread, 'kintone/command/space_thread'
16
+ autoload :SpaceMembers, 'kintone/command/space_members'
17
+ autoload :File, 'kintone/command/file'
18
+
19
+ # other than guest
20
+ autoload :TemplateSpace, 'kintone/command/template_space'
21
+ autoload :Guests, 'kintone/command/guests'
22
+ autoload :Apis, 'kintone/command/apis'
23
+
24
+ # guest only
25
+ autoload :SpaceGuests, 'kintone/command/space_guests'
26
+
27
+ module Accessor
28
+ # common
29
+ def record(api)
30
+ Record.new(api)
31
+ end
32
+
33
+ def records(api)
34
+ Records.new(api)
35
+ end
36
+
37
+ def bulk_request(api)
38
+ BulkRequest.new(api)
39
+ end
40
+
41
+ def form(api)
42
+ Form.new(api)
43
+ end
44
+
45
+ def preview_form(api)
46
+ PreviewForm.new(api)
47
+ end
48
+
49
+ def app(api)
50
+ App.new(api)
51
+ end
52
+
53
+ def apps(api)
54
+ Apps.new(api)
55
+ end
56
+
57
+ def app_acl(api)
58
+ AppAcl.new(api)
59
+ end
60
+
61
+ def record_acl(api)
62
+ RecordAcl.new(api)
63
+ end
64
+
65
+ def field_acl(api)
66
+ FieldAcl.new(api)
67
+ end
68
+
69
+ def space(api)
70
+ Space.new(api)
71
+ end
72
+
73
+ def space_body(api)
74
+ SpaceBody.new(api)
75
+ end
76
+
77
+ def space_thread(api)
78
+ SpaceThread.new(api)
79
+ end
80
+
81
+ def space_members(api)
82
+ SpaceMembers.new(api)
83
+ end
84
+
85
+ def file(api)
86
+ File.new(api)
87
+ end
88
+
89
+ # other than guest
90
+ def template_space(api)
91
+ TemplateSpace.new(api)
92
+ end
93
+
94
+ def guests(api)
95
+ Guests.new(api)
96
+ end
97
+
98
+ def apis(api)
99
+ Apis.new(api)
100
+ end
101
+
102
+ # guest only
103
+ def space_guests(api)
104
+ SpaceGuests.new(api)
105
+ end
106
+
107
+ alias bulk bulk_request
108
+ end
109
+ end
@@ -0,0 +1,22 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Apis < Kintone::Command
4
+ def self.path
5
+ 'apis'
6
+ end
7
+
8
+ def get
9
+ @api.get(@url)
10
+ end
11
+
12
+ def get_details_of(link)
13
+ url = Kintone::Api::BASE_PATH + link
14
+ @api.get(url)
15
+ end
16
+
17
+ def get_details_of_key(key)
18
+ response = get
19
+ link = response['apis'][key]['link']
20
+ get_details_of(link)
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::App < Kintone::Command
4
+ def self.path
5
+ 'app'
6
+ end
7
+
8
+ def get(id)
9
+ @api.get(@url, id: id)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::AppAcl < Kintone::Command
4
+ def self.path
5
+ 'app/acl'
6
+ end
7
+
8
+ def update(app, rights)
9
+ @api.put(@url, app: app, rights: rights)
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Apps < Kintone::Command
4
+ def self.path
5
+ 'apps'
6
+ end
7
+
8
+ def get(params = {})
9
+ response = @api.get(@url, params)
10
+ response['apps']
11
+ end
12
+ end
@@ -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
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::FieldAcl < Kintone::Command
4
+ def self.path
5
+ 'field/acl'
6
+ end
7
+
8
+ def update(id, rights)
9
+ @api.put(@url, id: id, rights: rights)
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::File < Kintone::Command
4
+ def self.path
5
+ 'file'
6
+ end
7
+
8
+ def get(file_key)
9
+ @api.get(@url, fileKey: file_key)
10
+ end
11
+
12
+ def register(path, content_type, original_filename)
13
+ @api.post_file(@url, path, content_type, original_filename)
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Form < Kintone::Command
4
+ def self.path
5
+ 'form'
6
+ end
7
+
8
+ def get(app)
9
+ @api.get(@url, app: app)
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Guests < Kintone::Command
4
+ def self.path
5
+ 'guests'
6
+ end
7
+
8
+ def register(guests)
9
+ @api.post(@url, guests: guests)
10
+ end
11
+
12
+ def delete(guests)
13
+ @api.delete(@url, guests: guests)
14
+ end
15
+
16
+ alias create register
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::PreviewForm < Kintone::Command
4
+ def self.path
5
+ 'preview/form'
6
+ end
7
+
8
+ def get(app)
9
+ @api.get(@url, app: app)
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Record < Kintone::Command
4
+ def self.path
5
+ 'record'
6
+ end
7
+
8
+ def get(app, id)
9
+ @api.get(@url, app: app, id: id)
10
+ end
11
+
12
+ def register(app, record)
13
+ @api.post(@url, app: app, record: record.to_kintone)
14
+ end
15
+
16
+ def update(app, id, record, revision: nil)
17
+ body = { app: app, id: id, record: record.to_kintone }
18
+ body[:revision] = revision if revision
19
+ @api.put(@url, body)
20
+ end
21
+
22
+ alias create register
23
+ end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::RecordAcl < Kintone::Command
4
+ def self.path
5
+ 'record/acl'
6
+ end
7
+
8
+ def update(id, rights)
9
+ @api.put(@url, id: id, rights: rights)
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Records < Kintone::Command
4
+ def self.path
5
+ 'records'
6
+ end
7
+
8
+ def get(app, query, fields, total_count: false)
9
+ params = { app: app, query: query.to_s, totalCount: total_count }
10
+ params[:fields] = fields unless fields.nil?
11
+ @api.get(@url, params)
12
+ end
13
+
14
+ def register(app, records)
15
+ @api.post(@url, app: app, records: records.to_kintone)
16
+ end
17
+
18
+ def update(app, records)
19
+ @api.put(@url, app: app, records: records.to_kintone)
20
+ end
21
+
22
+ def delete(app, ids, revisions: nil)
23
+ params = { app: app, ids: ids }
24
+ params[:revisions] = revisions if revisions
25
+ @api.delete(@url, params)
26
+ end
27
+
28
+ alias create register
29
+ end
@@ -0,0 +1,15 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::Space < Kintone::Command
4
+ def self.path
5
+ 'space'
6
+ end
7
+
8
+ def get(id)
9
+ @api.get(@url, id: id)
10
+ end
11
+
12
+ def delete(id)
13
+ @api.delete(@url, id: id)
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::SpaceBody < Kintone::Command
4
+ def self.path
5
+ 'space/body'
6
+ end
7
+
8
+ def update(id, body)
9
+ @api.put(@url, id: id, body: body)
10
+ end
11
+ 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,12 @@
1
+ require 'kintone/command'
2
+
3
+ class Kintone::Command::TemplateSpace < Kintone::Command
4
+ def self.path
5
+ 'template/space'
6
+ end
7
+
8
+ def create(id, name, members, is_guest: false, fixed_member: false)
9
+ body = { id: id, name: name, members: members, isGuest: is_guest, fixedMember: fixed_member }
10
+ @api.post(@url, body)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require 'kintone/api'
2
+
3
+ class Kintone::Command
4
+ def self.path
5
+ '/'
6
+ end
7
+
8
+ def initialize(api)
9
+ @api = api
10
+ @url = @api.get_url(self.class.path)
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class Kintone::KintoneError < StandardError
2
+ attr_reader :message_text, :id, :code, :http_status, :errors
3
+
4
+ def initialize(messages, http_status)
5
+ @message_text = messages['message']
6
+ @id = messages['id']
7
+ @code = messages['code']
8
+ @errors = messages['errors']
9
+ @http_status = http_status
10
+ super(format('%s [%s] %s(%s)', @http_status, @code, @message_text, @id))
11
+ end
12
+ 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