module FilestackCommon

Module is mixin for common functionalities that all Filestack objects can call.

Public Instance Methods

send_delete(handle, apikey, security) click to toggle source

Send the delete api request to delete a filelink

@param [String] url The url of the Filehandle

to be deleted (includes security)

@return [Typhoeus::Response]

# File lib/filestack/mixins/filestack_common.rb, line 38
def send_delete(handle, apikey, security)
  return 'Delete requires security' if security.nil?
  signature = security.signature
  policy = security.policy
  base = "#{FilestackConfig::API_URL}/file"
  UploadUtils.make_call(
    "#{base}/#{handle}?key=#{apikey}&signature=#{signature}&policy=#{policy}",
    'delete'
  )
end
send_download(url, filepath) click to toggle source

Get the content of a filehandle

@param [String] filepath Local path of file to be written

@return [Int] Size of file written

# File lib/filestack/mixins/filestack_common.rb, line 27
def send_download(url, filepath)
  content = send_get_content(url)
  File.write(filepath, content.body)
end
send_get_content(url, parameters: nil) click to toggle source

Get the contents of a Filestack handle

@param [String] file_handle The Filelink handle @param [FilestackSecurity] security Optional Filestack security object

if security is enabled

@return [Bytes]

# File lib/filestack/mixins/filestack_common.rb, line 18
def send_get_content(url, parameters: nil)
  UploadUtils.make_call(url, 'get', parameters: parameters)
end
send_metadata(handle, security = nil, params) click to toggle source
# File lib/filestack/mixins/filestack_common.rb, line 96
def send_metadata(handle, security = nil, params)
  if security
    policy = security.policy
    signature = security.signature
    url = "#{FilestackConfig::CDN_URL}/#{handle}/metadata?signature=#{signature}&policy=#{policy}"
  else
    url = "#{FilestackConfig::CDN_URL}/#{handle}/metadata"
  end
  response = UploadUtils.make_call(url, 'get', parameters: params)

  if response.code == 200
    return JSON.parse(response.body)
  end
  raise response.body
end
send_overwrite(filepath, handle, apikey, security) click to toggle source

Send the overwrite api request to update a filelink

@param [String] filepath Filepath of file to upload @param [String] handle The Filelink handle @param [String] apikey Filestack API Key @param [FilestackSecurity] security Filestack security object

@return [Typhoeus::Response]

# File lib/filestack/mixins/filestack_common.rb, line 57
def send_overwrite(filepath, handle, apikey, security)
  return 'Overwrite requires security' if security.nil?

  file = File.open(filepath, 'r')
  mimetype = MiniMime.lookup_by_filename(file).content_type
  content = file.read

  signature = security.signature
  policy = security.policy

  headers = { 'Content-Type' => mimetype }
  base = "#{FilestackConfig::API_URL}/file"

  UploadUtils.make_call(
    "#{base}/#{handle}?key=#{apikey}&signature=#{signature}&policy=#{policy}",
    'put',
    headers: headers,
    parameters: content
  )
end
send_tags(task, handle, security) click to toggle source

Get tags or sfw content from a filelink

@param [String] task 'tags' or 'sfw' @param [String] handle The filehandle @param [Filestack::Security] security Security object

@return [Hash]

# File lib/filestack/mixins/filestack_common.rb, line 85
def send_tags(task, handle, security)
  raise 'You must use security for tags' if security.nil?

  policy = security.policy
  signature = security.signature
  url = "#{FilestackConfig::CDN_URL}/#{task}/"\
    "security=signature:#{signature},policy:#{policy}/#{handle}"
  response = UploadUtils.make_call(url, 'get')
  JSON.parse(response.body)[task]
end