Class: RunApi::Core::Files

Inherits:
Object
  • Object
show all
Includes:
ResourceHelpers
Defined in:
lib/runapi/core/files.rb

Defined Under Namespace

Classes: DeletedFile, FileList, FileObject, UploadResponse

Constant Summary collapse

ENDPOINT =
"/api/v1/files"
PREPARE_ENDPOINT =
"#{ENDPOINT}/prepare"
CONFIRM_ENDPOINT =
"#{ENDPOINT}/confirm"
PROTOCOL_ENDPOINT =
"/v1/files"
RESPONSE_CLASS =
UploadResponse

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Files

Returns a new instance of Files.



50
51
52
# File 'lib/runapi/core/files.rb', line 50

def initialize(http)
  @http = http
end

Instance Method Details

#content(file_id, options: nil) ⇒ Object



81
82
83
# File 'lib/runapi/core/files.rb', line 81

def content(file_id, options: nil)
  @http.request(:get, "#{protocol_file_path(file_id)}/content", options:, raw: true)
end

#create(file: nil, source: nil, file_name: nil, options: nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/runapi/core/files.rb', line 54

def create(file: nil, source: nil, file_name: nil, options: nil)
  validate_source!(file:, source:)

  return upload_direct(file, file_name:, options:) if file

  request(:post, ENDPOINT, body: compact_params(source:, file_name:), options:)
end

#create_file(file:, purpose: "user_data", file_name: nil, options: nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/runapi/core/files.rb', line 62

def create_file(file:, purpose: "user_data", file_name: nil, options: nil)
  path = file_path(file)
  body = MultipartBody.new(
    fields: {purpose:},
    files: {file: MultipartFile.new(path:, filename: file_name || File.basename(path))}
  )
  request(:post, PROTOCOL_ENDPOINT, body:, options:, response_class: FileObject)
end

#delete_file(file_id, options: nil) ⇒ Object



85
86
87
# File 'lib/runapi/core/files.rb', line 85

def delete_file(file_id, options: nil)
  request(:delete, protocol_file_path(file_id), options:, response_class: DeletedFile)
end

#list(after_id: nil, limit: nil, order: nil, purpose: nil, options: nil) ⇒ Object



71
72
73
74
75
# File 'lib/runapi/core/files.rb', line 71

def list(after_id: nil, limit: nil, order: nil, purpose: nil, options: nil)
  query = URI.encode_www_form(compact_params(after: after_id, limit:, order:, purpose:))
  path = query.empty? ? PROTOCOL_ENDPOINT : "#{PROTOCOL_ENDPOINT}?#{query}"
  request(:get, path, options:, response_class: FileList)
end

#retrieve(file_id, options: nil) ⇒ Object



77
78
79
# File 'lib/runapi/core/files.rb', line 77

def retrieve(file_id, options: nil)
  request(:get, protocol_file_path(file_id), options:, response_class: FileObject)
end