boxr 1.23.1 → 1.24.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61830d438f70d8585c435bf00b719911cba3a4687ce9df4310f7232d50b4bd5b
4
- data.tar.gz: 677ffb4ccd6ea0ef5478e1dd879e202c2c8605e12561a72c865b05689295a4a1
3
+ metadata.gz: cd53bf1526acf4b05dcf83b888767dea4a2f6f3d9bcb9147b9a10778e5f0d859
4
+ data.tar.gz: ceab9581e73753f3f7125c9aaf3b811877c47c1128a17c50afa989966f86e8a3
5
5
  SHA512:
6
- metadata.gz: cf1a95e4d924c7e7c64bfda48f7c33524cb3c4a6a1de8d443044c92b56dd80a6747cfe2229e9cfd5d60ff68f147538d47aa06682cec6e059ef20e69f8ed342e9
7
- data.tar.gz: bbbdcbdeab436de980d16a6d9e3dd4def18068421094b75787991fee3f79bd6b530d7df2e5255171340d838ae2c6df8b7c54e525be5b73b7435c1558987048d1
6
+ metadata.gz: fa545cd50ffcb06f1afc54453b79db053e38e8646badf6a66c9ab8b13658c178b4c44a07ffeb6aec1905931be9e5a75473115f8742741e31877eb00a51137179
7
+ data.tar.gz: ddb3b721b49af57b392923c272b397732a27ed4426c5acaed858a6f0e525c173ea600d271912d265027c70217ae54bb5516d2fcc8c716ba7cfab77cf37c78521
data/.env.example CHANGED
@@ -17,7 +17,5 @@ BOX_CLIENT_SECRET={client secret of your Box app}
17
17
  BOX_ENTERPRISE_ID={box enterprise id}
18
18
  JWT_PRIVATE_KEY_PATH={path to your JWT private key}
19
19
  JWT_PRIVATE_KEY_PASSWORD={JWT private key password}
20
- BOX_PRIMARY_SIGNATURE_KEY={primary key for webhooks}
21
- BOX_SECONDARY_SIGNATURE_KEY={secondary key for webhooks}
22
20
  BOX_OAUTH_CLIENT_ID={client_id for your server authentication app}
23
21
  BOX_OAUTH_CLIENT_SECRET={client_secret for your server authentication app}
data/boxr.gemspec CHANGED
@@ -38,5 +38,5 @@ Gem::Specification.new do |spec|
38
38
  spec.add_runtime_dependency 'addressable', '~> 2.8'
39
39
  spec.add_runtime_dependency 'hashie', '>= 3.5', '< 6'
40
40
  spec.add_runtime_dependency 'httpclient', '~> 2.8'
41
- spec.add_runtime_dependency 'jwt', '>= 1.4', '< 3'
41
+ spec.add_runtime_dependency 'jwt', '>= 1.4', '< 4'
42
42
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boxr
4
+ class Client
5
+ def get_user_avatar(user_id)
6
+ user_id = ensure_id(user_id)
7
+ uri = "#{USERS_URI}/#{user_id}/avatar"
8
+
9
+ avatar_data, = get(uri, process_response: false)
10
+ avatar_data
11
+ end
12
+
13
+ def create_user_avatar(user_id, pic, pic_file_name: nil, pic_content_type: nil)
14
+ user_id = ensure_id(user_id)
15
+ uri = "#{USERS_URI}/#{user_id}/avatar"
16
+
17
+ body = { pic: pic }
18
+ body[:pic_file_name] = pic_file_name unless pic_file_name.nil?
19
+ body[:pic_content_type] = pic_content_type unless pic_content_type.nil?
20
+
21
+ avatar, = post(uri, body, process_body: false, content_type: 'multipart/form-data')
22
+ avatar
23
+ end
24
+
25
+ def delete_user_avatar(user_id)
26
+ user_id = ensure_id(user_id)
27
+ uri = "#{USERS_URI}/#{user_id}/avatar"
28
+
29
+ result, = delete(uri)
30
+ result
31
+ end
32
+ end
33
+ end
@@ -17,7 +17,6 @@ module Boxr
17
17
  body = { folder_id: parent_id, file_size: io.size, file_name: name }
18
18
  session_info, = post(uri, body, content_type: 'application/json',
19
19
  success_codes: [200, 201, 202])
20
-
21
20
  session_info
22
21
  end
23
22
 
@@ -35,14 +34,13 @@ module Boxr
35
34
  body = { file_size: io.size, file_name: name }
36
35
  session_info, = post(uri, body, content_type: 'application/json',
37
36
  success_codes: [200, 201, 202])
38
-
39
37
  session_info
40
38
  end
41
39
 
42
40
  def chunked_upload_get_session(session_id)
41
+ session_id = ensure_path_segment(session_id)
43
42
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}"
44
43
  session_info, = get(uri)
45
-
46
44
  session_info
47
45
  end
48
46
 
@@ -52,163 +50,185 @@ module Boxr
52
50
  end
53
51
  end
54
52
 
55
- def chunked_upload_part_from_io(io, session_id, content_range)
53
+ def chunked_upload_part_from_io(io, session_id, content_range) # rubocop:disable Metrics
54
+ session_id = ensure_path_segment(session_id)
56
55
  io.pos = content_range.min
57
56
  part_size = content_range.max - content_range.min + 1
58
- data = io.read(part_size)
57
+ body = io.read(part_size)
59
58
  io.rewind
60
59
 
61
- digest = "sha=#{Digest::SHA1.base64digest(data)}"
60
+ digest = "sha=#{Digest::SHA1.base64digest(body)}"
62
61
  range = "bytes #{content_range.min}-#{content_range.max}/#{io.size}"
63
62
 
