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.



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

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.



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

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.



33
34
35
36
37
38
39
40
41
# File 'lib/base/endpoints/files.rb', line 33

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.



28
29
30
# File 'lib/base/endpoints/files.rb', line 28

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

#get(id) ⇒ Object

Returns the metadata of the file with the given ID.



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

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

    parse(response.body)
  end
end