kintone_rb_takumen 1.0.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.
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 +11 -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 +43 -0
  15. data/lib/kintone/api.rb +124 -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 +15 -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 +143 -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,124 @@
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, basic_auth = nil)
21
+ @basic_auth = basic_auth
22
+ @connection =
23
+ Faraday.new(url: "https://#{domain}", headers: build_headers(user, password)) do |builder|
24
+ builder.request :url_encoded
25
+ builder.request :multipart
26
+ builder.response :json, content_type: /\bjson$/
27
+ builder.adapter :net_http
28
+ end
29
+
30
+ yield(@connection) if block_given?
31
+ end
32
+
33
+ def get_url(command)
34
+ BASE_PATH + (COMMAND % command)
35
+ end
36
+
37
+ def guest(space_id)
38
+ Kintone::Api::Guest.new(space_id, self)
39
+ end
40
+
41
+ def get(url, params = {})
42
+ response =
43
+ @connection.get do |request|
44
+ request.url url
45
+ # NOTE: Request URI Too Large 対策
46
+ request.headers['Content-Type'] = 'application/json'
47
+ request.body = params.to_h.to_json
48
+ end
49
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
50
+ response.body
51
+ end
52
+
53
+ def post(url, body)
54
+ response =
55
+ @connection.post do |request|
56
+ request.url url
57
+ request.headers['Content-Type'] = 'application/json'
58
+ request.body = body.to_json
59
+ end
60
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
61
+ response.body
62
+ end
63
+
64
+ def put(url, body)
65
+ response =
66
+ @connection.put do |request|
67
+ request.url url
68
+ request.headers['Content-Type'] = 'application/json'
69
+ request.body = body.to_json
70
+ end
71
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
72
+ response.body
73
+ end
74
+
75
+ def delete(url, body = nil)
76
+ response =
77
+ @connection.delete do |request|
78
+ request.url url
79
+ request.headers['Content-Type'] = 'application/json'
80
+ request.body = body.to_json
81
+ end
82
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
83
+ response.body
84
+ end
85
+
86
+ def post_file(url, path, content_type, original_filename)
87
+ response =
88
+ @connection.post do |request|
89
+ request.url url
90
+ request.headers['Content-Type'] = 'multipart/form-data'
91
+ request.body = { file: Faraday::UploadIO.new(path, content_type, original_filename) }
92
+ end
93
+ raise Kintone::KintoneError.new(response.body, response.status) if response.status != 200
94
+ response.body['fileKey']
95
+ end
96
+
97
+ def method_missing(name, *args)
98
+ if ACCESSIBLE_COMMAND.include?(name)
99
+ CommandAccessor.send(name, self)
100
+ else
101
+ super
102
+ end
103
+ end
104
+
105
+ def respond_to_missing?(name, *args)
106
+ ACCESSIBLE_COMMAND.include?(name) || super
107
+ end
108
+
109
+ class CommandAccessor
110
+ extend Kintone::Command::Accessor
111
+ end
112
+
113
+ private
114
+
115
+ def build_headers(user, password)
116
+ if password # パスワード認証
117
+ { 'X-Cybozu-Authorization' => Base64.strict_encode64("#{user}:#{password}") }
118
+ else # APIトークン認証
119
+ headers = { 'X-Cybozu-API-Token' => user }
120
+ headers['Authorization'] = "Basic #{@basic_auth}" if @basic_auth
121
+ headers
122
+ end
123
+ end
124
+ 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,15 @@
1
+ class Kintone::KintoneError < StandardError
2
+ attr_reader :message_text, :id, :code, :http_status, :errors
3
+
4
+ def initialize(messages, http_status)
5
+ if messages.is_a?(Hash) && messages.has_key?('results')
6
+ messages = messages['results'].find { |message| message.has_key?('message') }
7
+ end
8
+ @message_text = messages['message']
9
+ @id = messages['id']
10
+ @code = messages['code']
11
+ @errors = messages['errors']
12
+ @http_status = http_status
13
+ super(format('%s [%s] %s(%s)', @http_status, @code, @message_text, @id))
14
+ end
15
+ 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