Module: Jpegoptim
- Defined in:
- lib/jpegoptim.rb
Overview
The jpegoptim
tool command frontend.
Defined Under Namespace
Classes: Result
Constant Summary collapse
- COMMAND =
Holds
jpegoptim
command. :jpegoptim
- MATCHERS =
Holds output matchers.
[ /(.*)\[(ERROR)\]/, /jpegoptim:\s*(.*)/, /(.*)\s+\d+x\d+.*\((\-?\d+\.\d+)%\)/ ]
Class Method Summary collapse
-
.available? ⇒ Boolean
Checks if
jpegoptim
is available. -
.optimize(paths, options = { }, &block) ⇒ Struct
Performs optimizations above file or set of files.
Class Method Details
.available? ⇒ Boolean
Checks if jpegoptim
is available.
58 59 60 |
# File 'lib/jpegoptim.rb', line 58 def self.available? return Whereis.available? self::COMMAND end |
.optimize(paths, options = { }, &block) ⇒ Struct
Performs optimizations above file or set of files.
Destination directory option isn’t supported, so you are purely responsible for optimizing files on the right place. Use Ruby methods for it.
If block is given, runs jpegoptim
asynchronously. In that case, em-pipe-run
file must be already required.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/jpegoptim.rb', line 82 def self.optimize(paths, = { }, &block) # Command cmd = CommandBuilder::new(self::COMMAND) # Strip definition strip = [:strip] if strip.nil? or (strip == true) strip == :all end if strip cmd << ("strip-" << strip.to_s).to_sym end # Preserve if [:preserve] cmd << :preserve end # Max if [:max].kind_of? Integer cmd.arg(:max, [:max].to_i) end # Files if paths.kind_of? String paths = [paths] end # Runs the command cmd << paths if [:debug] == true STDERR.write cmd.to_s + "\n" end # Blocking if block.nil? output = cmd.execute! # Parses output succeed, errors = __parse_output(output) return self::Result::new(succeed, errors) # Non-blocking else cmd.execute do |output| succeed, errors = __parse_output(output) block.call(self::Result::new(succeed, errors)) end end end |