Class: DoorLoop::Files

Inherits:
Object
  • Object
show all
Defined in:
lib/doorloop/files.rb

Constant Summary collapse

MAX_FILE_SIZE =

50 MB

50 * 1024 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Files

Returns a new instance of Files.



10
11
12
13
# File 'lib/doorloop/files.rb', line 10

def initialize(client)
  @client = client
  @logger = client.logger
end

Instance Method Details

#delete(id) ⇒ Object



27
28
29
# File 'lib/doorloop/files.rb', line 27

def delete(id)
  @client.delete("files/#{id}")
end

#find(id) ⇒ Object



19
20
21
# File 'lib/doorloop/files.rb', line 19

def find(id)
  @client.get("files/#{id}")
end

#list(options = {}) ⇒ Object



15
16
17
# File 'lib/doorloop/files.rb', line 15

def list(options = {})
  @client.get('files', options)
end

#update(id, params) ⇒ Object



23
24
25
# File 'lib/doorloop/files.rb', line 23

def update(id, params)
  @client.put("files/#{id}", params)
end

#upload(file_path, params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/doorloop/files.rb', line 31

def upload(file_path, params)
  unless params[:name] && params[:resourceType] && params[:resourceId]
    @logger.error("name, resourceType and resourceId are required parameters")
    return nil
  end

  file_size = File.size(file_path)
  if file_size > MAX_FILE_SIZE
    @logger.error("File size exceeds the 50MB limit")
    return nil
  end

  payload = {
    multipart: true,
    file: File.new(file_path, 'rb'),
    name: params[:name],
    resourceId: params[:resourceId],
    resourceType: params[:resourceType],
    notes: params[:notes],
    tags: params[:tags],
    size: params[:size] || file_size,
    mimeType: params[:mimeType],
    createdBy: params[:createdBy],
    createdAt: params[:createdAt]
  }.compact

  begin
    response = @client.post('files', payload)
  rescue RestClient::ExceptionWithResponse => e
    @logger.error("File upload failed: #{e.response}")
    @client.error_handler.handle(e)
  end
end