Class: Shrine::Storage::Cloudinary
- Inherits:
-
Object
- Object
- Shrine::Storage::Cloudinary
- Defined in:
- lib/shrine/storage/cloudinary.rb
Defined Under Namespace
Classes: Delegator
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.
- #open(id, **options) ⇒ Object
- #presign(id = nil, **presign_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
55 56 57 58 59 60 61 |
# File 'lib/shrine/storage/cloudinary.rb', line 55 def clear!(**) if prefix api.delete_resources_by_prefix(prefix, **) else api.delete_all_resources(**) end end |
#delete(id) ⇒ Object
47 48 49 |
# File 'lib/shrine/storage/cloudinary.rb', line 47 def delete(id) uploader.destroy(public_id(id)) end |
#exists?(id) ⇒ Boolean
42 43 44 45 |
# File 'lib/shrine/storage/cloudinary.rb', line 42 def exists?(id) result = api.resources_by_ids([public_id(id)]) result.fetch("resources").any? end |
#open(id, **options) ⇒ Object
36 37 38 39 40 |
# File 'lib/shrine/storage/cloudinary.rb', line 36 def open(id, **) Down::Http.open(url(id, sign_url: true), **) rescue Down::NotFound raise Shrine::FileNotFound, "file #{id.inspect} not found on storage" end |
#presign(id = nil, **presign_options) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/shrine/storage/cloudinary.rb', line 63 def presign(id = nil, **) = id ? { public_id: public_id(id) } : { folder: prefix } .merge!(@upload_options) .merge!() fields = ::Cloudinary::Uploader.build_upload_params() 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 = utils.cloudinary_api_url("upload") { method: :post, url: url, fields: fields } end |
#update(id, **options) ⇒ Object
32 33 34 |
# File 'lib/shrine/storage/cloudinary.rb', line 32 def update(id, **) uploader.explicit(public_id(id), **) end |
#upload(io, id, shrine_metadata: {}, **upload_options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/shrine/storage/cloudinary.rb', line 19 def upload(io, id, shrine_metadata: {}, **) = { public_id: public_id(id) } .merge!(@upload_options) .merge!() result = store(io, **) update_id!(result, id) (result, ) result end |
#url(id, **options) ⇒ Object
51 52 53 |
# File 'lib/shrine/storage/cloudinary.rb', line 51 def url(id, **) utils.cloudinary_url(path(id), secure: true, **) end |