64
63
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}"
65
- body = data
66
- part_info, = put(uri, body, process_body: false, digest: digest,
67
- content_type: 'application/octet-stream', content_range: range, success_codes: [200, 201, 202])
68
-
64
+ part_info, = put(
65
+ uri, body, process_body: false, digest: digest, content_type: 'application/octet-stream',
66
+ content_range: range, success_codes: [200, 201, 202]
67
+ )
69
68
  part_info.part
70
69
  end
71
70
 
72
71
  def chunked_upload_list_parts(session_id, limit: nil, offset: nil)
72
+ session_id = ensure_path_segment(session_id)
73
73
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}/parts"
74
74
  query = {}
75
75
  query[:limit] = limit unless limit.nil?
76
76
  query[:offset] = offset unless offset.nil?
77
77
  parts_info, = get(uri, query: query)
78
-
79
78
  parts_info.entries
80
79
  end
81
80
 
82
- def chunked_upload_commit(path_to_file, session_id, parts, content_created_at: nil,
81
+ def chunked_upload_commit(path_to_file, session_id, parts, content_created_at: nil, # rubocop:disable Metrics
83
82
  content_modified_at: nil, if_match: nil, if_non_match: nil)
84
83
  File.open(path_to_file) do |file|
85
- chunked_upload_commit_from_io(file, session_id, parts,
86
- content_created_at: content_created_at, content_modified_at: content_modified_at, if_match: if_match, if_non_match: if_non_match)
84
+ chunked_upload_commit_from_io(
85
+ file, session_id, parts,
86
+ content_created_at: content_created_at, content_modified_at: content_modified_at,
87
+ if_match: if_match, if_non_match: if_non_match
88
+ )
87
89
  end
88
90
  end
89
91
 
90
- def chunked_upload_commit_from_io(io, session_id, parts, content_created_at: nil,
92
+ def chunked_upload_commit_from_io(io, session_id, parts, content_created_at: nil, # rubocop:disable Metrics
91
93
  content_modified_at: nil, if_match: nil, if_non_match: nil)
94
+ session_id = ensure_path_segment(session_id)
92
95
  io.pos = 0
93
- digest = Digest::SHA1.new
94
- while (buf = io.read(8 * 1024**2)) && buf.size.positive?
95
- digest.update(buf)
96
- end
97
- io.rewind
98
- digest = "sha=#{digest.base64digest}"
96
+ cca = content_created_at
97
+ cma = content_modified_at
98
+ digest = "sha=#{calculate_chunked_upload_digest(io).base64digest}"
99
99
 
100
100
  attributes = {}
101
- unless content_created_at.nil?
102
- attributes[:content_created_at] =
103
- content_created_at.to_datetime.rfc3339
104
- end
105
- unless content_modified_at.nil?
106
- attributes[:content_modified_at] =
107
- content_modified_at.to_datetime.rfc3339
108
- end
101
+ attributes[:content_created_at] = cca.to_datetime.rfc3339 if cca
102
+ attributes[:content_modified_at] = cma.to_datetime.rfc3339 if cma
109
103
 
110
104
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}/commit"
111
- body = {
112
- parts: parts,
113
- attributes: attributes
114
- }
115
- commit_info, = post(uri, body, process_body: true, digest: digest,
116
- content_type: 'application/json', if_match: if_match, if_non_match: if_non_match, success_codes: [200, 201, 202])
105
+ body = { parts: parts, attributes: attributes }
117
106
 
118
- commit_info
107
+ loop do
108
+ commit_info, response = post(
109
+ uri, body,
110
+ process_body: true, digest: digest, content_type: 'application/json',
111
+ if_match: if_match, if_non_match: if_non_match, success_codes: [200, 201, 202]
112
+ )
113
+ return commit_info if response.status != 202
114
+
115
+ sleep response.header['Retry-After'][0].to_i
116
+ end
119
117
  end
120
118
 
121
119
  def chunked_upload_abort_session(session_id)
120
+ session_id = ensure_path_segment(session_id)
122
121
  uri = "#{UPLOAD_URI}/files/upload_sessions/#{session_id}"
123
122
  abort_info, = delete(uri)
124
-
125
123
  abort_info
124
+ rescue BoxrError # Ignore errors from aborting session
125
+ nil
126
126
  end
127
127
 
128
- def chunked_upload_file(path_to_file, parent, name: nil, n_threads: 1, content_created_at: nil,
129
- content_modified_at: nil)
128
+ def chunked_upload_file(path_to_file, parent, name: nil, n_threads: 1, # rubocop:disable Metrics
129
+ content_created_at: nil, content_modified_at: nil)
130
130
  filename = name || File.basename(path_to_file)
131
131
 
132
132
  File.open(path_to_file) do |file|
133
- chunked_upload_file_from_io(file, parent, filename, n_threads: n_threads,
134
- content_created_at: content_created_at, content_modified_at: content_modified_at)
133
+ chunked_upload_file_from_io(
134
+ file, parent, filename,
135
+ n_threads: n_threads, content_created_at: content_created_at,
136
+ content_modified_at: content_modified_at
137
+ )
135
138
  end
136
139
  end
137
140
 
138
- def chunked_upload_file_from_io(io, parent, name, n_threads: 1, content_created_at: nil,
139
- content_modified_at: nil)
140
- session = nil
141
- file_info = nil
142
-
141
+ def chunked_upload_file_from_io(io, parent, name, n_threads: 1, # rubocop:disable Metrics
142
+ content_created_at: nil, content_modified_at: nil)
143
143
  session = chunked_upload_create_session_new_file_from_io(io, parent, name)
144
-
145
- file_info = chunked_upload_to_session_from_io(io, session, n_threads: n_threads,
146
- content_created_at: nil, content_modified_at: nil)
147
- file_info
148
- ensure
149
- chunked_upload_abort_session(session.id) if file_info.nil? && !session.nil?
144
+ chunked_upload_to_session_from_io(
145
+ io, session,
146
+ n_threads: n_threads, content_created_at: content_created_at,
147
+ content_modified_at: content_modified_at
148
+ )
150
149
  end
151
150
 
152
- def chunked_upload_new_version_of_file(path_to_file, file, name: nil, n_threads: 1,
151
+ def chunked_upload_new_version_of_file(path_to_file, file, name: nil, n_threads: 1, # rubocop:disable Metrics
153
152
  content_created_at: nil, content_modified_at: nil)
154
153
  filename = name || File.basename(path_to_file)
155
154
 
156
155
  File.open(path_to_file) do |io|
157
- chunked_upload_new_version_of_file_from_io(io, file, filename, n_threads: n_threads,
158
- content_created_at: content_created_at, content_modified_at: content_modified_at)
156
+ chunked_upload_new_version_of_file_from_io(
157
+ io, file, filename,
158
+ n_threads: n_threads, content_created_at: content_created_at,
159
+ content_modified_at: content_modified_at
160
+ )
159
161
  end
160
162
  end
161
163
 
162
- def chunked_upload_new_version_of_file_from_io(io, file, name, n_threads: 1,
163
- content_created_at: nil, content_modified_at: nil)
164
- session = nil
165
- file_info = nil
166
-
164
+ def chunked_upload_new_version_of_file_from_io( # rubocop:disable Metrics
165
+ io, file, name, n_threads: 1, content_created_at: nil, content_modified_at: nil
166
+ )
167
167
  session = chunked_upload_create_session_new_version_from_io(io, file, name)
168
-
169
- file_info = chunked_upload_to_session_from_io(io, session, n_threads: n_threads,
170
- content_created_at: nil, content_modified_at: nil)
171
- file_info
172
- ensure
173
- chunked_upload_abort_session(session.id) if file_info.nil? && !session.nil?
168
+ chunked_upload_to_session_from_io(
169
+ io, session,
170
+ n_threads: n_threads, content_created_at: content_created_at,
171
+ content_modified_at: content_modified_at
172
+ )
174
173
  end
175
174
 
176
175
  private
177
176
 
178
177
  PARALLEL_GEM_REQUIREMENT = Gem::Requirement.create('~> 1.0').freeze
179
178
 
180
- def chunked_upload_to_session_from_io(io, session, n_threads: 1, content_created_at: nil,
181
- content_modified_at: nil)
179
+ def chunked_upload_to_session_from_io(io, session, n_threads: 1, # rubocop:disable Metrics
180
+ content_created_at: nil, content_modified_at: nil)
182
181
  content_ranges = []
183
- offset = 0
184
- loop do
185
- limit = [offset + session.part_size, io.size].min - 1
186
- content_ranges << (offset..limit)
187
- break if limit == io.size - 1
188
-
189
- offset = limit + 1
182
+ start_offset = 0
183
+ part_size = session.part_size
184
+ file_size = io.size
185
+ commit_info = nil
186
+
187
+ while start_offset < file_size
188
+ end_offset = [start_offset + part_size, file_size].min - 1
189
+ content_ranges << (start_offset..end_offset)
190
+ start_offset += part_size
190
191
  end
191
192
 
192
193
  parts =
193
194
  if n_threads > 1
194
- unless gem_parallel_available?
195
- raise BoxrError.new(boxr_message: "parallel chunked uploads requires gem parallel (#{PARALLEL_GEM_REQUIREMENT}) to be loaded")
196
- end
197
-
198
- Parallel.map(content_ranges, in_threads: n_threads) do |content_range|
199
- File.open(io.path) do |io_dup|
200
- chunked_upload_part_from_io(io_dup, session.id, content_range)
201
- end
202
- end
195
+ threaded_chunked_upload_part_from_io(io, session.id, content_ranges, n_threads)
203
196
  else
197
+ # Single thread
204
198
  content_ranges.map do |content_range|
205
199
  chunked_upload_part_from_io(io, session.id, content_range)
206
200
  end
207
201
  end
208
202
 
209
- commit_info = chunked_upload_commit_from_io(io, session.id, parts,
210
- content_created_at: content_created_at, content_modified_at: content_modified_at)
203
+ commit_info = chunked_upload_commit_from_io(
204
+ io, session.id, parts,
205
+ content_created_at: content_created_at, content_modified_at: content_modified_at
206
+ )
211
207
  commit_info.entries[0]
208
+ ensure
209
+ chunked_upload_abort_session(session.id) if commit_info.nil? && session
210
+ end
211
+
212
+ def threaded_chunked_upload_part_from_io(io, session_id, content_ranges, n_threads)
213
+ unless gem_parallel_available?
214
+ msg = "parallel chunked uploads requires gem 'parallel'"
215
+ raise BoxrError.new(boxr_message: msg)
216
+ end
217
+
218
+ Parallel.map(content_ranges, in_threads: n_threads) do |content_range|
219
+ File.open(io.path) do |io_dup|
220
+ chunked_upload_part_from_io(io_dup, session_id, content_range)
221
+ end
222
+ end
223
+ end
224
+
225
+ def calculate_chunked_upload_digest(io)
226
+ digest = Digest::SHA1.new
227
+ while (buf = io.read(8 * 1024**2)) && buf.size.positive?
228
+ digest.update(buf)
229
+ end
230
+ io.rewind
231
+ digest
212
232
  end
213
233
 
214
234
  def gem_parallel_available?
data/lib/boxr/client.rb CHANGED
@@ -1,37 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Boxr
4
- class Client
4
+ class Client # rubocop:disable Metrics/ClassLength
5
5
  attr_reader :access_token, :refresh_token, :client_id, :client_secret, :identifier, :as_user_id
6
6
 
7
- # API_URI = "https://wcheng.inside-box.net/api/2.0"
8
- # UPLOAD_URI = "https://upload.wcheng.inside-box.net/api/2.0"
9
-
10
7
  API_URI = 'https://api.box.com/2.0'
11
8
  AUTH_URI = 'https://api.box.com/oauth2/token'
12
9
  REVOKE_AUTH_URI = 'https://api.box.com/oauth2/revoke'
13
10
  UPLOAD_URI = 'https://upload.box.com/api/2.0'
14
- FILES_URI = "#{API_URI}/files"
15
- FILES_UPLOAD_URI = "#{UPLOAD_URI}/files/content"
16
- FOLDERS_URI = "#{API_URI}/folders"
17
- USERS_URI = "#{API_URI}/users"
18
- GROUPS_URI = "#{API_URI}/groups"
19
- GROUP_MEMBERSHIPS_URI = "#{API_URI}/group_memberships"
11
+
12
+ ARCHIVES_URI = "#{API_URI}/archives"
20
13
  COLLABORATIONS_URI = "#{API_URI}/collaborations"
21
14
  COLLECTIONS_URI = "#{API_URI}/collections"
22
15
  COMMENTS_URI = "#{API_URI}/comments"
23
- SEARCH_URI = "#{API_URI}/search"
24
- TASKS_URI = "#{API_URI}/tasks"
25
- TASK_ASSIGNMENTS_URI = "#{API_URI}/task_assignments"
26
- SHARED_ITEMS_URI = "#{API_URI}/shared_items"
16
+ EVENTS_URI = "#{API_URI}/events"
27
17
  FILE_METADATA_URI = "#{API_URI}/files"
18
+ FILES_UPLOAD_URI = "#{UPLOAD_URI}/files/content"
19
+ FILES_URI = "#{API_URI}/files"
28
20
  FOLDER_METADATA_URI = "#{API_URI}/folders"
21
+ FOLDERS_URI = "#{API_URI}/folders"
22
+ GROUP_MEMBERSHIPS_URI = "#{API_URI}/group_memberships"
23
+ GROUPS_URI = "#{API_URI}/groups"
29
24
  METADATA_TEMPLATES_URI = "#{API_URI}/metadata_templates"
30
- EVENTS_URI = "#{API_URI}/events"
25
+ SEARCH_URI = "#{API_URI}/search"
26
+ SHARED_ITEMS_URI = "#{API_URI}/shared_items"
27
+ TASK_ASSIGNMENTS_URI = "#{API_URI}/task_assignments"
28
+ TASKS_URI = "#{API_URI}/tasks"
29
+ USERS_URI = "#{API_URI}/users"
31
30
  WEB_LINKS_URI = "#{API_URI}/web_links"
32
31
  WEBHOOKS_URI = "#{API_URI}/webhooks"
33
32
  ZIP_DOWNLOADS_URI = "#{API_URI}/zip_downloads"
34
33
 
34
+ BOX_VERSION_HEADER_2025 = { 'box-version' => '2025.0' }.freeze
35
+
35
36
  DEFAULT_LIMIT = 100
36
37
  FOLDER_ITEMS_LIMIT = 1000
37
38
 
@@ -93,7 +94,7 @@ module Boxr
93
94
  @jwt_private_key_password = jwt_private_key_password
94
95
  @jwt_public_key_id = jwt_public_key_id
95
96
  @identifier = identifier
96
- @as_user_id = ensure_id(as_user)
97
+ @as_user_id = ensure_id(as_user.is_a?(String) ? as_user.strip : as_user)
97
98
  @token_refresh_listener = token_refresh_listener
98
99
  BOX_CLIENT.proxy = proxy unless proxy.nil?
99
100
  end
@@ -101,15 +102,17 @@ module Boxr
101
102
  private
102
103
 
103
104
  def get(uri, query: nil, success_codes: [200], process_response: true, if_match: nil,
104
- box_api_header: nil, follow_redirect: true)
105
+ box_api_header: nil, follow_redirect: true, extra_headers: nil)
105
106
  uri = Addressable::URI.encode(uri)
106
107
 
107
108
  res = with_auto_token_refresh do
108
109
  headers = standard_headers
109
110
  headers['If-Match'] = if_match unless if_match.nil?
110
111
  headers['BoxApi'] = box_api_header unless box_api_header.nil?
112
+ headers.merge!(extra_headers) unless extra_headers.nil?
111
113
 
112
- BOX_CLIENT.get(uri, query: query, header: headers, follow_redirect: follow_redirect)
114
+ BOX_CLIENT.get(uri, query: query, header: validated_headers(headers),
115
+ follow_redirect: follow_redirect)
113
116
  end
114
117
 
115
118
  check_response_status(res, success_codes)
@@ -131,14 +134,20 @@ module Boxr
131
134
  query[:offset] = offset
132
135
  res = with_auto_token_refresh do
133
136
  headers = standard_headers
134
- BOX_CLIENT.get(uri, query: query, header: headers, follow_redirect: follow_redirect)
137
+ BOX_CLIENT.get(uri, query: query, header: validated_headers(headers),
138
+ follow_redirect: follow_redirect)
135
139
  end
136
140
 
137
141
  unless res.status == 200
138
142
  raise BoxrError.new(status: res.status, body: res.body, header: res.header)
139
143
  end
140
144
 
141
- body_json = JSON.load(res.body) # rubocop:disable Security/JSONLoad
145
+ body_json = parse_response_body(res.body)
146
+ if body_json.nil?
147
+ raise BoxrError.new(status: res.status, body: res.body, header: res.header,
148
+ boxr_message: 'Blank response body from paginated endpoint')
149
+ end
150
+
142
151
  total_count = body_json['total_count']
143
152
  offset += limit
144
153
 
@@ -151,7 +160,7 @@ module Boxr
151
160
  end
152
161
 
153
162
  def post(uri, body, query: nil, success_codes: [201], process_body: true, digest: nil,
154
- content_md5: nil, content_type: nil, if_match: nil, if_non_match: nil)
163
+ content_md5: nil, content_type: nil, if_match: nil, if_non_match: nil, extra_headers: nil)
155
164
  uri = Addressable::URI.encode(uri)
