class Paperclip::Tinify

Attributes

environments[RW]
tinify_key[RW]

Public Instance Methods

make() click to toggle source
# File lib/paperclip-tinify.rb, line 9
def make
  case content_type
  when 'image/jpeg', 'image/png' then compress
  else
    @file
  end
end

Private Instance Methods

compress() click to toggle source
# File lib/paperclip-tinify.rb, line 31
def compress
  src_path = File.expand_path(@file.path)

  unless environments.include? RailsEnvironment.get.to_sym
    Paperclip.log "tinify ignores this environment, skipping..."

    return File.open(src_path)
  end

  if api_key = Tinify.tinify_key
    Paperclip.log "tinifying #{src_path}"

    ::Tinify.key = api_key
    ::Tinify.from_file(src_path).to_file(src_path)
  else
    Paperclip.log "tinify key was not defined, skipping..."
  end

  File.open(src_path)
end
content_type() click to toggle source
# File lib/paperclip-tinify.rb, line 27
def content_type
  first_processor? ? @file.content_type : Paperclip::ContentTypeDetector.new(@file.path).detect
end
environments() click to toggle source
# File lib/paperclip-tinify.rb, line 19
def environments
  Tinify.environments || [:production]
end
first_processor?() click to toggle source
# File lib/paperclip-tinify.rb, line 23
def first_processor?
  @first_processor ||= @file.is_a?(Paperclip::AbstractAdapter)
end