boxr 1.22.0 → 1.23.0

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.
data/lib/boxr/client.rb CHANGED
@@ -1,16 +1,16 @@
1
- module Boxr
1
+ # frozen_string_literal: true
2
2
 
3
+ module Boxr
3
4
  class Client
4
-
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"
7
+ # API_URI = "https://wcheng.inside-box.net/api/2.0"
8
+ # UPLOAD_URI = "https://upload.wcheng.inside-box.net/api/2.0"
9
9
 
10
- API_URI = "https://api.box.com/2.0"
11
- AUTH_URI = "https://api.box.com/oauth2/token"
12
- REVOKE_AUTH_URI = "https://api.box.com/oauth2/revoke"
13
- UPLOAD_URI = "https://upload.box.com/api/2.0"
10
+ API_URI = 'https://api.box.com/2.0'
11
+ AUTH_URI = 'https://api.box.com/oauth2/token'
12
+ REVOKE_AUTH_URI = 'https://api.box.com/oauth2/revoke'
13
+ UPLOAD_URI = 'https://upload.box.com/api/2.0'
14
14
  FILES_URI = "#{API_URI}/files"
15
15
  FILES_UPLOAD_URI = "#{UPLOAD_URI}/files/content"
16
16
  FOLDERS_URI = "#{API_URI}/folders"
@@ -35,52 +35,55 @@ module Boxr
35
35
  DEFAULT_LIMIT = 100
36
36
  FOLDER_ITEMS_LIMIT = 1000
37
37
 
38
- FOLDER_AND_FILE_FIELDS = [:type,:id,:sequence_id,:etag,:name,:created_at,:modified_at,:description,
39
- :size,:path_collection,:created_by,:modified_by,:trashed_at,:purged_at,
40
- :content_created_at,:content_modified_at,:owned_by,:shared_link,:folder_upload_email,
41
- :parent,:item_status,:item_collection,:sync_state,:has_collaborations,:permissions,:tags,
42
- :sha1,:shared_link,:version_number,:comment_count,:lock,:extension,:is_package,:can_non_owners_invite]
38
+ FOLDER_AND_FILE_FIELDS = %i[type id sequence_id etag name created_at modified_at description
39
+ size path_collection created_by modified_by trashed_at purged_at
40
+ content_created_at content_modified_at owned_by shared_link folder_upload_email
41
+ parent item_status item_collection sync_state has_collaborations permissions tags
42
+ sha1 shared_link version_number comment_count lock extension is_package can_non_owners_invite].freeze
43
43
  FOLDER_AND_FILE_FIELDS_QUERY = FOLDER_AND_FILE_FIELDS.join(',')
44
44
 
45
- COMMENT_FIELDS = [:type,:id,:is_reply_comment,:message,:tagged_message,:created_by,:created_at,:item,:modified_at]
45
+ COMMENT_FIELDS = %i[type id is_reply_comment message tagged_message created_by created_at
46
+ item modified_at].freeze
46
47
  COMMENT_FIELDS_QUERY = COMMENT_FIELDS.join(',')
47
48
 
48
- TASK_FIELDS = [:type,:id,:item,:due_at,:action,:message,:task_assignment_collection,:is_completed,:created_by,:created_at]
49
+ TASK_FIELDS = %i[type id item due_at action message task_assignment_collection
50
+ is_completed created_by created_at].freeze
49
51
  TASK_FIELDS_QUERY = TASK_FIELDS.join(',')
50
52
 
51
- COLLABORATION_FIELDS = [:type,:id,:created_by,:created_at,:modified_at,:expires_at,:status,:accessible_by,:role,:acknowledged_at,:item]
53
+ COLLABORATION_FIELDS = %i[type id created_by created_at modified_at expires_at status
54
+ accessible_by role acknowledged_at item].freeze
52
55
  COLLABORATION_FIELDS_QUERY = COLLABORATION_FIELDS.join(',')
