Class: Shrine::Storage::Cloudinary
- Inherits:
-
Object
- Object
- Shrine::Storage::Cloudinary
- Defined in:
- lib/shrine/storage/cloudinary.rb
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#resource_type ⇒ Object
readonly
Returns the value of attribute resource_type.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#upload_options ⇒ Object
readonly
Returns the value of attribute upload_options.
Instance Method Summary collapse
- #clear!(**options) ⇒ Object
- #delete(id) ⇒ Object
- #exists?(id) ⇒ Boolean
-
#initialize(prefix: nil, resource_type: "image", type: "upload", store_data: nil, upload_options: {}, large: nil) ⇒ Cloudinary
constructor
A new instance of Cloudinary.
- #movable?(io, id) ⇒ Boolean
- #move(io, id, shrine_metadata: {}, **upload_options) ⇒ Object
- #open(id, **options) ⇒ Object
- #presign(id = nil, **options) ⇒ Object
- #update(id, **options) ⇒ Object
- #upload(io, id, shrine_metadata: {}, **upload_options) ⇒ Object
- #url(id, **options) ⇒ Object
Constructor Details
#initialize(prefix: nil, resource_type: "image", type: "upload", store_data: nil, upload_options: {}, large: nil) ⇒ Cloudinary
Returns a new instance of Cloudinary.
10 11 12 13 14 15 16 17 |
# File 'lib/shrine/storage/cloudinary.rb', line 10 def initialize(prefix: nil, resource_type: "image", type: "upload", store_data: nil, upload_options: {}, large: nil) @prefix = prefix @large = large @resource_type = resource_type @type = type @upload_options = @store_data = store_data end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
8 9 10 |
# File 'lib/shrine/storage/cloudinary.rb', line 8 def prefix @prefix end |
#resource_type ⇒ Object (readonly)
Returns the value of attribute resource_type.
8 9 10 |
# File 'lib/shrine/storage/cloudinary.rb', line 8 def resource_type @resource_type end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/shrine/storage/cloudinary.rb', line 8 def type @type end |
#upload_options ⇒ Object (readonly)
Returns the value of attribute upload_options.
8 9 10 |
# File 'lib/shrine/storage/cloudinary.rb', line 8 def @upload_options end |
Instance Method Details
#clear!(**options) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/shrine/storage/cloudinary.rb', line 62 def clear!(**) if prefix ::Cloudinary::Api.delete_resources_by_prefix(prefix, .merge()) else ::Cloudinary::Api.delete_all_resources(.merge()) end end |
#delete(id) ⇒ Object
54 55 56 |
# File 'lib/shrine/storage/cloudinary.rb', line 54 def delete(id) ::Cloudinary::Uploader.destroy(public_id(id), ) end |
#exists?(id) ⇒ Boolean
49 50 51 52 |
# File 'lib/shrine/storage/cloudinary.rb', line 49 def exists?(id) result = ::Cloudinary::Api.resources_by_ids([public_id(id)], ) result.fetch("resources").any? end |
#movable?(io, id) ⇒ Boolean
41 42 43 |
# File 'lib/shrine/storage/cloudinary.rb', line 41 def movable?(io, id) io.is_a?(UploadedFile) && io.storage.is_a?(Storage::Cloudinary) end |
#move(io, id, shrine_metadata: {}, **upload_options) ⇒ Object
37 38 39 |
# File 'lib/shrine/storage/cloudinary.rb', line 37 def move(io, id, shrine_metadata: {}, **) ::Cloudinary::Uploader.rename(io.storage.public_id(io.id), public_id(id), ) end |
#open(id, **options) ⇒ Object
45 46 47 |
# File 'lib/shrine/storage/cloudinary.rb', line 45 def open(id, **) Down::Http.open(url(id), **) end |
#presign(id = nil, **options) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/shrine/storage/cloudinary.rb', line 70 def presign(id = nil, **) = id ? { public_id: public_id(id) } : { folder: prefix } .update(@upload_options) fields = ::Cloudinary::Uploader.build_upload_params(.merge()) fields.reject! { |key, value| value.nil? || value == "" } fields[:signature] = ::Cloudinary::Utils.api_sign_request(fields, ::Cloudinary.config.api_secret) fields[:api_key] = ::Cloudinary.config.api_key url = ::Cloudinary::Utils.cloudinary_api_url("upload", ) { method: :post, url: url, fields: fields } end |
#update(id, **options) ⇒ Object
33 34 35 |
# File 'lib/shrine/storage/cloudinary.rb', line 33 def update(id, **) ::Cloudinary::Uploader.explicit(public_id(id), .merge()) end |
#upload(io, id, shrine_metadata: {}, **upload_options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/shrine/storage/cloudinary.rb', line 19 def upload(io, id, shrine_metadata: {}, **) = { public_id: public_id(id) } .update() .update(@upload_options) .update() result = store(io, **) update_id!(result, id) (result, ) result end |
#url(id, **options) ⇒ Object
58 59 60 |
# File 'lib/shrine/storage/cloudinary.rb', line 58 def url(id, **) ::Cloudinary::Utils.cloudinary_url(path(id), .merge(secure: true, **)) end |