156
165
  body = JSON.dump(body) if process_body
157
166
 
@@ -162,8 +171,9 @@ module Boxr
162
171
  headers['Content-MD5'] = content_md5 unless content_md5.nil?
163
172
  headers['Content-Type'] = content_type unless content_type.nil?
164
173
  headers['Digest'] = digest unless digest.nil?
174
+ headers.merge!(extra_headers) unless extra_headers.nil?
165
175
 
166
- BOX_CLIENT.post(uri, body: body, query: query, header: headers)
176
+ BOX_CLIENT.post(uri, body: body, query: query, header: validated_headers(headers))
167
177
  end
168
178
 
169
179
  check_response_status(res, success_codes)
@@ -172,7 +182,7 @@ module Boxr
172
182
  end
173
183
 
174
184
  def put(uri, body, query: nil, success_codes: [200, 201], process_body: true,
175
- content_type: nil, content_range: nil, digest: nil, if_match: nil)
185
+ content_type: nil, content_range: nil, digest: nil, if_match: nil, extra_headers: nil)
176
186
  uri = Addressable::URI.encode(uri)
177
187
  body = JSON.dump(body) if process_body
178
188
 
@@ -182,8 +192,9 @@ module Boxr
182
192
  headers['Content-Type'] = content_type unless content_type.nil?