53
56
 
54
- USER_FIELDS = [:type,:id,:name,:login,:created_at,:modified_at,:role,:language,:timezone,:space_amount,:space_used,
55
- :max_upload_size,:tracking_codes,:can_see_managed_users,:is_sync_enabled,:is_external_collab_restricted,
56
- :status,:job_title,:phone,:address,:avatar_uri,:is_exempt_from_device_limits,:is_exempt_from_login_verification,
57
- :enterprise,:my_tags]
57
+ USER_FIELDS = %i[type id name login created_at modified_at role language timezone space_amount space_used
58
+ max_upload_size tracking_codes can_see_managed_users is_sync_enabled is_external_collab_restricted
59
+ status job_title phone address avatar_uri is_exempt_from_device_limits is_exempt_from_login_verification
60
+ enterprise my_tags].freeze
58
61
  USER_FIELDS_QUERY = USER_FIELDS.join(',')
59
62
 
60
- GROUP_FIELDS = [:type, :id, :name, :created_at, :modified_at]
63
+ GROUP_FIELDS = %i[type id name created_at modified_at].freeze
61
64
  GROUP_FIELDS_QUERY = GROUP_FIELDS.join(',')
62
65
 
63
- WEB_LINK_FIELDS = [:type, :id, :created_at, :created_by, :description, :etag, :item_status, :modified_at, :modified_by,
64
- :name, :owned_by, :parent, :path_collection, :purged_at, :sequence_id, :shared_link, :trashed_at, :url]
66
+ WEB_LINK_FIELDS = %i[type id created_at created_by description etag item_status modified_at modified_by
67
+ name owned_by parent path_collection purged_at sequence_id shared_link trashed_at url].freeze
65
68
  WEB_LINK_FIELDS_QUERY = WEB_LINK_FIELDS.join(',')
66
69
 
67
- VALID_COLLABORATION_ROLES = ['editor','viewer','previewer','uploader','previewer uploader','viewer uploader','co-owner','owner']
68
-
69
-
70
- def initialize( access_token=ENV['BOX_DEVELOPER_TOKEN'],
71
- refresh_token: nil,
72
- client_id: ENV['BOX_CLIENT_ID'],
73
- client_secret: ENV['BOX_CLIENT_SECRET'],
74
- enterprise_id: ENV['BOX_ENTERPRISE_ID'],
75
- jwt_private_key: ENV['JWT_PRIVATE_KEY'],
76
- jwt_private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'],
77
- jwt_public_key_id: ENV['JWT_PUBLIC_KEY_ID'],
78
- identifier: nil,
79
- as_user: nil,
80
- &token_refresh_listener)
81
-
70
+ VALID_COLLABORATION_ROLES = ['editor', 'viewer', 'previewer', 'uploader', 'previewer uploader',
71
+ 'viewer uploader', 'co-owner', 'owner'].freeze
72
+
73
+ def initialize(access_token = ENV['BOX_DEVELOPER_TOKEN'],
74
+ refresh_token: nil,
75
+ client_id: ENV['BOX_CLIENT_ID'],
76
+ client_secret: ENV['BOX_CLIENT_SECRET'],
77
+ enterprise_id: ENV['BOX_ENTERPRISE_ID'],
78
+ jwt_private_key: ENV['JWT_PRIVATE_KEY'],
79
+ jwt_private_key_password: ENV['JWT_PRIVATE_KEY_PASSWORD'],
80
+ jwt_public_key_id: ENV['JWT_PUBLIC_KEY_ID'],
81
+ identifier: nil,
82
+ as_user: nil,
83
+ proxy: nil,
84
+ &token_refresh_listener)
82
85
  @access_token = access_token
83
- raise BoxrError.new(boxr_message: "Access token cannot be nil") if @access_token.nil?
86
+ raise BoxrError.new(boxr_message: 'Access token cannot be nil') if @access_token.nil?
84
87
 
