Class: Uploadcare::Rails::AttachedFile
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
#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
Sets the attribute client
18
19
20
|
# File 'lib/uploadcare/rails/attached_file.rb', line 18
def client=(value)
@client = value
end
|
Instance Method Details
#delete ⇒ Object
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
61
62
63
|
# File 'lib/uploadcare/rails/attached_file.rb', line 61
def loaded?
datetime_uploaded.present?
end
|
#store ⇒ Object
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_s ⇒ Object
65
66
67
|
# File 'lib/uploadcare/rails/attached_file.rb', line 65
def to_s
cdn_url
end
|
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
|