183
193
  headers['Content-Range'] = content_range unless content_range.nil?
184
194
  headers['Digest'] = digest unless digest.nil?
195
+ headers.merge!(extra_headers) unless extra_headers.nil?
185
196
 
186
- BOX_CLIENT.put(uri, body: body, query: query, header: headers)
197
+ BOX_CLIENT.put(uri, body: body, query: query, header: validated_headers(headers))
187
198
  end
188
199
 
189
200
  check_response_status(res, success_codes)
@@ -191,14 +202,15 @@ module Boxr
191
202
  processed_response(res)
192
203
  end
193
204
 
194
- def delete(uri, query: nil, success_codes: [204], if_match: nil)
205
+ def delete(uri, query: nil, success_codes: [204], if_match: nil, extra_headers: nil)
195
206
  uri = Addressable::URI.encode(uri)
196
207
 
197
208
  res = with_auto_token_refresh do
198
209
  headers = standard_headers
199
210
  headers['If-Match'] = if_match unless if_match.nil?
211
+ headers.merge!(extra_headers) unless extra_headers.nil?
200
212
 
201
- BOX_CLIENT.delete(uri, query: query, header: headers)
213
+ BOX_CLIENT.delete(uri, query: query, header: validated_headers(headers))
202
214
  end
203
215
 