85
88
  @refresh_token = refresh_token
86
89
  @client_id = client_id
@@ -92,12 +95,13 @@ module Boxr
92
95
  @identifier = identifier
93
96
  @as_user_id = ensure_id(as_user)
94
97
  @token_refresh_listener = token_refresh_listener
98
+ BOX_CLIENT.proxy = proxy unless proxy.nil?
95
99
  end
96
100
 
97
-
98
101
  private
99
102
 
100
- def get(uri, query: nil, success_codes: [200], process_response: true, if_match: nil, box_api_header: nil, follow_redirect: true)
103
+ def get(uri, query: nil, success_codes: [200], process_response: true, if_match: nil,
104
+ box_api_header: nil, follow_redirect: true)
101
105
  uri = Addressable::URI.encode(uri)
102
106
 
103
107
  res = with_auto_token_refresh do
@@ -111,17 +115,18 @@ module Boxr
111
115
  check_response_status(res, success_codes)
112
116
 
113
117
  if process_response
114
- return processed_response(res)
118
+ processed_response(res)
115
119
  else
116
- return res.body, res
120
+ [res.body, res]
117
121
  end
118
122
  end
119
123
 
120
- def get_all_with_pagination(uri, query: {}, offset: 0, limit: DEFAULT_LIMIT, follow_redirect: true)
124
+ def get_all_with_pagination(uri, query: {}, offset: 0, limit: DEFAULT_LIMIT,
125
+ follow_redirect: true)
121
126
  uri = Addressable::URI.encode(uri)
122
127
  entries = []
123
128
 
124
- begin
129
+ loop do
125
130
  query[:limit] = limit
126
131
  query[:offset] = offset
127
132
  res = with_auto_token_refresh do
@@ -129,21 +134,24 @@ module Boxr
129
134
  BOX_CLIENT.get(uri, query: query, header: headers, follow_redirect: follow_redirect)
130
135
  end
131
136
 
132
- if (res.status==200)
133
- body_json = JSON.load(res.body)
134
- total_count = body_json["total_count"]
135
- offset = offset + limit
136
-
137
- entries << body_json["entries"]
138
- else
137
+ unless res.status == 200
139
138
  raise BoxrError.new(status: res.status, body: res.body, header: res.header)
140
139
  end
141
- end until offset - total_count >= 0
142
140
 
143
- BoxrCollection.new(entries.flatten.map{ |i| BoxrMash.new(i) })
141
+ body_json = JSON.parse(res.body)
142
+ total_count = body_json['total_count']
143
+ offset += limit
144
+
145
+ entries << body_json['entries']
146
+
147
+ break if offset - total_count >= 0
148
+ end
149
+
150
+ BoxrCollection.new(entries.flatten.map { |i| BoxrMash.new(i) })
144
151
  end
145
152
 
146
- def post(uri, body, query: nil, success_codes: [201], process_body: true, digest: nil, content_md5: nil, content_type: nil, if_match: nil, if_non_match: nil)
153
+ 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)
147
155
  uri = Addressable::URI.encode(uri)
148
156
  body = JSON.dump(body) if process_body
149
157
 
@@ -151,9 +159,9 @@ module Boxr
151
159
  headers = standard_headers
152
160
  headers['If-Match'] = if_match unless if_match.nil?
153
161
  headers['If-Non-Match'] = if_non_match unless if_non_match.nil?
154
- headers["Content-MD5"] = content_md5 unless content_md5.nil?
155
- headers["Content-Type"] = content_type unless content_type.nil?
156
- headers["Digest"] = digest unless digest.nil?
162
+ headers['Content-MD5'] = content_md5 unless content_md5.nil?
163
+ headers['Content-Type'] = content_type unless content_type.nil?
164
+ headers['Digest'] = digest unless digest.nil?
157
165
 
158
166
  BOX_CLIENT.post(uri, body: body, query: query, header: headers)
159
167
  end
