Class: ImageKitFactory
- Inherits:
-
Object
- Object
- ImageKitFactory
- Includes:
- Singleton
- Defined in:
- app/lib/image_kit_factory.rb
Overview
Library code: image kit factory.
Class Method Summary collapse
- .build_url ⇒ Object
- .client ⇒ Object
- .copy_file ⇒ Object
- .delete_file ⇒ Object
- .get_file ⇒ Object
- .get_metadata ⇒ Object
- .get_metadata_from_url ⇒ Object
- .get_purge_status ⇒ Object
- .list_assets ⇒ Object
- .purge_cache ⇒ Object
- .rename_file ⇒ Object
- .upload_file ⇒ Object
- .url_endpoint ⇒ Object
Instance Method Summary collapse
-
#build_url(src:, transformations: [], query_parameters: {}, transformation_position: 'query') ⇒ Object
Helper methods to build ImageKit parameters This centralizes all ImageKit-specific knowledge in one place.
- #client ⇒ Object
-
#copy_file(source_file_path:, destination_path:, new_file_name: nil, include_file_versions: false) ⇒ Object
Copy a file to a new location (preserves original).
- #delete_file(file_id) ⇒ Object
- #get_file(file_id) ⇒ Object
- #get_metadata(file_id) ⇒ Object
- #get_metadata_from_url(url) ⇒ Object
- #get_purge_status(request_id) ⇒ Object
-
#initialize ⇒ ImageKitFactory
constructor
A new instance of ImageKitFactory.
- #list_assets(tags: []) ⇒ Object
- #purge_cache(url) ⇒ Object
- #rename_file(file_path:, new_file_name:) ⇒ Object
- #upload_file(file:, file_name:, tags: [], folder: 'img/', use_unique_file_name: false) ⇒ Object
- #url_endpoint ⇒ Object
Constructor Details
#initialize ⇒ ImageKitFactory
Returns a new instance of ImageKitFactory.
10 11 12 13 |
# File 'app/lib/image_kit_factory.rb', line 10 def initialize require 'imagekitio' require 'active_storage' end |
Class Method Details
.build_url ⇒ Object
143 144 145 |
# File 'app/lib/image_kit_factory.rb', line 143 def build_url(*, **) instance.build_url(*, **) end |
.client ⇒ Object
15 16 17 |
# File 'app/lib/image_kit_factory.rb', line 15 def self.client instance.client end |
.copy_file ⇒ Object
159 160 161 |
# File 'app/lib/image_kit_factory.rb', line 159 def copy_file(*, **) instance.copy_file(*, **) end |
.delete_file ⇒ Object
163 164 165 |
# File 'app/lib/image_kit_factory.rb', line 163 def delete_file(*, **) instance.delete_file(*, **) end |
.get_file ⇒ Object
151 152 153 |
# File 'app/lib/image_kit_factory.rb', line 151 def get_file(*, **) instance.get_file(*, **) end |
.get_metadata ⇒ Object
167 168 169 |
# File 'app/lib/image_kit_factory.rb', line 167 def (*, **) instance.(*, **) end |
.get_metadata_from_url ⇒ Object
171 172 173 |
# File 'app/lib/image_kit_factory.rb', line 171 def (*, **) instance.(*, **) end |
.get_purge_status ⇒ Object
183 184 185 |
# File 'app/lib/image_kit_factory.rb', line 183 def get_purge_status(*, **) instance.get_purge_status(*, **) end |
.list_assets ⇒ Object
175 176 177 |
# File 'app/lib/image_kit_factory.rb', line 175 def list_assets(*, **) instance.list_assets(*, **) end |
.purge_cache ⇒ Object
179 180 181 |
# File 'app/lib/image_kit_factory.rb', line 179 def purge_cache(*, **) instance.purge_cache(*, **) end |
.rename_file ⇒ Object
155 156 157 |
# File 'app/lib/image_kit_factory.rb', line 155 def rename_file(*, **) instance.rename_file(*, **) end |
.upload_file ⇒ Object
147 148 149 |
# File 'app/lib/image_kit_factory.rb', line 147 def upload_file(*, **) instance.upload_file(*, **) end |
.url_endpoint ⇒ Object
30 31 32 |
# File 'app/lib/image_kit_factory.rb', line 30 def self.url_endpoint instance.url_endpoint end |
Instance Method Details
#build_url(src:, transformations: [], query_parameters: {}, transformation_position: 'query') ⇒ Object
Helper methods to build ImageKit parameters
This centralizes all ImageKit-specific knowledge in one place
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/lib/image_kit_factory.rb', line 40 def build_url(src:, transformations: [], query_parameters: {}, transformation_position: 'query') if transformations.present? transformation_objects = transformations.map do |t| ::Imagekitio::Transformation.new(**t.symbolize_keys) end end = ::Imagekitio::SrcOptions.new( src: src, url_endpoint: url_endpoint, transformation: transformation_objects, transformation_position: transformation_position.to_sym, query_parameters: query_parameters.presence&.stringify_keys ) client.helper.build_url() end |
#client ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'app/lib/image_kit_factory.rb', line 19 def client # ImageKit 4.0: Create client instance with credentials # Note: Client only needs private_key, public_key is not used by v4.0 API @client ||= begin ikioc = Heatwave::Configuration.fetch(:imagekit) ::Imagekitio::Client.new( private_key: ikioc[:private_key] ) end end |
#copy_file(source_file_path:, destination_path:, new_file_name: nil, include_file_versions: false) ⇒ Object
Copy a file to a new location (preserves original)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/lib/image_kit_factory.rb', line 87 def copy_file(source_file_path:, destination_path:, new_file_name: nil, include_file_versions: false) params = ::Imagekitio::FileCopyParams.new( source_file_path: source_file_path, destination_path: destination_path, include_file_versions: include_file_versions ) result = client.files.copy(params) # If a new filename is specified and different from source, rename the copy if new_file_name.present? source_filename = File.basename(source_file_path) if source_filename != new_file_name new_file_path = File.join(destination_path, source_filename) rename_file(file_path: new_file_path, new_file_name: new_file_name) end end result end |
#delete_file(file_id) ⇒ Object
107 108 109 110 |
# File 'app/lib/image_kit_factory.rb', line 107 def delete_file(file_id) params = ::Imagekitio::FileDeleteParams.new client.files.delete(file_id, params) end |
#get_file(file_id) ⇒ Object
69 70 71 72 |
# File 'app/lib/image_kit_factory.rb', line 69 def get_file(file_id) params = ::Imagekitio::FileGetParams.new client.files.get(file_id, params) end |
#get_metadata(file_id) ⇒ Object
112 113 114 115 |
# File 'app/lib/image_kit_factory.rb', line 112 def (file_id) params = ::Imagekitio::Files::MetadataGetParams.new client.files..get(file_id, params) end |
#get_metadata_from_url(url) ⇒ Object
117 118 119 120 |
# File 'app/lib/image_kit_factory.rb', line 117 def (url) params = ::Imagekitio::Files::MetadataGetFromUrlParams.new(url: url) client.files..get_from_url(params) end |
#get_purge_status(request_id) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'app/lib/image_kit_factory.rb', line 132 def get_purge_status(request_id) params = ::Imagekitio::Cache::InvalidationGetParams.new client.cache.invalidation.get(request_id, params) rescue ::Imagekitio::Errors::BadRequestError => e # Request ID is invalid or expired - return nil to indicate failure Rails.logger.warn("ImageKit purge status check failed for request_id #{request_id}: #{e.}") nil end |
#list_assets(tags: []) ⇒ Object
122 123 124 125 |
# File 'app/lib/image_kit_factory.rb', line 122 def list_assets(tags: []) params = ::Imagekitio::AssetListParams.new(tags: ) client.assets.list(params) end |
#purge_cache(url) ⇒ Object
127 128 129 130 |
# File 'app/lib/image_kit_factory.rb', line 127 def purge_cache(url) params = ::Imagekitio::Cache::InvalidationCreateParams.new(url: url) client.cache.invalidation.create(params) end |
#rename_file(file_path:, new_file_name:) ⇒ Object
74 75 76 77 78 79 80 |
# File 'app/lib/image_kit_factory.rb', line 74 def rename_file(file_path:, new_file_name:) params = ::Imagekitio::FileRenameParams.new( file_path: file_path, new_file_name: new_file_name ) client.files.rename(params) end |
#upload_file(file:, file_name:, tags: [], folder: 'img/', use_unique_file_name: false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'app/lib/image_kit_factory.rb', line 58 def upload_file(file:, file_name:, tags: [], folder: 'img/', use_unique_file_name: false) params = ::Imagekitio::FileUploadParams.new( file: file, file_name: file_name, tags: , use_unique_file_name: use_unique_file_name, folder: folder ) client.files.upload(params) end |
#url_endpoint ⇒ Object
34 35 36 |
# File 'app/lib/image_kit_factory.rb', line 34 def url_endpoint @url_endpoint ||= Heatwave::Configuration.fetch(:imagekit)[:url_endpoint] end |