Class: ImageKitIo::Client

Inherits:
Object
  • Object
show all
Includes:
Constantable, Utils::Calculation
Defined in:
lib/imagekitio/client.rb

Overview

ImageKitIo class holds each method will be used by user

Constant Summary

Constants included from Utils::Calculation

Utils::Calculation::DEFAULT_TIME_DIFF

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constantable

#constants, included

Methods included from Utils::Calculation

get_authenticated_params, hamming_distance, is_valid_hex

Constructor Details

#initialize(private_key, public_key, url_endpoint, transformation_pos = nil, options = nil) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/imagekitio/client.rb', line 23

def initialize(private_key, public_key, url_endpoint, transformation_pos = nil, options = nil)
  unless(private_key.is_a?(String) && private_key.to_s.strip.length != 0)
    raise ArgumentError, constants.MISSING_PRIVATE_KEY
  end
  unless(public_key.is_a?(String) && public_key.to_s.strip.length != 0)
    raise ArgumentError, constants.MISSING_PUBLIC_KEY
  end
  unless(url_endpoint.is_a?(String) && url_endpoint.to_s.strip.length != 0)
    raise ArgumentError, constants.MISSING_URL_ENDPOINT
  end

  @private_key = private_key
  @public_key = public_key
  @url_endpoint = url_endpoint
  @transformation_position = transformation_pos
  @options = options

  @ik_req = Request.new(private_key, public_key, url_endpoint)
  @url_obj = Url.new(@ik_req)
  @file_service = ApiService::File.new(@ik_req)
  @custom_metadata_field_service = ApiService::CustomMetadataField.new(@ik_req)
  @folder_service = ApiService::Folder.new(@ik_req)
  @bulk_service = ApiService::Bulk.new(@ik_req)
end

Instance Attribute Details

#bulk_serviceObject (readonly)

Returns the value of attribute bulk_service.



21
22
23
# File 'lib/imagekitio/client.rb', line 21

def bulk_service
  @bulk_service
end

#custom_metadata_field_serviceObject (readonly)

Returns the value of attribute custom_metadata_field_service.



21
22
23
# File 'lib/imagekitio/client.rb', line 21

def 
  @custom_metadata_field_service
end

#file_serviceObject (readonly)

Returns the value of attribute file_service.



21
22
23
# File 'lib/imagekitio/client.rb', line 21

def file_service
  @file_service
end

#folder_serviceObject (readonly)

Returns the value of attribute folder_service.



21
22
23
# File 'lib/imagekitio/client.rb', line 21

def folder_service
  @folder_service
end

Instance Method Details

#add_bulk_tags(file_ids: [], tags: []) ⇒ Object



135
136
137
# File 'lib/imagekitio/client.rb', line 135

def add_bulk_tags(file_ids: [], tags: [])
  @bulk_service.add_tags(file_ids: file_ids, tags: tags)
end

#bulk_job_status(job_id: nil) ⇒ Object



152
153
154
# File 'lib/imagekitio/client.rb', line 152

def bulk_job_status(job_id: nil)
  @bulk_service.job_status(job_id: job_id)
end

#copy_file(source_file_path: nil, destination_path: nil, include_file_versions: false) ⇒ Object



123
124
125
# File 'lib/imagekitio/client.rb', line 123

def copy_file(source_file_path: nil, destination_path: nil, include_file_versions: false)
  @file_service.copy(source_file_path: source_file_path, destination_path: destination_path, include_file_versions: include_file_versions)
end

#copy_folder(source_folder_path: nil, destination_path: nil, include_file_versions: false) ⇒ Object



164
165
166
# File 'lib/imagekitio/client.rb', line 164

def copy_folder(source_folder_path: nil, destination_path: nil, include_file_versions: false)
  @folder_service.copy(source_folder_path: source_folder_path, destination_path: destination_path, include_file_versions: include_file_versions)
end

#create_custom_metadata_field(name: nil, label: nil, schema: {}) ⇒ Object



172
173
174
# File 'lib/imagekitio/client.rb', line 172