@@ -163,16 +171,17 @@ module Boxr
163
171
  processed_response(res)
164
172
  end
165
173
 
166
- def put(uri, body, query: nil, success_codes: [200, 201], process_body: true, content_type: nil, content_range: nil, digest: nil, if_match: nil)
174
+ 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)
167
176
  uri = Addressable::URI.encode(uri)
168
177
  body = JSON.dump(body) if process_body
169
178
 
170
179
  res = with_auto_token_refresh do
171
180
  headers = standard_headers
172
181
  headers['If-Match'] = if_match unless if_match.nil?
173
- headers["Content-Type"] = content_type unless content_type.nil?
174
- headers["Content-Range"] = content_range unless content_range.nil?
175
- headers["Digest"] = digest unless digest.nil?
182
+ headers['Content-Type'] = content_type unless content_type.nil?
183
+ headers['Content-Range'] = content_range unless content_range.nil?
184
+ headers['Digest'] = digest unless digest.nil?
176
185
 
177
186
  BOX_CLIENT.put(uri, body: body, query: query, header: headers)
178
187
  end
@@ -210,34 +219,35 @@ module Boxr
210
219
  processed_response(res)
211
220
  end
212
221
 
213
- def standard_headers()
214
- headers = {"Authorization" => "Bearer #{@access_token}"}
215
- if @jwt_private_key.nil?
216
- headers['As-User'] = "#{@as_user_id}" unless @as_user_id.nil?
217
- end
222
+ def standard_headers
223
+ headers = { 'Authorization' => "Bearer #{@access_token}" }
224
+ headers['As-User'] = @as_user_id.to_s if @jwt_private_key.nil? && !@as_user_id.nil?
218
225
  headers
219
226
  end
220
227
 
221
228
  def with_auto_token_refresh
222
- return yield unless @refresh_token or @jwt_private_key
229
+ return yield unless @refresh_token || @jwt_private_key
223
230
 
224
231
  res = yield
225
232
  if res.status == 401
226
233
  auth_header = res.header['WWW-Authenticate'][0]
227
- if auth_header && auth_header.include?('invalid_token')
234
+ if auth_header&.include?('invalid_token')
228
235
  if @refresh_token
229
- new_tokens = Boxr::refresh_tokens(@refresh_token, client_id: client_id, client_secret: client_secret)
236
+ new_tokens = Boxr.refresh_tokens(@refresh_token, client_id: client_id,
237
+ client_secret: client_secret)
230
238
  @access_token = new_tokens.access_token
231
239
  @refresh_token = new_tokens.refresh_token
232
- @token_refresh_listener.call(@access_token, @refresh_token, @identifier) if @token_refresh_listener
240
+ @token_refresh_listener&.call(@access_token, @refresh_token,
241
+ @identifier)
233
242
  else
234
243
  if @as_user_id
235
- new_token = Boxr::get_user_token(@as_user_id, private_key: @jwt_private_key, private_key_password: @jwt_private_key_password, public_key_id: @jwt_public_key_id, client_id: @client_id, client_secret: @client_secret)
236
- @access_token = new_token.access_token
244
+ new_token = Boxr.get_user_token(@as_user_id, private_key: @jwt_private_key,
245
+ private_key_password: @jwt_private_key_password, public_key_id: @jwt_public_key_id, client_id: @client_id, client_secret: @client_secret)
237
246
  else
238
- new_token = Boxr::get_enterprise_token(private_key: @jwt_private_key, private_key_password: @jwt_private_key_password, public_key_id: @jwt_public_key_id, enterprise_id: @enterprise_id, client_id: @client_id, client_secret: @client_secret)
239
- @access_token = new_token.access_token
247
+ new_token = Boxr.get_enterprise_token(private_key: @jwt_private_key,
248
+ private_key_password: @jwt_private_key_password, public_key_id: @jwt_public_key_id, enterprise_id: @enterprise_id, client_id: @client_id, client_secret: @client_secret)
240
249
  end