204
216
  check_response_status(res, success_codes)
@@ -211,7 +223,7 @@ module Boxr
211
223
 
212
224
  res = with_auto_token_refresh do
213
225
  headers = standard_headers
214
- BOX_CLIENT.options(uri, body: JSON.dump(body), header: headers)
226
+ BOX_CLIENT.options(uri, body: JSON.dump(body), header: validated_headers(headers))
215
227
  end
216
228
 
217
229
  check_response_status(res, success_codes)
@@ -225,6 +237,15 @@ module Boxr
225
237
  headers
226
238
  end
227
239
 
240
+ def validated_headers(headers)
241
+ name, = headers.find do |_, value|
242
+ Array(value).flatten.any? { |part| part.to_s.match?(/[\r\n\0]/) }
243
+ end
244
+ raise BoxrError.new(boxr_message: "Invalid characters in #{name} header") unless name.nil?
245
+
246
+ headers
247
+ end
248
+
228
249
  def with_auto_token_refresh
229
250
  return yield unless @refresh_token || @jwt_private_key
230
251
 
@@ -265,10 +286,19 @@ module Boxr
265
286
  end
266
287
 
267
288
  def processed_response(res)
268
- body_json = JSON.load(res.body) # rubocop:disable Security/JSONLoad
289
+ body_json = parse_response_body(res.body)
269
290
  [BoxrMash.new(body_json), res]
270
291
  end
271
292
 
293
+ # Parses a JSON response body. Returns nil for a blank body (the Box API
294
+ # returns empty bodies for some responses), matching the prior leniency of
295
+ # JSON.load while avoiding its unsafe object deserialization (create_additions).
296
+ def parse_response_body(body)
297
+ return nil if body.to_s.empty?
298
+
299
+ JSON.parse(body)
300
+ end
301
+
272
302
  def build_fields_query(fields, all_fields_query)
273
303
  if fields == :all
274
304
  { fields: all_fields_query }
@@ -296,18 +326,30 @@ module Boxr
296
326
  def ensure_id(item)
297
327
  # Ruby 2.4 unified Fixnum and Bignum into Integer. This tests for Ruby 2.4
298
328
  if 1.instance_of?(Integer)
299
- return item if item.instance_of?(String) || item.instance_of?(Integer) || item.nil?
329
+ return ensure_path_segment(item) if item.instance_of?(String) ||
330
+ item.instance_of?(Integer) || item.nil?
300
331
  elsif item.instance_of?(String) || item.instance_of?(Integer) || item.instance_of?(Integer) || item.nil?
301
332
  if item.instance_of?(String) || item.instance_of?(Integer) || item.instance_of?(Integer) || item.nil?
302
- return item
333
+ return ensure_path_segment(item)
303
334
  end
304
335
  end
305
336
 
306
- return item.id if item.respond_to?(:id)
337
+ return ensure_path_segment(item.id) if item.respond_to?(:id)
307
338
 
