Class: Uploadcare::Rails::File

Inherits:
Entity::File
  • Object
show all
Includes:
Objects::Loadable
Defined in:
lib/uploadcare/rails/objects/file.rb

Overview

A wrapper class for Uploadcare::File object. Allows caching loaded files and has methods for Rails model attributes

Constant Summary collapse

ATTR_ENTITIES =
[:cdn_url].freeze

Instance Method Summary collapse

Methods included from Objects::Loadable

#cache_expires_in, #cache_key, #caching_enabled?, #update_attrs, #uploadcare_configuration

Instance Method Details

#deleteObject



31
32
33
# File 'lib/uploadcare/rails/objects/file.rb', line 31

def delete
  Uploadcare::FileApi.delete_file(uuid)
end

#loadObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/uploadcare/rails/objects/file.rb', line 39

def load
  file_info = if caching_enabled?
                ::Rails.cache.fetch(cache_key, expires_in: cache_expires_in) do
                  request_file_info_from_api
                end
              else
                request_file_info_from_api
              end
  update_attrs(file_info)
end

#loaded?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/uploadcare/rails/objects/file.rb', line 50

def loaded?
  datetime_uploaded.present?
end

#storeObject



25
26
27
28
29
# File 'lib/uploadcare/rails/objects/file.rb', line 25

def store
  file_info = Uploadcare::FileApi.store_file(uuid).merge(cdn_url: cdn_url).to_h
  ::Rails.cache.write(cache_key, file_info, expires_in: cache_expires_in) if caching_enabled?
  update_attrs(file_info)
end

#to_sObject



35
36
37
# File 'lib/uploadcare/rails/objects/file.rb', line 35

def to_s
  cdn_url
end

#transform_url(transformations, transformator_class = Uploadcare::Rails::Transformations::ImageTransformations) ⇒ Object



18
19
20
21
22
23
# File 'lib/uploadcare/rails/objects/file.rb', line 18

def transform_url(transformations, transformator_class = Uploadcare::Rails::Transformations::ImageTransformations)
  return if cdn_url.blank?

  transformations_query = transformator_class.new(transformations).call if transformations.present?
  [cdn_url, transformations_query].compact.join('-')
end