Class: Cloudflared::ImagesResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/cloudflared/resources/images.rb

Constant Summary collapse

FIFTEEN_MINUTES =

ONE_DAY = 60 * 60 * 24 ONE_HOUR = 60 * 60 THIRTY_MINUTES = 60 * 30

60 * 15
IMAGE_DELIVERY_URL =
"https://imagedelivery.net"

Instance Attribute Summary

Attributes inherited from Resource

#client

Instance Method Summary collapse

Methods inherited from Resource

#default_headers, #delete_request, #get_request, #handle_response, #initialize, #patch_request, #post_request, #put_request, #upload_request

Constructor Details

This class inherits a constructor from Cloudflared::Resource

Instance Method Details

#delete(file_id:, **params) ⇒ Object



12
13
14
15
# File 'lib/cloudflared/resources/images.rb', line 12

def delete(file_id:, **params)
  delete_request("images/v1/#{file_id}", params: params)
  true
end

#details(file_id:, **params) ⇒ Object



17
18
19
# File 'lib/cloudflared/resources/images.rb', line 17

def details(file_id:, **params)
  Image.new get_request("images/v1/#{file_id}", params: params).body.dig("result")
end

#direct_upload_url(**params) ⇒ Object



21
22
23
# File 'lib/cloudflared/resources/images.rb', line 21

def direct_upload_url(**params)
  post_request("images/v1/direct_upload", body: params).body.dig("result", "uploadURL")
end

#download(file_id:, **params) ⇒ Object



25
26
27
# File 'lib/cloudflared/resources/images.rb', line 25

def download(file_id:, **params)
  get_request("images/v1/#{file_id}/blob", params: params).body
end

#list(**params) ⇒ Object



29
30
31
# File 'lib/cloudflared/resources/images.rb', line 29

def list(**params)
  Collection.from_response get_request("images/v1", params: params), key: "images", type: Image
end

#public_url(path) ⇒ Object



33
34
35
# File 'lib/cloudflared/resources/images.rb', line 33

def public_url(path)
  "#{IMAGE_DELIVERY_URL}/#{@client.images_hash}/#{path}"
end

#signed_url(path, expiry_seconds: FIFTEEN_MINUTES) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloudflared/resources/images.rb', line 37

def signed_url(path, expiry_seconds: FIFTEEN_MINUTES)
  # The path uses the image + the file_id (and a variant if passed through)
  path = path[1..] if path[0] == "/"
  path = "#{@client.images_hash}/#{path}"

  # Calculate the hexdigest with the  leading slash
  sig = OpenSSL::HMAC.hexdigest("SHA256", @client.images_default_key, "/#{path}")

  # Calculate the seconds since the epoch in the future
  exp = Time.new.to_i + expiry_seconds

  # Respond with the url
  qry = path.include?("?") ? "&" : "?"
  "#{IMAGE_DELIVERY_URL}/#{path}#{qry}sig=#{sig}&exp=#{exp}"
end

#stats(**params) ⇒ Object



53
54
55
# File 'lib/cloudflared/resources/images.rb', line 53

def stats(**params)
  get_request("images/v1/stats", params: params).body.dig("result")
end

#update(file_id:, **params) ⇒ Object



57
58
59
# File 'lib/cloudflared/resources/images.rb', line 57

def update(file_id:, **params)
  Image.new patch_request("images/v1/#{file_id}", body: params).body.dig("result")
end

#upload(file:, **params) ⇒ Object



61
62
63
# File 'lib/cloudflared/resources/images.rb', line 61

def upload(file:, **params)
  Image.new upload_request("images/v1", file: file, params: params).body.dig("result")
end