Class: Base::Endpoints::Files

Inherits:
Base::Endpoint show all
Defined in:
lib/base/endpoints/files.rb

Overview

This endpoint contains methods for uploading and managing files.

Instance Attribute Summary

Attributes inherited from Base::Endpoint

#connection, #path

Instance Method Summary collapse

Methods inherited from Base::Endpoint

#io, #parse, #request

Constructor Details

#initialize(access_token:, url:) ⇒ Files

Initializes this endpoint.



8
9
10
11
# File 'lib/base/endpoints/files.rb', line 8

def initialize(access_token:, url:)
  @path = 'files'
  super
end

Instance Method Details

#create(path:, type:, filename:) ⇒ Object

Uploads the given file and returns its metadata.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/base/endpoints/files.rb', line 24

def create(path:, type:, filename:)
  request do
    io =
      Faraday::UploadIO.new(path, type, filename)

    response =
      connection.post('', 'file' => io)

    parse(response.body)
  end
end

#delete(id) ⇒ Object

Deletes the file with the given ID.



64
65
66
67
68
69
70
71
# File 'lib/base/endpoints/files.rb', line 64

def delete(id)
  request do
    response =
      connection.delete id

    parse(response.body)
  end
end

#download(id) ⇒ Object

Downloads the file with the given ID into an IO.



43
44
45
46
47
48
49
50
51
# File 'lib/base/endpoints/files.rb', line 43

def download(id)
  response =
    Faraday.new(download_url(id)) do |conn|
      conn.use RaiseError
      conn.use Faraday::Adapter::NetHttp
    end.get

  io(response.body)
end

#download_url(id) ⇒ Object

Returns the publicly accessible download URL of the file with the given ID.



38
39
40
# File 'lib/base/endpoints/files.rb', line 38

def download_url(id)
  "#{connection.url_prefix}#{id}/download"
end

#get(id) ⇒ Object

Returns the metadata of the file with the given ID.



54
55
56
57
58
59
60
61
# File 'lib/base/endpoints/files.rb', line 54

def get(id)
  request do
    response =
      connection.get id

    parse(response.body)
  end
end

#list(page: 1, per_page: 10) ⇒ Object

Lists the files of a project



14
15
16
17
18
19
20
21
# File 'lib/base/endpoints/files.rb', line 14

def list(page: 1, per_page: 10)
  request do
    response =
      connection.get('', per_page: per_page, page: page)

    parse(response.body)
  end
end