kintone-oauth-extension 0.2.1

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 (75) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +37 -0
  3. data/.github/workflows/test.yml +21 -0
  4. data/.gitignore +21 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +47 -0
  7. data/CHANGELOG.md +13 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +374 -0
  12. data/bin/console +4 -0
  13. data/bin/setup +6 -0
  14. data/kintone.gemspec +38 -0
  15. data/lib/kintone.rb +8 -0
  16. data/lib/kintone/api.rb +121 -0
  17. data/lib/kintone/api/guest.rb +44 -0
  18. data/lib/kintone/command.rb +12 -0
  19. data/lib/kintone/command/accessor.rb +109 -0
  20. data/lib/kintone/command/apis.rb +22 -0
  21. data/lib/kintone/command/app.rb +11 -0
  22. data/lib/kintone/command/app_acl.rb +11 -0
  23. data/lib/kintone/command/apps.rb +12 -0
  24. data/lib/kintone/command/bulk_request.rb +12 -0
  25. data/lib/kintone/command/field_acl.rb +11 -0
  26. data/lib/kintone/command/file.rb +15 -0
  27. data/lib/kintone/command/form.rb +11 -0
  28. data/lib/kintone/command/guests.rb +17 -0
  29. data/lib/kintone/command/preview_form.rb +11 -0
  30. data/lib/kintone/command/record.rb +23 -0
  31. data/lib/kintone/command/record_acl.rb +11 -0
  32. data/lib/kintone/command/records.rb +29 -0
  33. data/lib/kintone/command/space.rb +15 -0
  34. data/lib/kintone/command/space_body.rb +11 -0
  35. data/lib/kintone/command/space_guests.rb +11 -0
  36. data/lib/kintone/command/space_members.rb +16 -0
  37. data/lib/kintone/command/space_thread.rb +14 -0
  38. data/lib/kintone/command/template_space.rb +12 -0
  39. data/lib/kintone/kintone_error.rb +12 -0
  40. data/lib/kintone/oauth_api.rb +91 -0
  41. data/lib/kintone/query.rb +152 -0
  42. data/lib/kintone/query/extension.rb +23 -0
  43. data/lib/kintone/type.rb +6 -0
  44. data/lib/kintone/type/extension/enumerable.rb +5 -0
  45. data/lib/kintone/type/extension/hash.rb +5 -0
  46. data/lib/kintone/type/extension/object.rb +5 -0
  47. data/lib/kintone/type/record.rb +11 -0
  48. data/lib/kintone/version.rb +3 -0
  49. data/spec/kintone/api/guest_spec.rb +289 -0
  50. data/spec/kintone/api_spec.rb +566 -0
  51. data/spec/kintone/command/apis_spec.rb +179 -0
  52. data/spec/kintone/command/app_acl_spec.rb +43 -0
  53. data/spec/kintone/command/app_spec.rb +54 -0
  54. data/spec/kintone/command/apps_spec.rb +90 -0
  55. data/spec/kintone/command/bulk_request_spec.rb +92 -0
  56. data/spec/kintone/command/field_acl_spec.rb +47 -0
  57. data/spec/kintone/command/file_spec.rb +65 -0
  58. data/spec/kintone/command/form_spec.rb +47 -0
  59. data/spec/kintone/command/guests_spec.rb +107 -0
  60. data/spec/kintone/command/preview_form_spec.rb +30 -0
  61. data/spec/kintone/command/record_acl_spec.rb +48 -0
  62. data/spec/kintone/command/record_spec.rb +210 -0
  63. data/spec/kintone/command/records_spec.rb +463 -0
  64. data/spec/kintone/command/space_body_spec.rb +47 -0
  65. data/spec/kintone/command/space_guests_spec.rb +55 -0
  66. data/spec/kintone/command/space_members_spec.rb +117 -0
  67. data/spec/kintone/command/space_spec.rb +86 -0
  68. data/spec/kintone/command/space_thread_spec.rb +77 -0
  69. data/spec/kintone/command/template_space_spec.rb +59 -0
  70. data/spec/kintone/kintone_error_spec.rb +93 -0
  71. data/spec/kintone/oauth_api_spec.rb +164 -0
  72. data/spec/kintone/query_spec.rb +506 -0
  73. data/spec/kintone/type/record_spec.rb +38 -0
  74. data/spec/spec_helper.rb +10 -0
  75. metadata +302 -0
