class ImageKitFile

Public Class Methods

new(req_obj) click to toggle source

This File class holds file related operations like upload, list etc

# File lib/imagekit/file.rb, line 12
def initialize(req_obj)
  @req_obj = req_obj
end

Public Instance Methods

batch_delete(file_ids) click to toggle source
# File lib/imagekit/file.rb, line 81
def batch_delete(file_ids)
  url = "#{URL::BASE_URL}#{URL::BULK_FILE_DELETE}"
  payload = {'fileIds': file_ids}
  @req_obj.request("post", url, @req_obj.create_headers, payload.to_json)
end
delete(file_id) click to toggle source
# File lib/imagekit/file.rb, line 74
def delete(file_id)
  # Delete a file_id by file_id
  url = "#{URL::BASE_URL}/#{file_id}"
  headers = @req_obj.create_headers
  @req_obj.request("delete", url, headers)
end
details(file_identifier) click to toggle source
# File lib/imagekit/file.rb, line 61
def details(file_identifier)
  # Get detail of file by file_identifier
  url = "#{URL::BASE_URL}/#{file_identifier}/details/"
  headers = @req_obj.create_headers
  @req_obj.request("get", url, headers)
end
get_metadata(file_id) click to toggle source
# File lib/imagekit/file.rb, line 68
def get_metadata(file_id)
  # Get metadata of a file by file_id
  url = "#{URL::BASE_URL}/#{file_id}/metadata"
  @req_obj.request("get", url, @req_obj.create_headers)
end
get_metadata_from_remote_url(remote_file_url) click to toggle source
# File lib/imagekit/file.rb, line 102
def get_metadata_from_remote_url(remote_file_url)
  if remote_file_url == ""
    raise ArgumentError, "remote_file_url is required"
  end
  url = "#{URL::REMOTE_METADATA_FULL_URL}?url=#{remote_file_url}"
  @req_obj.request("get", url, @req_obj.create_headers)
end
list(options) click to toggle source
# File lib/imagekit/file.rb, line 51
def list(options)
  #  returns list of files on ImageKit Server
  #  :options dictionary of options
  formatted_options = request_formatter(options)
  raise KeyError(LIST_FILES_INPUT_MISSING) unless formatted_options.is_a?(Hash)
  url = URL::BASE_URL
  headers = @req_obj.create_headers.update({params: options})
  @req_obj.request("get", url, headers, options)
end
purge_cache(file_url) click to toggle source
# File lib/imagekit/file.rb, line 87
def purge_cache(file_url)

  # purges cache from server by file_url

  url = "#{URL::BASE_URL}/purge"
  payload = {'url': file_url}
  @req_obj.request("post", url, @req_obj.create_headers, payload)
end
purge_cache_status(request_id) click to toggle source
# File lib/imagekit/file.rb, line 96
def purge_cache_status(request_id)
  # This function is to get cache_status
  url = "#{URL::BASE_URL}/purge/#{request_id}"
  @req_obj.request("get", url, @req_obj.create_headers)
end
update_details(file_id, options) click to toggle source
# File lib/imagekit/file.rb, line 33
def update_details(file_id, options)
  # Update file detail by file_id and options

  unless (options.key? :tags) || (options.key? :custom_coordinates)
    raise ArgumentError, UPDATE_DATA_MISSING
  end
  unless options.fetch(:tags, []).is_a?(Array)
    raise ArgumentError, UPDATE_DATA_TAGS_INVALID
  end
  unless options.fetch(:custom_coordinates, "").is_a?(String)
    raise ArgumentError, UPDATE_DATA_COORDS_INVALID
  end
  url = "#{URL::BASE_URL}/#{file_id}/details/"
  headers = @req_obj.create_headers
  payload = request_formatter(options)
  @req_obj.request("patch", url, headers, payload.to_json)
end
upload(file, file_name, options) click to toggle source
# File lib/imagekit/file.rb, line 16
def upload(file, file_name, options)
  # uploads files with required arguments
  # supports bot url and binary
  raise ArgumentError, MISSING_UPLOAD_FILE_PARAMETER unless file
  raise ArgumentError, MISSING_UPLOAD_FILE_PARAMETER unless file_name
  options = validate_upload_options(options || {})
  if options.is_a?(FalseClass)
    raise ArgumentError, "Invalid Upload option"
  else
    headers = @req_obj.create_headers
    payload = {multipart: true, file: file, fileName: file_name}.merge(options)

    url = "#{URL::BASE_URL}#{URL::UPLOAD}"
    @req_obj.request("post", url, headers, payload)
  end
end
validate_upload_options(options) click to toggle source
# File lib/imagekit/file.rb, line 110
def validate_upload_options(options)

  #  Validates upload value, checks if params are valid,
  #  changes snake to camel case which is supported by
  #  ImageKit server


  response_list = []
  options.each do |key, val|
    if VALID_UPLOAD_OPTIONS.include?(key.to_s)
      if val.is_a?(Array)
        val = val.join(",")
      end
      if val.is_a?(TrueClass) || val.is_a?(FalseClass)
        val = val.to_s
      end
      options[key] = val
    else
      return false
    end
  end
  request_formatter(options)
end