250
+ @access_token = new_token.access_token
241
251
  end
242
252
 
243
253
  res = yield
@@ -248,19 +258,22 @@ module Boxr
248
258
  end
249
259
 
250
260
  def check_response_status(res, success_codes)
251
- raise BoxrError.new(status: res.status, body: res.body, header: res.header) unless success_codes.include?(res.status)
261
+ return if success_codes.include?(res.status)
262
+
263
+ raise BoxrError.new(status: res.status, body: res.body,
264
+ header: res.header)
252
265
  end
253
266
 
254
267
  def processed_response(res)
255
- body_json = JSON.load(res.body)
256
- return BoxrMash.new(body_json), res
268
+ body_json = JSON.parse(res.body)
269
+ [BoxrMash.new(body_json), res]
257
270
  end
258
271
 
259
272
  def build_fields_query(fields, all_fields_query)
260
273
  if fields == :all
261
- {:fields => all_fields_query}
262
- elsif fields.is_a?(Array) && fields.length > 0
263
- {:fields => fields.join(',')}
274
+ { fields: all_fields_query }
275
+ elsif fields.is_a?(Array) && fields.length.positive?
276
+ { fields: fields.join(',') }
264
277
  else
265
278
  {}
266
279
  end
@@ -269,29 +282,30 @@ module Boxr
269
282
  def to_comma_separated_string(values)
270
283
  return values if values.is_a?(String) || values.is_a?(Symbol)
271
284
 
272
- if values.is_a?(Array) && values.length > 0
273
- values.join(',')
274
- else
275
- nil
276
- end
285
+ return unless values.is_a?(Array) && values.length.positive?
286
+
287
+ values.join(',')
277
288
  end
278
289
 
279
290
  def build_range_string(from, to)
280
291
  range_string = "#{from},#{to}"
281
- range_string = nil if range_string == ","
292
+ range_string = nil if range_string == ','
282
293
  range_string
283
294
  end
284
295
 
285
296
  def ensure_id(item)
286
297
  # Ruby 2.4 unified Fixnum and Bignum into Integer. This tests for Ruby 2.4
287
- if 1.class == Integer
288
- return item if item.class == String || item.class == Integer || item.nil?
289
- else
290
- return item if item.class == String || item.class == Fixnum || item.class == Bignum || item.nil?
298
+ if 1.instance_of?(Integer)
299
+ return item if item.instance_of?(String) || item.instance_of?(Integer) || item.nil?
300
+ elsif item.instance_of?(String) || item.instance_of?(Integer) || item.instance_of?(Integer) || item.nil?
301
+ if item.instance_of?(String) || item.instance_of?(Integer) || item.instance_of?(Integer) || item.nil?
302
+ return item
303
+ end
291
304
  end
292
305
 
293
306
  return item.id if item.respond_to?(:id)
294
- raise BoxrError.new(boxr_message: "Expecting an id of class String or Fixnum, or object that responds to :id")
307
+
308
+ raise BoxrError.new(boxr_message: 'Expecting an id of class String or Fixnum, or object that responds to :id')
295
309
  end
296
310
 
297
311
  def restore_trashed_item(uri, name, parent)
@@ -299,31 +313,32 @@ module Boxr
299
313
 
300
314
  attributes = {}
301
315
  attributes[:name] = name unless name.nil?
302
- attributes[:parent] = {id: parent_id} unless parent_id.nil?
316
+ attributes[:parent] = { id: parent_id } unless parent_id.nil?
303
317
 
304
- restored_item, response = post(uri, attributes)
318
+ restored_item, = post(uri, attributes)
305
319
  restored_item
306
320
  end
307
321
 
