Class: ImageKitFile

Inherits:
Object
  • Object
show all
Defined in:
lib/imagekit/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(req_obj) ⇒ ImageKitFile

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



12
13
14
# File 'lib/imagekit/file.rb', line 12

def initialize(req_obj)
  @req_obj = req_obj
end

Instance Method Details

#batch_delete(file_ids) ⇒ Object



81
82
83
84
85
# 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) ⇒ Object



74
75
76
77
78
79
# 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) ⇒ Object



61
62
63
64
65
66
# 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) ⇒ Object



68
69
70
71
72
# File 'lib/imagekit/file.rb', line 68

def (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) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/imagekit/file.rb', line 102

def (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) ⇒ Object



51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



87
88
89
90
91
92
93
94
# 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) ⇒ Object



96
97
98
99
100
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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