308
339
  raise BoxrError.new(boxr_message: 'Expecting an id of class String or Fixnum, or object that responds to :id')
309
340
  end
310
341
 
342
+ def ensure_path_segment(value)
343
+ case value
344
+ when nil, Integer
345
+ return value
346
+ when String, Symbol
347
+ return value if value.match?(/\A[0-9A-Za-z_-]+\z/)
348
+ end
349
+
350
+ raise BoxrError.new(boxr_message: "Invalid ID or path segment: #{value.inspect}")
351
+ end
352
+
311
353
  def restore_trashed_item(uri, name, parent)
312
354
  parent_id = ensure_id(parent)
313
355
 
@@ -2,6 +2,15 @@
2
2
 
3
3
  module Boxr
4
4
  class Client
5
+ def collection_from_id(collection_id)
6
+ collection_id = ensure_id(collection_id)
7
+ uri = "#{COLLECTIONS_URI}/#{collection_id}"
8
+
9
+ collection, = get(uri)
10
+ collection
11
+ end
12
+ alias collection collection_from_id
13
+
5
14
  def collections
6
15
  get_all_with_pagination(COLLECTIONS_URI, offset: 0, limit: DEFAULT_LIMIT)
7
16
  end
data/lib/boxr/files.rb CHANGED
@@ -34,8 +34,8 @@ module Boxr
34
34
  alias preview_url embed_url
35
35
  alias preview_link embed_url
36
36
 
37
- def update_file(file, name: nil, description: nil, parent: nil, shared_link: nil, tags: nil,
38
- lock: nil, if_match: nil)
37
+ def update_file(file, name: nil, description: nil, parent: nil, # rubocop:disable Metrics
38
+ shared_link: nil, tags: nil, lock: nil, if_match: nil)
39
39
  file_id = ensure_id(file)
40
40
  parent_id = ensure_id(parent)
41
41
  uri = "#{FILES_URI}/#{file_id}"
@@ -73,14 +73,18 @@ module Boxr
73
73
  update_file(file, parent: new_parent, name: name, if_match: if_match)
74
74
  end
75
75
 
76
- def download_file(file, version: nil, follow_redirect: true)
76
+ def download_file(file, version: nil, follow_redirect: true) # rubocop:disable Metrics
77
77
  file_id = ensure_id(file)
78
+ uri = "#{FILES_URI}/#{file_id}/content"
79
+ query = {}
80
+ query[:version] = version unless version.nil?
78
81
 
79
82
  loop do
80
- uri = "#{FILES_URI}/#{file_id}/content"
81
- query = {}
82
- query[:version] = version unless version.nil?
83
- _, response = get(uri, query: query, success_codes: [302, 202], process_response: false, follow_redirect: false) # we don't want httpclient to automatically follow the redirect; we need to grab it
83
+ # Don't automatically follow the redirect
84
+ _, response = get(uri, query: query, success_codes: [302, 202],
85
+ process_response: false, follow_redirect: false)
86
+
87
+ # By default, Box always returns a 302
84
88
  if response.status == 302
85
89
  location = response.header['Location'][0]
86
90
  return location unless follow_redirect
@@ -88,13 +92,9 @@ module Boxr
88
92
  file_content, = get(location, process_response: false)
89
93
  return file_content
90
94
 
91
- # simply return the url
92
-
93
95
  elsif response.status == 202
94
- retry_after_seconds = response.header['Retry-After'][0]
95
- sleep retry_after_seconds.to_i
96
+ sleep response.header['Retry-After'][0].to_i
96
97
  end
97
- break if file_content
98
98
  end
99
99
  end
100
100
 
@@ -102,18 +102,21 @@ module Boxr
102
102
  download_file(file, version: version, follow_redirect: false)
103
103
  end
104
104
 
105
- def upload_file(path_to_file, parent, name: nil, content_created_at: nil, content_modified_at: nil,
106
- preflight_check: true, send_content_md5: true)
105
+ def upload_file(path_to_file, parent, name: nil, content_created_at: nil, # rubocop:disable Metrics
106
+ content_modified_at: nil, preflight_check: true, send_content_md5: true)
107
107
  filename = name || File.basename(path_to_file)
108
108
 
109
109
  File.open(path_to_file) do |file|
110
- upload_file_from_io(file, parent, name: filename, content_created_at: content_created_at,
111
- content_modified_at: content_modified_at, preflight_check: preflight_check, send_content_md5: send_content_md5)
110
+ upload_file_from_io(
111
+ file, parent, name: filename, content_created_at: content_created_at,
112
+ content_modified_at: content_modified_at, preflight_check: preflight_check,
113
+ send_content_md5: send_content_md5
114
+ )
112
115
  end
113
116
  end
114
117
 
115
- def upload_file_from_io(io, parent, name:, content_created_at: nil, content_modified_at: nil,
116
- preflight_check: true, send_content_md5: true)
118
+ def upload_file_from_io(io, parent, name:, content_created_at: nil, # rubocop:disable Metrics
119
+ content_modified_at: nil, preflight_check: true, send_content_md5: true)
117
120
  parent_id = ensure_id(parent)
118
121
 
119
122
  preflight_check(io, name, parent_id) if preflight_check
@@ -141,18 +144,25 @@ module Boxr
141
144
  file_info.entries[0]
142
145
  end
143
146
 
144
- def upload_new_version_of_file(path_to_file, file, content_modified_at: nil, send_content_md5: true,
145
- preflight_check: true, if_match: nil, name: nil)
147
+ def upload_new_version_of_file( # rubocop:disable Metrics
148
+ path_to_file, file, content_modified_at: nil,
149
+ send_content_md5: true, preflight_check: true, if_match: nil, name: nil
150
+ )
146
151
  filename = name || File.basename(path_to_file)