308
- def create_shared_link(uri, item_id, access, unshared_at, can_download, can_preview, password)
309
- attributes = {shared_link: {access: access}}
310
- attributes[:shared_link][:unshared_at] = unshared_at.to_datetime.rfc3339 unless unshared_at.nil?
322
+ def create_shared_link(uri, _item_id, access, unshared_at, can_download, can_preview, password)
323
+ attributes = { shared_link: { access: access } }
324
+ unless unshared_at.nil?
325
+ attributes[:shared_link][:unshared_at] =
326
+ unshared_at.to_datetime.rfc3339
327
+ end
311
328
  attributes[:shared_link][:password] = password unless password.nil?
312
329
  attributes[:shared_link][:permissions] = {} unless can_download.nil? && can_preview.nil?
313
330
  attributes[:shared_link][:permissions][:can_download] = can_download unless can_download.nil?
314
331
  attributes[:shared_link][:permissions][:can_preview] = can_preview unless can_preview.nil?
315
332
 
316
- updated_item, response = put(uri, attributes)
333
+ updated_item, = put(uri, attributes)
317
334
  updated_item
318
335
  end
319
336
 
320
337
  def disable_shared_link(uri)
321
- attributes = {shared_link: nil}
338
+ attributes = { shared_link: nil }
322
339
 
323
- updated_item, response = put(uri, attributes)
340
+ updated_item, = put(uri, attributes)
324
341
  updated_item
325
342
  end
326
-
327
343
  end
328
-
329
344
  end
@@ -1,11 +1,15 @@
1
1
  module Boxr
2
2
  class Client
3
-
4
- def folder_collaborations(folder, fields: [], offset: 0, limit: DEFAULT_LIMIT)
3
+ def folder_collaborations(folder, fields: [], limit: DEFAULT_LIMIT, marker: nil)
5
4
  folder_id = ensure_id(folder)
6
5
  query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
6
+ query[:limit] = limit
7
+ query[:marker] = marker unless marker.nil?
8
+
7
9
  uri = "#{FOLDERS_URI}/#{folder_id}/collaborations"
8
- collaborations = get_all_with_pagination(uri, query: query, offset: offset, limit: limit)
10
+
11
+ folder_collaborations, = get(uri, query: query)
12
+ folder_collaborations['entries']
9
13
  end
10
14
 
11
15
  def file_collaborations(file, fields: [], limit: DEFAULT_LIMIT, marker: nil)
@@ -16,15 +20,15 @@ module Boxr
16
20
 
17
21
  uri = "#{FILES_URI}/#{file_id}/collaborations"
18
22
 
19
- collaborations, response = get(uri, query: query)
20
- collaborations['entries']
23
+ file_collaborations, = get(uri, query: query)
24
+ file_collaborations['entries']
21
25
  end
22
26
 
23
27
  def group_collaborations(group, offset: 0, limit: DEFAULT_LIMIT)
24
28
  group_id = ensure_id(group)
25
29
  uri = "#{GROUPS_URI}/#{group_id}/collaborations"
26
30
 
27
- collaborations = get_all_with_pagination(uri, offset: offset, limit: limit)
31
+ get_all_with_pagination(uri, offset: offset, limit: limit)
28
32
  end
29
33
 
30
34
  def add_collaboration(item, accessible_by, role, fields: [], notify: nil, type: :folder)
@@ -32,11 +36,11 @@ module Boxr
32
36
  query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
33
37
  query[:notify] = notify unless notify.nil?
34
38
 
35
- attributes = {item: {id: item_id, type: type}}
39
+ attributes = { item: { id: item_id, type: type } }
36
40
  attributes[:accessible_by] = accessible_by
37
41
  attributes[:role] = validate_role(role)
38
42
 
39
- collaboration, response = post(COLLABORATIONS_URI, attributes, query: query)
43
+ collaboration, = post(COLLABORATIONS_URI, attributes, query: query)
40
44
  collaboration
41
45
  end
42
46
 
@@ -47,14 +51,14 @@ module Boxr
47
51
  attributes[:role] = validate_role(role) unless role.nil?
48
52
  attributes[:status] = status unless status.nil?
