Class: Uploadcare::Rails::AttachedFile

Inherits:
Object
  • Object
show all
Includes:
Internal::AttachmentLoading
Defined in:
lib/uploadcare/rails/attached_file.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  uuid cdn_url datetime_uploaded datetime_stored datetime_removed
  is_image is_ready mime_type original_file_url original_filename
  size url variations content_info metadata appdata source
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Internal::AttachmentLoading

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

Constructor Details

#initialize(attributes = {}, client: nil) ⇒ AttachedFile

Returns a new instance of AttachedFile.



20
21
22
23
24
25
26
27
# File 'lib/uploadcare/rails/attached_file.rb', line 20

def initialize(attributes = {}, client: nil)
  @client = client
  attributes = attributes.transform_keys(&:to_s)
  ATTRIBUTES.each do |attr|
    key = attr.to_s
    instance_variable_set("@#{attr}", attributes[key]) if attributes.key?(key)
  end
end

Instance Attribute Details

#client=(value) ⇒ Object (writeonly)

Sets the attribute client

Parameters:

  • value

    the value to set the attribute client to.



18
19
20
# File 'lib/uploadcare/rails/attached_file.rb', line 18

def client=(value)
  @client = value
end

Instance Method Details

#deleteObject



44
45
46
# File 'lib/uploadcare/rails/attached_file.rb', line 44

def delete
  resolve_client.files.batch_delete(uuids: [ uuid ])
end

#load(force: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/uploadcare/rails/attached_file.rb', line 48

def load(force: false)
  ::Rails.cache.delete(cache_key) if force && caching_enabled?

  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)


61
62
63
# File 'lib/uploadcare/rails/attached_file.rb', line 61

def loaded?
  datetime_uploaded.present?
end

#storeObject



36
37
38
39
40
41
42
# File 'lib/uploadcare/rails/attached_file.rb', line 36

def store
  resource = Uploadcare::Resources::File.new({ "uuid" => uuid }, resolve_client)
  resource.store
  file_info = file_info_from_resource(resource)
  ::Rails.cache.write(cache_key, file_info, expires_in: cache_expires_in) if caching_enabled?
  update_attrs(file_info)
end

#to_sObject



65
66
67
# File 'lib/uploadcare/rails/attached_file.rb', line 65

def to_s
  cdn_url
end

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



29
30
31
32
33
34
# File 'lib/uploadcare/rails/attached_file.rb', line 29

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

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