module Paperclip::Storage::Cloudinary
Public Class Methods
extended(base)
click to toggle source
You can download your configuration directly from Cloudinary
to start using this gem immediately:
cloudinary.com/console/cloudinary.yml
# File lib/paperclip/storage/cloudinary.rb, line 9 def self.extended base begin require 'cloudinary' rescue LoadError => e e.message << ' (You may need to install the cloudinary gem)' raise e end end
Public Instance Methods
api_key()
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 99 def api_key cloudinary_credentials[:api_key] end
api_secret()
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 103 def api_secret cloudinary_credentials[:api_secret] end
cloud_name()
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 95 def cloud_name cloudinary_credentials[:cloud_name] end
cloudinary_credentials()
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 107 def cloudinary_credentials @cloudinary_credentials ||= parse_credentials(@options[:cloudinary_credentials] || find_default_config_path) @cloudinary_credentials end
copy_to_local_file(style, local_dest_path)
click to toggle source
# 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)
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 66 def exists? style = default_style ::Cloudinary::Uploader.exists? public_id(style), cloudinary_url_options(style) end
flush_deletes()
click to toggle source
# 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()
click to toggle source
# 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 = @options[:cloudinary_upload_options] || {} default_opts = upload_opts[:default] || {} style_opts = upload_opts[:styles].try(:[], style_name) || {} options = {} [default_opts, style_opts].each do |opts| options.deep_merge!(opts) do |key, existing_value, new_value| new_value end end execute_lambdas options, style_name, self options.merge! defaults ::Cloudinary::Uploader.upload file, options end after_flush_writes @queued_for_write.clear end
parse_credentials(creds)
click to toggle source
# 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)
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 82 def public_id style public_id_for_path(path style) end
public_id_for_path(s)
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 86 def public_id_for_path s s[0..-(File.extname(s).length + 1)] end
resource_type()
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 90 def resource_type type = @options[: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 = {})
click to toggle source
Calls superclass method
# File lib/paperclip/storage/cloudinary.rb, line 70 def url style_or_options = default_style, options = {} if style_or_options.is_a?(Hash) options.merge! style_or_options style = default_style else style = style_or_options end inline_opts = options[:cloudinary] || {} result = ::Cloudinary::Utils.cloudinary_url path(style), cloudinary_url_options(style, inline_opts) result.nil? ? super(nil) : result end
Private Instance Methods
cloudinary_url_options(style_name, inline_opts={})
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 121 def cloudinary_url_options style_name, inline_opts={} url_opts = @options[:cloudinary_url_options] || {} default_opts = url_opts[:default] || {} style_opts = url_opts[:styles].try(:[], style_name) || {} default_opts[:resource_type] = resource_type options = {} [default_opts, style_opts, inline_opts].each do |opts| options.deep_merge!(opts) do |key, existing_value, new_value| new_value.try(:call, style_name, self) || new_value end end options.merge! default_opts options end
execute_lambdas(hash, style_name, attachment)
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 162 def execute_lambdas hash, style_name, attachment hash.each do |key, value| if value.is_a? Hash execute_lambdas value, style_name, attachment elsif value.respond_to?(:call) hash[key] = value.call(style_name, attachment) end end end
find_credentials(creds)
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 147 def find_credentials creds case creds when File YAML::load(ERB.new(File.read(creds.path)).result) when String, Pathname YAML::load(ERB.new(File.read(creds)).result) when Hash creds when NilClass {} else raise ArgumentError, "Credentials given are not a path, file, proc, or hash." end end
find_default_config_path()
click to toggle source
# File lib/paperclip/storage/cloudinary.rb, line 138 def find_default_config_path config_path = Rails.root.join("config/cloudinary.yml") if File.exist? config_path return config_path else return Rails.root.join("config/cloudinary.yaml") end end