class Pronto::ImageOptim

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/pronto/image_optim.rb, line 6
def initialize(*args)
  super

  @image_optim = ::ImageOptim.new
end

Public Instance Methods

run() click to toggle source

Entry point to our Pronto runner

# File lib/pronto/image_optim.rb, line 13
def run
  return [] unless @patches

  @patches.select(&method(:valid_patch?))
          .map(&method(:inspect))
          .flatten
          .compact
end

Private Instance Methods

image_file?(path) click to toggle source
# File lib/pronto/image_optim.rb, line 44
def image_file?(path)
  %w[.png .jpg .jpeg].include?(File.extname(path))
end
inspect(patch) click to toggle source
# File lib/pronto/image_optim.rb, line 29
def inspect(patch)
  path = patch.new_file_full_path
  size = File.size(path)

  optimized_path = @image_optim.optimize_image(path)
  return unless optimized_path

  optimized_size = File.size(optimized_path)
  return unless optimized_size < size

  percent = 100 - (100.0 / size * optimized_size)

  Message.new(path, nil, :info, "File size can be reduced by #{percent.round(2)}%", nil, self.class)
end
valid_patch?(patch) click to toggle source
# File lib/pronto/image_optim.rb, line 24
def valid_patch?(patch)
  path = patch.new_file_full_path
  image_file?(path)
end