def (name: nil, label: nil, schema: {})
  @custom_metadata_field_service.create(name: name, label: label, schema: schema)
end

#create_folder(folder_name: nil, parent_folder_path: "/") ⇒ Object



156
157
158
# File 'lib/imagekitio/client.rb', line 156

def create_folder(folder_name: nil, parent_folder_path: "/")
  @folder_service.create(folder_name: folder_name, parent_folder_path: parent_folder_path)
end

#delete_bulk_ai_tags(file_ids: [], ai_tags: []) ⇒ Object



148
149
150
# File 'lib/imagekitio/client.rb', line 148

def delete_bulk_ai_tags(file_ids: [], ai_tags: [])
  @bulk_service.remove_ai_tags(file_ids: file_ids, ai_tags: ai_tags)
end

#delete_bulk_files(file_ids: []) ⇒ Object



139
140
141
142
# File 'lib/imagekitio/client.rb', line 139

def delete_bulk_files(file_ids: [])
  # Delete file in bulks by list of file id
  @bulk_service.remove_files(file_ids: file_ids)
end

#delete_bulk_tags(file_ids: [], tags: []) ⇒ Object



144
145
146
# File 'lib/imagekitio/client.rb', line 144

def delete_bulk_tags(file_ids: [], tags: [])
  @bulk_service.remove_tags(file_ids: file_ids, tags: tags)
end

#delete_custom_metadata_field(id: nil) ⇒ Object



184
185
186
# File 'lib/imagekitio/client.rb', line 184

def (id: nil)
  @custom_metadata_field_service.delete(id: id)
end

#delete_file(file_id: nil) ⇒ Object



94
95
96
97
# File 'lib/imagekitio/client.rb', line 94

def delete_file(file_id: nil)
  # Delete a file by file-id
  @file_service.delete(file_id: file_id)
end

#delete_file_version(file_id: nil, version_id: nil) ⇒ Object



76
77
78
# File 'lib/imagekitio/client.rb', line 76

def delete_file_version(file_id: nil, version_id: nil)
  @file_service.delete_file_version(file_id: file_id, version_id: version_id)
end

#delete_folder(folder_path: nil) ⇒ Object



160
161
162
# File 'lib/imagekitio/client.rb', line 160

def delete_folder(folder_path: nil)
  @folder_service.delete(folder_path: folder_path)
end

#file_version_detail(file_id: nil, version_id: nil) ⇒ Object



72
73
74
# File 'lib/imagekitio/client.rb', line 72

def file_version_detail(file_id: nil, version_id: nil)
  @file_service.get_file_version_detail(file_id: file_id, version_id: version_id)
end

#file_versions(file_id: nil) ⇒ Object



68
69
70
# File 'lib/imagekitio/client.rb', line 68

def file_versions(file_id: nil)
  @file_service.get_file_versions(file_id: file_id)
end

#get_authentication_parameters(token = nil, expire = nil) ⇒ Object



197
198
199
200
# File 'lib/imagekitio/client.rb', line 197

def get_authentication_parameters(token = nil, expire = nil)
  # Get Authentication params
  get_authenticated_params(token, expire, @ik_req.private_key)
end

#get_custom_metadata_fields(options = {}) ⇒ Object



176
177
178
# File 'lib/imagekitio/client.rb', line 176

def (options = {})
  @custom_metadata_field_service.list(**options)
end

#get_file_details(file_id: nil) ⇒ Object



84
85
86
87
# File 'lib/imagekitio/client.rb', line 84

def get_file_details(file_id: nil)
  # Get file detail by file-id or file_url
  @file_service.details(file_identifier: file_id)
end

#get_file_metadata(file_id: nil) ⇒ Object



99
100
101
102
# File 'lib/imagekitio/client.rb', line 99

def (file_id: nil)
  # Get metadata of a file by file-id
  @file_service.(file_id: file_id)
end

#get_remote_file_url_metadata(remote_file_url: "") ⇒ Object

Get metadata from remote_file_url param remote_file_url: url string of remote file