@@ -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
+ 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,91 @@
1
+ require 'oauth2'
2
+
3
+ class Kintone::OAuthApi < Kintone::Api
4
+ # @return [OAuth2::AccessToken]
5
+ attr_reader :access_token
6
+
7
+ # @param [String] domain the kintone's domain (e.g. 'foobar.cybozu.com').
8
+ # @param [String] token the Access Token value.
9
+ # @param [Hash] opts the options to create the Access Token with.
10
+ # @option opts [String] :client_id (nil) the client_id value.
11
+ # @option opts [String] :client_secret (nil) the client_secret value.
12
+ # @option opts [String] :refresh_token (nil) the refresh_token value.
13
+ # @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire.
14
+ # @yield [builder] The Faraday connection builder
15
+ def initialize(domain, token, opts = {}, &block)
16
+ client_options = {
17
+ site: "https://#{domain}",
18
+ token_url: '/oauth2/token',
19
+ authorize_url: '/oauth2/authorization',
20
+ connection_build: connection_builder(&block)
21
+ }
22
+ client = ::OAuth2::Client.new(opts[:client_id], opts[:client_secret], client_options)
23
+ @access_token = ::OAuth2::AccessToken.new(client, token, refresh_token: opts[:refresh_token], expires_at: opts[:expires_at])
24
+ end
25
+
26
+ def get(url, params = {})
27
+ opts = request_options(params: params, headers: nil)
28
+ request(:get, url, opts)
29
+ end
30
+
31
+ def post(url, body)
32
+ opts = request_options(body: body)
33
+ request(:post, url, opts)
34
+ end
35
+
36
+ def put(url, body)
37
+ opts = request_options(body: body)
38
+ request(:put, url, opts)
39
+ end
40
+
41
+ def delete(url, body = nil)
42
+ opts = request_options(body: body)
43
+ request(:delete, url, opts)
44
+ end
45
+
46
+ def refresh!
47
+ @access_token = @access_token.refresh!
48
+ end
49
+
50
+ private
51
+
52
+ def connection_builder(&block)
53
+ lambda { |con|
54
+ con.request :url_encoded
55
+ con.request :multipart
56
+ # NOTE: comment out for avoiding following bug at OAuth2 v1.4.4.
57
+ # In 2.x the bug will be fixed.
58
+ # refer to https://github.com/oauth-xx/oauth2/pull/380
59
+ # con.response :json, content_type: /\bjson$/
60
+ block.call(con) if block_given?
61
+ }
62
+ end
63
+
64
+ def request(verb, url, opts)
65
+ response = @access_token.request(verb, url, opts)
66
+ validate_response(response)
67
+ rescue OAuth2::Error => e
68
+ response = e.response
69
+ raise Kintone::KintoneError.new(response.body, response.status)
70
+ end
71
+
72
+ def validate_response(response, expected_status = 200)
73
+ if response.status != expected_status
74
+ raise Kintone::KintoneError.new(response.body, response.status)
75
+ end
76
+
77
+ JSON.parse(response.body)
78
+ end
79
+
80
+ def request_options(params: nil, body: nil, headers: default_headers)
81
+ opts = {}
82
+ opts[:headers] = headers
83
+ opts[:params] = params if params
84
+ opts[:body] = body.to_json if body
85
+ opts
86
+ end
87
+
88
+ def default_headers
89
+ { 'Content-Type' => 'application/json' }
90
+ end
91
+ end
@@ -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