Class: Dato::Uploads

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/uploads.rb

Constant Summary collapse

BASE_ITEM_URL =
"https://site-api.datocms.com/upload-requests"

Instance Method Summary collapse

Constructor Details

#initialize(api_token) ⇒ Uploads

Returns a new instance of Uploads.



7
8
9
10
11
12
13
14
15
16
# File 'lib/dato/uploads.rb', line 7

def initialize(api_token)
  @auth_header = "Bearer #{api_token}"

  headers = {
    "Authorization" => @auth_header,
    "Accept" => "application/json",
    "X-Api-Version" => 3
  }
  @http_headers = HTTP.headers(headers)
end

Instance Method Details

#create_from_file(path_to_file, filename: nil, attributes: nil, meta: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/dato/uploads.rb', line 26

def create_from_file(path_to_file, filename: nil, attributes: nil, meta: nil)
  request = request_file_upload_permission(filename || path_to_file)
  upload_url = request[:url]
  upload_id = request[:id]

  upload_file_to_bucket(url: upload_url, path: path_to_file)

  upload_file_to_dato(upload_id:, attributes:)
end

#create_from_url(url, filename: nil, attributes: nil, meta: nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/dato/uploads.rb', line 18

def create_from_url(url, filename: nil, attributes: nil, meta: nil)
  file = download_file_from_url(url)
  result = create_from_file(file.path, filename:, attributes:, meta:)
  file.delete

  result
end

#retrieve_job_result(job_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dato/uploads.rb', line 36

def retrieve_job_result(job_id)
  headers = {
    "X-Api-Version" => 3,
    "Authorization" => @auth_header,
    "Accept" => "application/json",
    "Content-Type" => "application/vnd.api+json"
  }

  headers = HTTP.headers(headers)

  headers.get("https://site-api.datocms.com/job-results/#{job_id}")
end