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

Class Method Details

.available?Boolean

Checks if jpegoptim is available.

Returns:

  • (Boolean)

    true if it is, false in otherwise



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.

Parameters:

  • paths (String, Array)

    file path or array of paths for optimizing

  • options (Hash) (defaults to: { })

    options

  • block (Proc)

    block for giving back the results

Options Hash (options):

  • :strip (Boolean, Symbol)

    says what informations strip, see jpegoptim documentation, default is :all

  • :preserve (Boolean)

    turns on preserving the timestamps

  • :max (Integer)

    set maximum image quality factor

  • :debug (Boolean)

    turn on debugging mode, so command will be put out to the STDERR

Returns:



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, options = { }, &block)

    # Command
    cmd = CommandBuilder::new(self::COMMAND)
    
    # Strip definition
    strip = options[:strip]
    if strip.nil? or (strip == true)
        strip == :all
    end
    if strip
        cmd << ("strip-" << strip.to_s).to_sym
    end
    
    # Preserve
    if options[:preserve]
        cmd << :preserve
    end
    
    # Max
    if options[:max].kind_of? Integer
        cmd.arg(:max, options[:max].to_i)
    end
    
    # Files
    if paths.kind_of? String
        paths = [paths]
    end
    
    # Runs the command
    cmd << paths
    
    if options[: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