49
53
 
50
- updated_collaboration, response = put(uri, attributes)
54
+ updated_collaboration, = put(uri, attributes)
51
55
  updated_collaboration
52
56
  end
53
57
 
54
58
  def remove_collaboration(collaboration)
55
59
  collaboration_id = ensure_id(collaboration)
56
60
  uri = "#{COLLABORATIONS_URI}/#{collaboration_id}"
57
- result, response = delete(uri)
61
+ result, = delete(uri)
58
62
  result
59
63
  end
60
64
 
@@ -65,15 +69,16 @@ module Boxr
65
69
  query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
66
70
  query[:status] = status unless status.nil?
67
71
 
68
- collaboration, response = get(uri, query: query)
72
+ collaboration, = get(uri, query: query)
69
73
  collaboration
70
74
  end
71
75
 
72
- #these are pending collaborations for the current user; use the As-User Header to request for different users
76
+ # These are pending collaborations for the current user
77
+ # Use the As-User Header to request for different users
73
78
  def pending_collaborations(fields: [])
74
79
  query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
75
80
  query[:status] = :pending
76
- pending_collaborations, response = get(COLLABORATIONS_URI, query: query)
81
+ pending_collaborations, = get(COLLABORATIONS_URI, query: query)
77
82
  pending_collaborations['entries']
78
83
  end
79
84
 
@@ -90,7 +95,9 @@ module Boxr
90
95
  end
91
96
 
92
97
  role = role.to_s
93
- raise BoxrError.new(boxr_message: "Invalid collaboration role: '#{role}'") unless VALID_COLLABORATION_ROLES.include?(role)
98
+ unless VALID_COLLABORATION_ROLES.include?(role)
99
+ raise BoxrError.new(boxr_message: "Invalid collaboration role: '#{role}'")
100
+ end
94
101
 
95
102
  role
96
103
  end
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Boxr
2
4
  class Client
3
-
4
5
  def collections
5
- collections = get_all_with_pagination(COLLECTIONS_URI, offset: 0, limit: DEFAULT_LIMIT)
6
+ get_all_with_pagination(COLLECTIONS_URI, offset: 0, limit: DEFAULT_LIMIT)
6
7
  end
7
8
 
8
9
  def collection_items(collection, fields: [])
9
10
  collection_id = ensure_id(collection)
10
11
  uri = "#{COLLECTIONS_URI}/#{collection_id}/items"
11
12
  query = build_fields_query(fields, FOLDER_AND_FILE_FIELDS_QUERY)
12
- items = get_all_with_pagination(uri, query: query, offset: 0, limit: DEFAULT_LIMIT)
13
+ get_all_with_pagination(uri, query: query, offset: 0, limit: DEFAULT_LIMIT)
13
14
  end
14
-
15
15
  end
16
- end
16
+ end
data/lib/boxr/comments.rb CHANGED
@@ -1,12 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Boxr
2
4
  class Client
3
-
4
5
  def file_comments(file, fields: [])
5
6
  file_id = ensure_id(file)
6
7
  uri = "#{FILES_URI}/#{file_id}/comments"
7
8
  query = build_fields_query(fields, COMMENT_FIELDS_QUERY)
8
9
 
9
- comments = get_all_with_pagination(uri, query: query, offset: 0, limit: DEFAULT_LIMIT)
10
+ get_all_with_pagination(uri, query: query, offset: 0, limit: DEFAULT_LIMIT)
10
11
  end
11
12
 
12
13
  def add_comment_to_file(file, message: nil, tagged_message: nil)
@@ -22,38 +23,37 @@ module Boxr
22
23
  def change_comment(comment, message)
23
24
  comment_id = ensure_id(comment)
24
25
  uri = "#{COMMENTS_URI}/#{comment_id}"
25
- attributes = {message: message}
26
- updated_comment, response = put(uri, attributes)
26
+ attributes = { message: message }
27
+ updated_comment, = put(uri, attributes)
27
28
  updated_comment
