Class: Ubiquity::Wiredrive::API::V2::Utilities
- Inherits:
-
Client
- Object
- Client
- Ubiquity::Wiredrive::API::V2::Utilities
show all
- Defined in:
- lib/ubiquity/wiredrive/api/v2/utilities.rb
Instance Attribute Summary
Attributes inherited from Client
#http_client, #http_response, #logger, #request, #response
Instance Method Summary
collapse
-
#asset_create_extended(args = { }, options = { }) ⇒ Object
-
#folder_create(args = { }, options = { }) ⇒ Object
-
#folders_get(args = { }, options = { }) ⇒ Object
-
#path_check(path, path_contains_asset = false, options = { }) ⇒ Object
-
#path_create(args = { }, options = { }) ⇒ Object
-
#path_resolve(path, path_contains_asset = false, options = { }) ⇒ Object
-
#path_resolve_folder(project_id, path, parent_id = nil) ⇒ Object
-
#project_get_by_name(args = { }, options = { }) ⇒ Object
Methods inherited from Client
#asset_create, #asset_delete, #asset_get, #asset_primary_file_init, #assets_get, #error, #error_message, #errors, #file_upload, #initialize, #initialize_http_client, #initialize_logger, #process_request, #process_request_using_class, #project_create, #project_delete, #project_get, #projects_get, #success?
Instance Method Details
#asset_create_extended(args = { }, options = { }) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 5
def asset_create_extended(args = { }, options = { })
_args = args ? args.dup : { }
project_name = _args.delete(:project_name) { }
file_path = _args.delete(:file_path) { }
if file_path
file_name = File.basename(file_path)
_args[:name] ||= file_name
end
if project_name
_args[:project_id] ||= begin
project = project_get_by_name(:name => project_name)
raise ArgumentError, "Project Not Found by Name. '#{project_name}'" unless project
project['id']
end
end
asset = asset_create(_args, options)
return unless asset
if file_path
asset_id = asset['id']
location = asset_primary_file_init(:asset_id => asset_id)
return unless location
file_upload(:file_path => file_path, :destination_uri => location)
end
asset
end
|
#folder_create(args = { }, options = { }) ⇒ Object
36
37
38
39
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 36
def folder_create(args = { }, options = { })
args_out = args.merge(:is_folder => true, :workflow => 'project')
asset_create(args_out, options)
end
|
#folders_get(args = { }, options = { }) ⇒ Object
41
42
43
44
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 41
def folders_get(args = { }, options = { })
args_out = args.merge(:is_folder => true)
assets_get(args_out, options)
end
|
#path_check(path, path_contains_asset = false, options = { }) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 55
def path_check(path, path_contains_asset = false, options = { })
return false unless path
path = path[1..-1] while path.start_with? '/'
path_ary = path.split('/')
existing_path_result = path_resolve(path, path_contains_asset, options)
existing_path_ary = existing_path_result[:id_path_ary]
check_path_length = path_ary.length
existing_path_length = existing_path_ary.length
missing_path = path_ary.drop(existing_path_length)
missing_path_length = missing_path.length
if missing_path_length > 0
if missing_path_length == check_path_length
project_missing = true
if path_contains_asset
asset_missing = true
if check_path_length > 2
searched_folders = true
folder_missing = true
else
searched_folders = false
folder_missing = false
end
else
if check_path_length > 1
searched_folders = true
folder_missing = true
else
searched_folders = false
folder_missing = false
end
end
else
project_missing = false
if path_contains_asset
asset_missing = true
if missing_path_length == 1
folder_missing = false
searched_folders = check_path_length > 2
else
if check_path_length > 2
searched_folders = true
folder_missing = true
else
searched_folders = false
folder_missing = false
end
end
else
searched_folders = true
folder_missing = true
end
end
else
searched_folders = !existing_path_result[:folders].empty?
project_missing = folder_missing = asset_missing = false
end
{
:check_path_ary => path_ary,
:existing => existing_path_result,
:missing_path => missing_path,
:searched_folders => searched_folders,
:project_missing => project_missing,
:folder_missing => folder_missing,
:asset_missing => asset_missing,
}
end
|
#path_create(args = { }, options = { }) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 151
def path_create(args = { }, options = { })
args = { :path => args } if args.is_a?(String)
logger.debug { "PATH CREATE: #{args.inspect}" }
path = args[:path]
raise ArgumentError, ':path is a required argument.' unless path
contains_asset = args.fetch(:contains_asset, false)
asset_file_path = args[:asset_file_path]
overwrite_asset = args.fetch(:overwrite_asset, false)
additional_asset_create_params = args[:additional_asset_create_params] || { }
path_check_result = path_check(path, contains_asset)
logger.debug { "CHECK PATH RESULT #{path_check_result.inspect}" }
return false unless path_check_result
project_missing = path_check_result[:project_missing]
folder_missing = path_check_result[:folder_missing]
asset_missing = path_check_result[:asset_missing]
existing = path_check_result[:existing]
asset = existing[:asset]
searched_folders = path_check_result[:searched_folders]
missing_path = path_check_result[:missing_path]
project_name = path_check_result[:check_path_ary][0]
if project_missing
logger.debug { "Missing Project - Creating Project '#{project_name}'" }
project = project_create(:name => project_name)
raise "Error Creating Project. Response: #{project}" unless project.is_a?(Hash)
path_check_result[:project] = project
project_id = project['id']
missing_path.shift
logger.debug { "Created Project '#{project_name}' - #{project_id}" }
else
project_id = existing[:id_path_ary].first
end
if searched_folders
if folder_missing
parent_folder_id = (existing[:id_path_ary].length <= 1) ? 0 : existing[:id_path_ary].last
asset_name = missing_path.pop if contains_asset
previous_missing = project_missing
missing_path.each do |folder_name|
begin
logger.debug { "Creating folder '#{folder_name}' parent id: #{parent_folder_id} project id: #{project_id}" }
new_folder = folder_create(:name => folder_name, :project_id => project_id, :parent_id => parent_folder_id)
raise "Error Creating Folder. Response: #{new_folder}" unless new_folder.is_a?(Hash)
logger.debug { "New Folder Created: #{new_folder.inspect}" }
parent_folder_id = new_folder['id']
rescue => e
raise "Failed to create folder '#{folder_name}' parent id: '#{parent_folder_id}' project id: '#{project_id}'. Exception: #{e.message}"
end
previous_missing = true
end
else
if contains_asset and not asset_missing
parent_folder_id = existing[:id_path_ary].fetch(-2)
else
parent_folder_id = existing[:id_path_ary].last
end
end
else
parent_folder_id = nil
end
if contains_asset
asset_name ||= File.basename(asset_file_path)
additional_asset_create_params = { } unless additional_asset_create_params.is_a?(Hash)
additional_asset_create_params[:parent_id] = parent_folder_id if parent_folder_id
additional_asset_create_params[:project_id] = project_id
additional_asset_create_params[:file_path] = asset_file_path
additional_asset_create_params[:name] = asset_name
additional_asset_create_params[:workflow] = 'project'
logger.debug { "Path Create Asset Handling. Asset Missing: #{asset_missing} Create Args: #{additional_asset_create_params.inspect}" }
if overwrite_asset and !asset_missing
asset_id = existing[:id_path_ary].last
begin
raise "Error Message: #{error_message}" unless asset_delete(:id => asset_id)
rescue => e
raise e, "Error Deleting Existing Asset. Asset ID: #{asset_id} Exception: #{e.message}"
end
asset_missing = true
end
if asset_missing
asset = asset_create_extended(additional_asset_create_params)
raise "Error Creating Asset: #{asset.inspect} Args: #{additional_asset_create_params.inspect}" unless success?
else
end
path_check_result[:asset] = asset
end
result = path_check_result.merge({ :project_id => project_id, :parent_folder_id => parent_folder_id })
logger.debug { "Create Missing Path Result: #{result.inspect}" }
return result
end
|
#path_resolve(path, path_contains_asset = false, options = { }) ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 269
def path_resolve(path, path_contains_asset = false, options = { })
id_path_ary = [ ]
name_path_ary = [ ]
return_first_matching_asset = options.fetch(:return_first_matching_asset, true)
if path.is_a?(String)
path = path[1..-1] while path.start_with?('/')
path_ary = path.split('/')
elsif path.is_a?(Array)
path_ary = path.dup
else
raise ArgumentError, "path is required to be a String or an Array. Path Class Name: #{path.class.name}"
end
asset_name = path_ary.pop if path_contains_asset
project_name = path_ary.shift
raise ArgumentError, 'path must contain a project name.' unless project_name
logger.debug { "Search for Project Name: #{project_name}" }
projects = projects_get(:name => project_name)
project = projects.first
return {
:name_path => '/',
:name_path_ary => [ ],
:id_path => '/',
:id_path_ary => [ ],
:project => nil,
:asset => nil,
:folders => [ ]
} if !project or project.empty?
project_id = project['id']
id_path_ary << project_id
name_path_ary << project_name
parsed_folders = path_resolve_folder(project_id, path_ary)
if parsed_folders.nil?
asset_folder_id = 0
folders = []
else
id_path_ary.concat(parsed_folders[:id_path_ary])
name_path_ary.concat(parsed_folders[:name_path_ary])
asset_folder_id = parsed_folders[:id_path_ary].last if path_contains_asset
folders = parsed_folders.fetch(:folder_ary, [])
end
asset = nil
if path_contains_asset and (asset_folder_id or path_ary.length == 2)
assets = assets_get(:name => asset_name, :is_folder => false, :project_id => project_id, :parent_id => asset_folder_id)
if assets and !assets.empty?
assets = [ assets.first ] if return_first_matching_asset
id_path_ary.concat assets.map { |_asset| _asset['id'] }
name_path_ary.concat assets.map { |_asset| _asset['name'] }
end
end
{
:name_path => "/#{name_path_ary.join('/')}",
:name_path_ary => name_path_ary,
:id_path => "/#{id_path_ary.join('/')}",
:id_path_ary => id_path_ary,
:project => project,
:asset => asset,
:folders => folders
}
end
|
#path_resolve_folder(project_id, path, parent_id = nil) ⇒ Object
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 350
def path_resolve_folder(project_id, path, parent_id = nil)
if path.is_a?(Array)
path_ary = path.dup
elsif path.is_a? String
path = path[1..-1] while path.start_with?('/')
path_ary = path.split('/')
end
return nil if !path_ary or path_ary.empty?
id_path_ary = [ ]
name_path_ary = [ ]
folder_name = path_ary.shift
name_path_ary << folder_name
args_out = {
:project_id => project_id,
:name => folder_name
}
args_out[:parent_id] = parent_id if parent_id
folders = folders_get(args_out)
folder = folders.first
return nil unless folder
folder_ary = [ folder ]
folder_id = folder['id']
id_path_ary << folder_id.to_s
resolved_folder_path = path_resolve_folder(project_id, path_ary, folder_id)
unless resolved_folder_path.nil?
id_path_ary.concat(resolved_folder_path[:id_path_ary] || [ ])
name_path_ary.concat(resolved_folder_path[:name_path_ary] || [ ])
folder_ary.concat(resolved_folder_path[:folder_ary] || [ ])
end
{
:id_path_ary => id_path_ary,
:name_path_ary => name_path_ary,
:folder_ary => folder_ary
}
end
|
#project_get_by_name(args = { }, options = { }) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/ubiquity/wiredrive/api/v2/utilities.rb', line 46
def project_get_by_name(args = { }, options = { })
project_name = case args
when String; args
when Hash; args[:name]
end
projects = projects_get( { :name => project_name }, options)
projects.first
end
|