Class: ImageKitIo::ApiService::File
- Inherits:
-
Object
- Object
- ImageKitIo::ApiService::File
- Includes:
- Constantable, Utils::OptionValidator
- Defined in:
- lib/imagekitio/api_service/file.rb
Instance Method Summary collapse
- #copy(source_file_path: nil, destination_path: nil) ⇒ Object
- #delete(file_id: nil) ⇒ Object
- #details(file_identifier: nil) ⇒ Object
- #get_metadata(file_id: nil) ⇒ Object
- #get_metadata_from_remote_url(remote_file_url: nil) ⇒ Object
-
#initialize(req_obj) ⇒ File
constructor
This File class holds file related operations like upload, list etc.
- #list(**options) ⇒ Object
- #move(source_file_path: nil, destination_path: nil) ⇒ Object
- #purge_cache(file_url: nil) ⇒ Object
- #purge_cache_status(request_id: nil) ⇒ Object
- #rename(file_path: nil, new_file_name: nil, **options) ⇒ Object
- #stream_file(remote_file_url: nil, &block) ⇒ Object
- #update_details(file_id: nil, **options) ⇒ Object
-
#upload(file: nil, file_name: nil, **options) ⇒ Object
uploads files with required arguments, supports bot url and binary Options types: -
extensionsshould be array of hash eg: option = [ { ‘name’ => ‘remove-bg’, ‘options’ => { ‘add_shadow’ => ‘true’ } }, { ‘name’ => ‘google-auto-tagging’, ‘minConfidence’ => 80 } ] -custom_metadatashould be hash eg: option = { “SKU”: “VS882HJ2JD”, “price”: 599.99, “brand”: “H&M”, “discount”: 30 }.
Methods included from Constantable
Methods included from Utils::OptionValidator
Methods included from Utils::Formatter
camel_to_snake, format_to_json, request_formatter, snake_to_camel
Constructor Details
#initialize(req_obj) ⇒ File
This File class holds file related operations like upload, list etc
13 14 15 |
# File 'lib/imagekitio/api_service/file.rb', line 13 def initialize(req_obj) @req_obj = req_obj end |
Instance Method Details
#copy(source_file_path: nil, destination_path: nil) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/imagekitio/api_service/file.rb', line 134 def copy(source_file_path: nil, destination_path: nil) if source_file_path == '' || source_file_path.nil? || destination_path == '' || destination_path.nil? raise ArgumentError, 'parameters required' end url = "#{constants.BASE_URL}/copy" payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path } @req_obj.request('post', url, @req_obj.create_headers, payload) end |
#delete(file_id: nil) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/imagekitio/api_service/file.rb', line 90 def delete(file_id: nil) # Delete a file_id by file_id if file_id == "" || file_id.nil? raise ArgumentError, "file_id is required" end url = "#{constants.BASE_URL}/#{file_id}" headers = @req_obj.create_headers @req_obj.request("delete", url, headers) end |
#details(file_identifier: nil) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/imagekitio/api_service/file.rb', line 71 def details(file_identifier: nil) # Get detail of file by file_identifier if file_identifier == "" || file_identifier.nil? raise ArgumentError, "file_identifier is required" end url = "#{constants.BASE_URL}/#{file_identifier}/details/" headers = @req_obj.create_headers @req_obj.request("get", url, headers) end |
#get_metadata(file_id: nil) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/imagekitio/api_service/file.rb', line 81 def (file_id: nil) # Get metadata of a file by file_id if file_id == "" || file_id.nil? raise ArgumentError, "file_id is required" end url = "#{constants.BASE_URL}/#{file_id}/metadata" @req_obj.request("get", url, @req_obj.create_headers) end |
#get_metadata_from_remote_url(remote_file_url: nil) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/imagekitio/api_service/file.rb', line 119 def (remote_file_url: nil) if remote_file_url == "" || remote_file_url.nil? raise ArgumentError, "remote_file_url is required" end url = "#{constants.REMOTE_METADATA_FULL_URL}?url=#{remote_file_url}" @req_obj.request("get", url, @req_obj.create_headers) end |
#list(**options) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/imagekitio/api_service/file.rb', line 61 def list(**) # returns list of files on ImageKitIo Server # :options dictionary of options = request_formatter( || {}) raise KeyError(constants.LIST_FILES_INPUT_MISSING) unless .is_a?(Hash) url = constants.BASE_URL headers = @req_obj.create_headers.update({params: }) @req_obj.request("get", url, headers, ) end |
#move(source_file_path: nil, destination_path: nil) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/imagekitio/api_service/file.rb', line 143 def move(source_file_path: nil, destination_path: nil) if source_file_path == '' || source_file_path.nil? || destination_path == '' || destination_path.nil? raise ArgumentError, 'parameters required' end url = "#{constants.BASE_URL}/move" payload = { 'sourceFilePath': source_file_path, 'destinationPath': destination_path } @req_obj.request('post', url, @req_obj.create_headers, payload) end |
#purge_cache(file_url: nil) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/imagekitio/api_service/file.rb', line 100 def purge_cache(file_url: nil) # purges cache from server by file_url if file_url == "" || file_url.nil? raise ArgumentError, "file_url is required" end url = "#{constants.BASE_URL}/purge" payload = {'url': file_url} @req_obj.request("post", url, @req_obj.create_headers, payload) end |
#purge_cache_status(request_id: nil) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/imagekitio/api_service/file.rb', line 110 def purge_cache_status(request_id: nil) # This function is to get cache_status if request_id == "" || request_id.nil? raise ArgumentError, "remote_file_url is required" end url = "#{constants.BASE_URL}/purge/#{request_id}" @req_obj.request("get", url, @req_obj.create_headers) end |
#rename(file_path: nil, new_file_name: nil, **options) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/imagekitio/api_service/file.rb', line 152 def rename(file_path: nil, new_file_name: nil, **) if file_path == '' || file_path.nil? || new_file_name == '' || new_file_name.nil? raise ArgumentError, 'parameters required' end url = "#{constants.BASE_URL}/rename" payload = { 'filePath': file_path, 'newFileName': new_file_name }.merge(request_formatter()).to_json @req_obj.request('put', url, @req_obj.create_headers, payload) end |
#stream_file(remote_file_url: nil, &block) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/imagekitio/api_service/file.rb', line 127 def stream_file(remote_file_url: nil, &block) if remote_file_url == '' || remote_file_url.nil? raise ArgumentError, 'remote_file_url is required' end @req_obj.request_stream('get', remote_file_url, headers: @req_obj.create_headers, &block) end |
#update_details(file_id: nil, **options) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/imagekitio/api_service/file.rb', line 48 def update_details(file_id: nil, **) unless .fetch(:tags, []).is_a?(Array) raise ArgumentError, constants.UPDATE_DATA_TAGS_INVALID end unless .fetch(:custom_coordinates, "").is_a?(String) raise ArgumentError, constants.UPDATE_DATA_COORDS_INVALID end url = "#{constants.BASE_URL}/#{file_id}/details/" headers = @req_obj.create_headers payload = request_formatter( || {}) @req_obj.request("patch", url, headers, payload.to_json) end |
#upload(file: nil, file_name: nil, **options) ⇒ Object
uploads files with required arguments, supports bot url and binary Options types:
- `extensions` should be array of hash
eg: option['extension'] = [
{ 'name' => 'remove-bg', 'options' => { 'add_shadow' => 'true' } },
{ 'name' => 'google-auto-tagging', 'minConfidence' => 80 }
]
- `custom_metadata` should be hash
eg: option['custom_metadata'] = {
"SKU": "VS882HJ2JD",
"price": 599.99,
"brand": "H&M",
"discount": 30
}
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/imagekitio/api_service/file.rb', line 31 def upload(file: nil, file_name: nil, **) raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file_name = format_to_json(, :extensions, Array) = format_to_json(, :custom_metadata, Hash) = ( || {}) if .is_a?(FalseClass) raise ArgumentError, "Invalid Upload option" else headers = @req_obj.create_headers payload = {multipart: true, file: file, fileName: file_name}.merge() url = "#{constants.BASE_URL}#{constants.UPLOAD}" @req_obj.request("post", url, headers, payload) end end |