28
29
  end
29
30
 
30
31
  def comment_from_id(comment_id, fields: [])
31
32
  comment_id = ensure_id(comment_id)
32
- uri ="#{COMMENTS_URI}/#{comment_id}"
33
- comment, response = get(uri)
33
+ uri = "#{COMMENTS_URI}/#{comment_id}"
34
+ query = build_fields_query(fields, COMMENT_FIELDS_QUERY)
35
+ comment, _response = get(uri, query: query)
34
36
  comment
35
37
  end
36
- alias :comment :comment_from_id
38
+ alias comment comment_from_id
37
39
 
38
40
  def delete_comment(comment)
39
41
  comment_id = ensure_id(comment)
40
42
  uri = "#{COMMENTS_URI}/#{comment_id}"
41
- result, response = delete(uri)
43
+ result, = delete(uri)
42
44
  result
43
45
  end
44
46
 
45
-
46
47
  private
47
48
 
48
49
  def add_comment(type, id, message, tagged_message)
49
50
  uri = COMMENTS_URI
50
- attributes = {item: {type: type, id: id}}
51
+ attributes = { item: { type: type, id: id } }
51
52
  attributes[:message] = message unless message.nil?
52
53
  attributes[:tagged_message] = tagged_message unless tagged_message.nil?
53
54
 
54
- new_comment, response = post(uri, attributes)
55
+ new_comment, = post(uri, attributes)
55
56
  new_comment
56
57
  end
57
-
58
58
  end
59
- end
59
+ end
data/lib/boxr/errors.rb CHANGED
@@ -1,39 +1,44 @@
1
- module Boxr
1
+ # frozen_string_literal: true
2
2
 
3
+ module Boxr
3
4
  class BoxrError < StandardError
4
-
5
- attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :boxr_message, :request_id
5
+ attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :boxr_message,
6
+ :request_id
6
7
 
7
8
  def initialize(status: nil, body: nil, header: nil, boxr_message: nil)
9
+ super()
8
10
  @status = status
9
11
  @response_body = body
10
12
  @header = header
11
13
  @boxr_message = boxr_message
12
14
 
13
- if(body)
14
- begin
15
- body_json = JSON.load(body)
15
+ return unless body
16
16
 
17
- if body_json
18
- @type = body_json["type"]
19
- @box_status = body_json["status"]
20
- @code = body_json["code"]
21
- @help_uri = body_json["help_uri"]
22
- @box_message = body_json["message"]
23
- @request_id = body_json["request_id"]
24
- end
25
- rescue
17
+ begin
18
+ body_json = JSON.parse(body)
19
+
20
+ if body_json
21
+ @type = body_json['type']
22
+ @box_status = body_json['status']
23
+ @code = body_json['code']
24
+ @help_uri = body_json['help_uri']
25
+ @box_message = body_json['message']
26
+ @request_id = body_json['request_id']
26
27
  end
28
+ rescue JSON::ParserError
29
+ # Ignore JSON parsing errors
27
30
  end
28
31
  end
29
32
 
30
33
  def message
31
- auth_header = @header['WWW-Authenticate'][0] unless @header.nil?
32
- if(auth_header && auth_header != [])
34
+ unless @header.nil? || @header['WWW-Authenticate'].nil?
35
+ auth_header = @header['WWW-Authenticate'][0]
36
+ end
37
+ if auth_header && auth_header != []
33
38
  "#{@status}: #{auth_header}"
34
- elsif(@box_message)
39
+ elsif @box_message
35
40
  "#{@status}: #{@box_message}"
36
- elsif(@boxr_message)
41
+ elsif @boxr_message
37
42
  @boxr_message
38
43
  else
39
44
  "#{@status}: #{@response_body}"
@@ -44,5 +49,4 @@ module Boxr
44
49
  message
45
50
  end
46
51
  end
47
-
48
- end
52
+ end