115
116
117
# File 'lib/imagekitio/client.rb', line 115

def (remote_file_url: "")
  @file_service.(remote_file_url: remote_file_url)
end

#list_files(options = {}) ⇒ Object



63
64
65
66
# File 'lib/imagekitio/client.rb', line 63

def list_files(options = {})
  # list all files
  @file_service.list(**options)
end

#move_file(source_file_path: nil, destination_path: nil) ⇒ Object



127
128
129
# File 'lib/imagekitio/client.rb', line 127

def move_file(source_file_path: nil, destination_path: nil)
  @file_service.move(source_file_path: source_file_path, destination_path: destination_path)
end

#move_folder(source_folder_path: nil, destination_path: nil) ⇒ Object



168
169
170
# File 'lib/imagekitio/client.rb', line 168

def move_folder(source_folder_path: nil, destination_path: nil)
  @folder_service.move(source_folder_path: source_folder_path, destination_path: destination_path)
end

#phash_distance(first, second) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/imagekitio/client.rb', line 188

def phash_distance(first, second)
  # Get hamming distance between two phash(image hash) to check
  # similarity between images
  if first.to_s.strip == "" || second.to_s.strip == ""
    raise ArgumentError, constants.MISSING_PHASH_VALUE
  end
  hamming_distance(first, second)
end

#purge_file_cache(file_url: nil) ⇒ Object



104
105
106
107
# File 'lib/imagekitio/client.rb', line 104

def purge_file_cache(file_url: nil)
  # Purge cache from ImageKitIo server by file_url
  @file_service.purge_cache(file_url: file_url)
end

#purge_file_cache_status(request_id: nil) ⇒ Object



109
110
111
# File 'lib/imagekitio/client.rb', line 109

def purge_file_cache_status(request_id: nil)
  @file_service.purge_cache_status(request_id: request_id.to_s)
end

#rename_file(file_path: nil, new_file_name: nil, **options) ⇒ Object



131
132
133
# File 'lib/imagekitio/client.rb', line 131

def rename_file(file_path: nil, new_file_name: nil, **options)
  @file_service.rename(file_path: file_path, new_file_name: new_file_name, **options)
end

#restore_file_version(file_id: nil, version_id: nil) ⇒ Object



80
81
82
# File 'lib/imagekitio/client.rb', line 80

def restore_file_version(file_id: nil, version_id: nil)
  @file_service.restore_file_version(file_id: file_id, version_id: version_id)
end

#set_ik_request(ik_req) ⇒ Object



48
49
50
51
52
# File 'lib/imagekitio/client.rb', line 48

def set_ik_request(ik_req)
  # setter for imagekit request mainly will be used for
  # test
  @ik_req = ik_req
end

#stream_file(file_url: nil, &block) ⇒ Object



119
120
121
# File 'lib/imagekitio/client.rb', line 119

def stream_file(file_url: nil, &block)
  @file_service.stream_file(remote_file_url: file_url, &block)
end

#update_custom_metadata_field(id: nil, label: nil, schema: nil) ⇒ Object



180
181
182
# File 'lib/imagekitio/client.rb', line 180

def (id: nil, label: nil, schema: nil)
  @custom_metadata_field_service.update(id: id, label: label, schema: schema)
end

#update_file_details(file_id: nil, **options) ⇒ Object



89
90
91
92
# File 'lib/imagekitio/client.rb', line 89

def update_file_details(file_id: nil, **options)
  # update file details by file id and other options payload
  @file_service.update_details(file_id: file_id, **options)
end

#upload_file(file: nil, file_name: nil, **options) ⇒ Object



58
59
60
61
# File 'lib/imagekitio/client.rb', line 58

def upload_file(file: nil, file_name: nil, **options)
  # upload file to imagekit server
  @file_service.upload(file: file, file_name: file_name, **options)
end

#url(options = {}) ⇒ Object



54
55
56
# File 'lib/imagekitio/client.rb', line 54

def url(options = {})
  @url_obj.generate_url(options)
end