module Paperclip::Storage::Imgur

Public Class Methods

extended(base) click to toggle source

Specify credentials in a file, path, string or hash. Required fields: client_id, client_secret, refresh_token

# File lib/paperclip/storage/imgur.rb, line 11
def self.extended(base)
  base.instance_eval do
    imgur_credentials = parse_credentials(@options[:imgur_credentials])
    imgur_options = @options[:imgur_options] || {}
    environment = defined?(Rails) ? Rails.env : imgur_options[:environment].to_s

    # Use credentials for the current Rails environment, if any
    @imgur_credentials = (imgur_credentials[environment] || imgur_credentials).symbolize_keys
  end
end

Public Instance Methods

copy_to_local_file(style, destination_path) click to toggle source
# File lib/paperclip/storage/imgur.rb, line 68
def copy_to_local_file(style, destination_path)
  # TO BE DONE
  #local_file = File.open(destination_path, 'wb')
  #local_file.write(imgur_session.get_file(path(style)))
  #local_file.close
end
exists?(style_name = default_style) click to toggle source

We have to trust that any Imgur hash stored into *_file_name represents an existing Imgur image. This assumption let us avoid the latency of a network call. If not, someone has touched where he shouldn't!

# File lib/paperclip/storage/imgur.rb, line 25
def exists?(style_name = default_style)
  if original_filename
    true
  else
    false
  end
end
flush_deletes() click to toggle source
# File lib/paperclip/storage/imgur.rb, line 45
def flush_deletes
  @queued_for_delete.each do |path|
    imgur_session.image.image_delete(path) # Doesn't matter if the image doesn't really exists
  end
  @queued_for_delete = []
end
flush_writes() click to toggle source
# File lib/paperclip/storage/imgur.rb, line 33
def flush_writes
  @queued_for_write.each do |style, file| # 'style' is 'original' etc…
    image = imgur_session.image.image_upload(file)

    # We cannot use update_attribute because it internally calls save, and save calls
    # flush_writes again, and it will end up in a stack overflow due excessive recursion
    instance.update_column :"#{name}_#{:file_name}", image.id
  end
  after_flush_writes
  @queued_for_write = {}
end
path(style_name = default_style) click to toggle source

Returns the path of the attachment. It's exactly the Imgur hash.

# File lib/paperclip/storage/imgur.rb, line 64
def path(style_name = default_style)
  original_filename
end
url(size = default_style) click to toggle source

Returns the image's URL. We don't use imgur_session.find to avoid the latency of a network call.

# File lib/paperclip/storage/imgur.rb, line 54
def url(size = default_style)
  image_id = instance.send("#{name}_#{:file_name}")

  return @url_generator.for(size, {}) if image_id.nil? || image_id.empty? # Show Paperclip's default missing image path

  Imgurapi::Image.new(id: image_id).url(size: size, use_ssl: imgur_session.use_ssl?)
end

Private Instance Methods

imgur_session() click to toggle source
# File lib/paperclip/storage/imgur.rb, line 77
def imgur_session
  @imgur_session ||= Imgurapi::Session.instance(@imgur_credentials)
end
parse_credentials(credentials = nil) click to toggle source
# File lib/paperclip/storage/imgur.rb, line 81
def parse_credentials(credentials = nil)
  if credentials.nil? && defined?(Rails)
    credentials = "#{Rails.root}/config/imgur.yml"
  end

  credentials = case credentials
                when File
                  YAML.load(ERB.new(File.read(credentials.path)).result)
                when String, Pathname
                  YAML.load(ERB.new(File.read(credentials)).result)
                else
                  credentials
                end

  return credentials.stringify_keys if credentials.respond_to? :stringify_keys

  raise ArgumentError, 'Please specify Imgur credentials via a file, string or hash.'
end