147
152
 
148
153
  File.open(path_to_file) do |io|
149
- upload_new_version_of_file_from_io(io, file, name: filename,
150
- content_modified_at: content_modified_at, preflight_check: preflight_check, send_content_md5: send_content_md5, if_match: if_match)
154
+ upload_new_version_of_file_from_io(
155
+ io, file,
156
+ name: filename, content_modified_at: content_modified_at,
157
+ preflight_check: preflight_check, send_content_md5: send_content_md5, if_match: if_match
158
+ )
151
159
  end
152
160
  end
153
161
 
154
- def upload_new_version_of_file_from_io(io, file, name: nil, content_modified_at: nil, send_content_md5: true,
155
- preflight_check: true, if_match: nil)
162
+ def upload_new_version_of_file_from_io( # rubocop:disable Metrics
163
+ io, file, name: nil, content_modified_at: nil,
164
+ send_content_md5: true, preflight_check: true, if_match: nil
165
+ )
156
166
  name || file.name
157
167
 
158
168
  file_id = ensure_id(file)
@@ -173,8 +183,9 @@ module Boxr
173
183
 
174
184
  body = { attributes: JSON.dump(attributes), file: io }
175
185
 
176
- file_info, = post(uri, body, process_body: false, content_md5: content_md5,
177
- if_match: if_match)
186
+ file_info, = post(
187
+ uri, body, process_body: false, content_md5: content_md5, if_match: if_match
188
+ )
178
189
 
179
190
  file_info.entries[0]
180
191
  end
@@ -225,7 +236,7 @@ module Boxr
225
236
  new_file
226
237
  end
227
238
 
228
- def thumbnail(file, min_height: nil, min_width: nil, max_height: nil, max_width: nil)
239
+ def thumbnail(file, min_height: nil, min_width: nil, max_height: nil, max_width: nil) # rubocop:disable Metrics
229
240
  file_id = ensure_id(file)
230
241
  uri = "#{FILES_URI}/#{file_id}/thumbnail.png"
231
242
  query = {}
@@ -246,8 +257,8 @@ module Boxr
246
257
  thumbnail
247
258
  end
248
259
 
249
- def create_shared_link_for_file(file, access: nil, unshared_at: nil, can_download: nil,
250
- can_preview: nil, password: nil)
260
+ def create_shared_link_for_file(file, access: nil, unshared_at: nil, # rubocop:disable Metrics
261
+ can_download: nil, can_preview: nil, password: nil)
251
262
  file_id = ensure_id(file)
252
263
  uri = "#{FILES_URI}/#{file_id}"
253
264
  create_shared_link(uri, file_id, access, unshared_at, can_download, can_preview, password)
data/lib/boxr/metadata.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Boxr
4
- class Client
4
+ class Client # rubocop:disable Metrics/ClassLength
5
5
  def create_metadata(file, metadata, scope: :global, template: :properties)
6
6
  file_id = ensure_id(file)
7
+ scope = ensure_path_segment(scope)
8
+ template = ensure_path_segment(template)
7
9
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
8
10
  metadata, = post(uri, metadata, content_type: 'application/json')
9
11
  metadata
@@ -11,6 +13,8 @@ module Boxr
11
13
 
12
14
  def create_folder_metadata(folder, metadata, scope, template)
13
15
  folder_id = ensure_id(folder)
16
+ scope = ensure_path_segment(scope)
17
+ template = ensure_path_segment(template)
14
18
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
15
19
  metadata, = post(uri, metadata, content_type: 'application/json')
16
20
  metadata
@@ -18,6 +22,8 @@ module Boxr
18
22
 
19
23
  def metadata(file, scope: :global, template: :properties)
20
24
  file_id = ensure_id(file)
25
+ scope = ensure_path_segment(scope)
26
+ template = ensure_path_segment(template)
21
27
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
22
28
  metadata, = get(uri)
23
29
  metadata
@@ -25,6 +31,8 @@ module Boxr
25
31
 
26
32
  def folder_metadata(folder, scope, template)
27
33
  folder_id = ensure_id(folder)
34
+ scope = ensure_path_segment(scope)
35
+ template = ensure_path_segment(template)
28
36
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
29
37
  metadata, = get(uri)
30
38
  metadata
@@ -46,6 +54,8 @@ module Boxr
46
54
 
47
55
  def update_metadata(file, updates, scope: :global, template: :properties)
48
56
  file_id = ensure_id(file)
57
+ scope = ensure_path_segment(scope)
58
+ template = ensure_path_segment(template)
49
59
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
50
60
 
51
61
  # in the event just one update is specified ensure that it is packaged inside an array
@@ -57,6 +67,8 @@ module Boxr
57
67
 
58
68
  def update_folder_metadata(folder, updates, scope, template)
59
69
  folder_id = ensure_id(folder)
70
+ scope = ensure_path_segment(scope)
71
+ template = ensure_path_segment(template)
60
72
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
61
73
 
62
74
  # in the event just one update is specified ensure that it is packaged inside an array
@@ -68,6 +80,8 @@ module Boxr
68
80
 
69
81
  def delete_metadata(file, scope: :global, template: :properties)
70
82
  file_id = ensure_id(file)
83
+ scope = ensure_path_segment(scope)
84
+ template = ensure_path_segment(template)
71
85
  uri = "#{FILE_METADATA_URI}/#{file_id}/metadata/#{scope}/#{template}"
72
86
  result, = delete(uri)
73
87
  result
@@ -75,6 +89,8 @@ module Boxr
75
89
 
76
90
  def delete_folder_metadata(folder, scope, template)
77
91
  folder_id = ensure_id(folder)
92
+ scope = ensure_path_segment(scope)
93
+ template = ensure_path_segment(template)
78
94
  uri = "#{FOLDER_METADATA_URI}/#{folder_id}/metadata/#{scope}/#{template}"
