Module: Paperclip::Storage::Cloudinary
- Defined in:
- lib/paperclip/storage/cloudinary.rb
Class Method Summary collapse
-
.extended(base) ⇒ Object
You can download your configuration directly from Cloudinary to start using this gem immediately:.
Instance Method Summary collapse
- #api_key ⇒ Object
- #api_secret ⇒ Object
- #cloud_name ⇒ Object
- #cloudinary_credentials ⇒ Object
- #copy_to_local_file(style, local_dest_path) ⇒ Object
- #exists?(style = default_style) ⇒ Boolean
- #flush_deletes ⇒ Object
- #flush_writes ⇒ Object
- #parse_credentials(creds) ⇒ Object
- #public_id(style) ⇒ Object
- #public_id_for_path(s) ⇒ Object
- #resource_type ⇒ Object
- #url(style_or_options = default_style, options = {}) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
You can download your configuration directly from Cloudinary to start using this gem immediately:
9 10 11 12 13 14 15 16 |
# File 'lib/paperclip/storage/cloudinary.rb', line 9 def self.extended base begin require 'cloudinary' rescue LoadError => e e. << ' (You may need to install the cloudinary gem)' raise e end end |
Instance Method Details
#api_key ⇒ Object
99 100 101 |
# File 'lib/paperclip/storage/cloudinary.rb', line 99 def api_key cloudinary_credentials[:api_key] end |
#api_secret ⇒ Object
103 104 105 |
# File 'lib/paperclip/storage/cloudinary.rb', line 103 def api_secret cloudinary_credentials[:api_secret] end |
#cloud_name ⇒ Object
95 96 97 |
# File 'lib/paperclip/storage/cloudinary.rb', line 95 def cloud_name cloudinary_credentials[:cloud_name] end |
#cloudinary_credentials ⇒ Object
107 108 109 110 |
# File 'lib/paperclip/storage/cloudinary.rb', line 107 def cloudinary_credentials @cloudinary_credentials ||= parse_credentials([:cloudinary_credentials] || find_default_config_path) @cloudinary_credentials end |
#copy_to_local_file(style, local_dest_path) ⇒ Object
60 61 62 63 64 |
# File 'lib/paperclip/storage/cloudinary.rb', line 60 def copy_to_local_file style, local_dest_path File.open(local_dest_path, 'wb') do |file| file.write ::Cloudinary::Downloader.download(url(style)) end end |
#exists?(style = default_style) ⇒ Boolean
66 67 68 |
# File 'lib/paperclip/storage/cloudinary.rb', line 66 def exists? style = default_style ::Cloudinary::Uploader.exists? public_id(style), (style) end |
#flush_deletes ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/paperclip/storage/cloudinary.rb', line 48 def flush_deletes @queued_for_delete.each do |path| defaults = { resource_type: resource_type, invalidate: true } ::Cloudinary::Uploader.destroy public_id_for_path(path), defaults end @queued_for_delete.clear end |
#flush_writes ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/paperclip/storage/cloudinary.rb', line 18 def flush_writes @queued_for_write.each do |style_name, file| defaults = { public_id: public_id(style_name), resource_type: resource_type, use_filename: true, unique_filename: false, overwrite: true, invalidate: true } upload_opts = [:cloudinary_upload_options] || {} default_opts = upload_opts[:default] || {} style_opts = upload_opts[:styles].try(:[], style_name) || {} = {} [default_opts, style_opts].each do |opts| .deep_merge!(opts) do |key, existing_value, new_value| new_value end end execute_lambdas , style_name, self .merge! defaults ::Cloudinary::Uploader.upload file, end after_flush_writes @queued_for_write.clear end |
#parse_credentials(creds) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/paperclip/storage/cloudinary.rb', line 112 def parse_credentials creds creds = creds.respond_to?('call') ? creds.call(self) : creds creds = find_credentials(creds).stringify_keys env = Object.const_defined?(:Rails) ? Rails.env : nil (creds[env] || creds).symbolize_keys end |
#public_id(style) ⇒ Object
82 83 84 |
# File 'lib/paperclip/storage/cloudinary.rb', line 82 def public_id style public_id_for_path(path style) end |
#public_id_for_path(s) ⇒ Object
86 87 88 |
# File 'lib/paperclip/storage/cloudinary.rb', line 86 def public_id_for_path s s[0..-(File.extname(s).length + 1)] end |
#resource_type ⇒ Object
90 91 92 93 |
# File 'lib/paperclip/storage/cloudinary.rb', line 90 def resource_type type = [:cloudinary_resource_type] || 'image' %w{image raw video audio}.include?(type.to_s) ? type.to_s : 'image' end |
#url(style_or_options = default_style, options = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/paperclip/storage/cloudinary.rb', line 70 def url = default_style, = {} if .is_a?(Hash) .merge! style = default_style else style = end inline_opts = [:cloudinary] || {} result = ::Cloudinary::Utils.cloudinary_url path(style), (style, inline_opts) result.nil? ? super(nil) : result end |