79
95
  result, = delete(uri)
80
96
  result
@@ -88,6 +104,8 @@ module Boxr
88
104
  alias get_enterprise_templates enterprise_metadata
89
105
 
90
106
  def metadata_schema(scope, template_key)
107
+ scope = ensure_path_segment(scope)
108
+ template_key = ensure_path_segment(template_key)
91
109
  uri = "#{METADATA_TEMPLATES_URI}/#{scope}/#{template_key}/schema"
92
110
  schema, = get(uri)
93
111
  schema
@@ -116,6 +134,8 @@ module Boxr
116
134
  end
117
135
 
118
136
  def delete_metadata_template(scope, template_key)
137
+ scope = ensure_path_segment(scope)
138
+ template_key = ensure_path_segment(template_key)
119
139
  uri = "#{METADATA_TEMPLATES_URI}/#{scope}/#{template_key}/schema"
120
140
  result, = delete(uri)
121
141
  result
data/lib/boxr/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Boxr
4
- VERSION = '1.23.1'
4
+ VERSION = '1.24.1'
5
5
  end
@@ -31,7 +31,8 @@ module Boxr
31
31
  end
32
32
 
33
33
  def verify_signature
34
- generate_signature(primary_signature_key) == primary_signature || generate_signature(secondary_signature_key) == secondary_signature
34
+ signature_valid?(primary_signature_key, primary_signature) ||
35
+ signature_valid?(secondary_signature_key, secondary_signature)
35
36
  end
36
37
 
37
38
  def generate_signature(key)
@@ -54,5 +55,17 @@ module Boxr
54
55
  def message_age
55
56
  current_time - delivery_time
56
57
  end
58
+
59
+ def secure_compare(expected, actual)
60
+ return false unless expected.bytesize == actual.bytesize
61
+
62
+ expected.bytes.zip(actual.bytes).reduce(0) { |result, (x, y)| result | (x ^ y) }.zero?
63
+ end
64
+
65
+ def signature_valid?(key, signature)
66
+ return false if key.empty? || !signature.is_a?(String)
67
+
68
+ secure_compare(generate_signature(key), signature)
69
+ end
57
70
  end
58
71
  end
data/lib/boxr.rb CHANGED
@@ -7,27 +7,28 @@ require 'addressable/template'
7
7
  require 'jwt'
8
8
  require 'securerandom'
9
9
 
10
- require 'boxr/version'
11
- require 'boxr/errors'
12
- require 'boxr/client'
13
- require 'boxr/shared_items'
14
- require 'boxr/folders'
15
- require 'boxr/files'
10
+ require 'boxr/auth'
11
+ require 'boxr/avatars'
16
12
  require 'boxr/chunked_uploads'
17
- require 'boxr/comments'
18
- require 'boxr/users'
19
- require 'boxr/groups'
13
+ require 'boxr/client'
20
14
  require 'boxr/collaborations'
21
15
  require 'boxr/collections'
16
+ require 'boxr/comments'
17
+ require 'boxr/errors'
18
+ require 'boxr/events'
19
+ require 'boxr/files'
20
+ require 'boxr/folders'
21
+ require 'boxr/groups'
22
+ require 'boxr/metadata'
22
23
  require 'boxr/search'
24
+ require 'boxr/shared_items'
23
25
  require 'boxr/tasks'
24
- require 'boxr/metadata'
25
- require 'boxr/events'
26
- require 'boxr/auth'
27
- require 'boxr/web_links'
26
+ require 'boxr/users'
27
+ require 'boxr/version'
28
28
  require 'boxr/watermarking'
29
- require 'boxr/webhooks'
29
+ require 'boxr/web_links'
30
30
  require 'boxr/webhook_validator'
31
+ require 'boxr/webhooks'
31
32
  require 'boxr/zip_downloads'
32
33
 
33
34
  # Simple wrapper around Array
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.23.1
4
+ version: 1.24.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Burnette
8
8
  - Xavier Hocquet
9
- autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2025-09-25 00:00:00.000000000 Z
11
+ date: 1980-01-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: awesome_print
@@ -250,7 +249,7 @@ dependencies:
250
249
  version: '1.4'
251
250
  - - "<"
252
251
  - !ruby/object:Gem::Version
253
- version: '3'
252
+ version: '4'
254
253
  type: :runtime
255
254
  prerelease: false
256
255
  version_requirements: !ruby/object:Gem::Requirement
@@ -260,7 +259,7 @@ dependencies:
260
259
  version: '1.4'
261
260
  - - "<"
262
261
  - !ruby/object:Gem::Version
263
- version: '3'
262
+ version: '4'
264
263
  description: ''
265
264
  email:
266
265
  - chadburnette@me.com
@@ -293,6 +292,7 @@ files:
293
292
  - examples/user_events.rb
294
293
  - lib/boxr.rb
295
294
  - lib/boxr/auth.rb
295
+ - lib/boxr/avatars.rb
296
296
  - lib/boxr/chunked_uploads.rb
297
297
  - lib/boxr/client.rb
298
298
  - lib/boxr/collaborations.rb
@@ -318,7 +318,6 @@ homepage: https://github.com/cburnette/boxr
318
318
  licenses:
319
319
  - MIT
320
320
  metadata: {}
321
- post_install_message:
322
321
  rdoc_options: []
323
322
  require_paths:
324
323
  - lib
@@ -333,8 +332,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
333
332
  - !ruby/object:Gem::Version
334
333
  version: '0'
335
334
  requirements: []
336
- rubygems_version: 3.1.6
337
- signing_key:
335
+ rubygems_version: 3.6.9
338
336
  specification_version: 4
339
337
  summary: A Ruby client library for the Box V2 Content API.
340
338
  test_files: []