Zoffix Znet

NAME

POE::Component::CSS::Minifier - non-blocking wrapper around CSS::Minifier with URI fetching abilities

SYNOPSIS

    use strict;
    use warnings;

    use POE qw/Component::CSS::Minifier/;

    my $poco = POE::Component::CSS::Minifier->spawn;

    POE::Session->create( package_states => [ main => [qw(_start results)] ], );

    $poe_kernel->run;

    sub _start {
        $poco->minify(
                event   => 'results',
                uri     => 'http://zoffix.com/main.css',
                outfile => 'out.css',
            }
        );
    }

    sub results {
        if ( $_[ARG0]->{error} ) {
            print "Error: $_[ARG0]->{error}\n";
        }
        else {
            print "Minified ito $_[ARG0]->{outfile}\n";
        }
        $poco->shutdown;
    }

Using event based interface is also possible of course.

DESCRIPTION

The module is a non-blocking wrapper around CSS::Minifier, which provides interface to strip useless spaces from CSS code. The wrapper also provides additional functionality to fetch CSS from URI.

CONSTRUCTOR

spawn

    my $poco = POE::Component::CSS::Minifier->spawn;

    POE::Component::CSS::Minifier->spawn(
        alias => 'mini',
        ua_args => { timeout => 30 },
        options => {
            debug => 1,
            trace => 1,
            # POE::Session arguments for the component
        },
        debug => 1, # output some debug info
    );

The spawn method returns a POE::Component::CSS::Minifier object. It takes a few arguments, all of which are optional. The possible arguments are as follows:

alias

    ->spawn( alias => 'mini' );

Optional. Specifies a POE Kernel alias for the component.

ua_args

    ->spawn( ua_args => { timeout => 30 }, );

Optional. Takes a hashref as an argument that will be directly dereferenced into LWP::UserAgent's constructor when uri argument in minify event/method is used as input. Defaults to: empty hashref.

options

    ->spawn(
        options => {
            trace => 1,
            default => 1,
        },
    );

Optional. A hashref of POE Session options to pass to the component's session.

debug

    ->spawn(
        debug => 1
    );

When set to a true value turns on output of debug messages. Defaults to: 0.

METHODS

minify

    $poco->minify( {
            event       => 'event_for_output',
            uri         => 'http://zoffix.com/main.css',
            outfile     => 'minified.css',
            _blah       => 'pooh!',
            session     => 'other',
        }
    );

Takes a hashref as an argument, does not return a sensible return value. See minify event's description for more information.

session_id

    my $poco_id = $poco->session_id;

Takes no arguments. Returns component's session ID.

shutdown

    $poco->shutdown;

Takes no arguments. Shuts down the component.

ACCEPTED EVENTS

minify

    $poe_kernel->post( mini => minify => {
            event       => 'event_for_output',
            uri         => 'http://zoffix.com/main.css',
            outfile     => 'minified.css', # optional
            _blah       => 'pooh!',
            session     => 'other',
        }
    );

    # or

    $poe_kernel->post( mini => minify => {
            event       => 'event_for_output',
            infile      => 'in.css',
            outfile     => 'minified.css', # optional
            _blah       => 'pooh!',
            session     => 'other',
        }
    );

    # or

    $poe_kernel->post( mini => minify => {
            event       => 'event_for_output',
            in          => 'div:hover { border: 1px solid #000; }',
            outfile     => 'minified.css', # optional
            _blah       => 'pooh!',
            session     => 'other',
        }
    );

Instructs the component to strip useless spaces from CSS code. Takes a hashref as an argument, the possible keys/value of that hashref are as follows:

event

    { event => 'results_event', }

Mandatory. Specifies the name of the event to emit when results are ready. See OUTPUT section for more information.

in

    { in => 'div:hover { border: 1px solid #000; }' }

Optional. One of the methods to give input. Takes a string with CSS code to "minify" as an argument.

uri

    { uri => 'http://zoffix.com/main.css' }

Optional. One of the methods to give input. Takes a URI as a value that will be fetched and serve as CSS code to "minify".

infile

    { infile => 'in.css' }

Optional. One of the methods to give input. Takes a filename as an argument. The file will be opened and the filehandle will be given to minify() function of CSS::Minifier to serve as CSS code to "minify".

outfile

    { outfile => 'minified.css' }

Optional. When specified, the "minified" CSS code will be written to the file, filename of which you specify in outfile argument. Note: file will be created if does not exist and completely destroyed without a warning if it does exist. If not specified the minified CSS code will be passed to the output event handler (see below).

session

    { session => 'other' }

    { session => $other_session_reference }

    { session => $other_session_ID }

Optional. Takes either an alias, reference or an ID of an alternative session to send output to.

user defined

    {
        _user    => 'random',
        _another => 'more',
    }

Optional. Any keys starting with _ (underscore) will not affect the component and will be passed back in the result intact.

shutdown

    $poe_kernel->post( mini => 'shutdown' );

Takes no arguments. Tells the component to shut itself down.

OUTPUT

    $VAR1 = {
        'out' => 'div:hover{border:1px solid #000;}',
        'in' => 'div:hover { border: 1px solid #000; }',
        '_blah' => 'foos'
    };

The event handler set up to handle the event which you've specified in the event argument to minify() method/event will receive input in the $_[ARG0] in a form of a hashref. The possible keys/value of that hashref are as follows:

out

    { 'out' => 'div:hover{border:1px solid #000;}', }

Unless outfile was specified as an argument to minify event/method, the out key will be present and its value will be minified CSS code.

error

    { 'error' => 'infile error [No such file or directory]' }

If an error occurred, the error key will be present and its value will be description of an error. The error could be errors during opening input or output files as well as network errors when uri argument was given to minify event/method.

in, uri and infile

    { 'in' => 'div:hover { border: 1px solid #000; }', }

Depending on how you are giving the input to minify event/method, the key you specify in minify event/method will be present in the output.

user defined

    { '_blah' => 'foos' }

Any arguments beginning with _ (underscore) passed into the minify() event/method will be present intact in the result.

SEE ALSO

POE, CSS::Minifier

REPOSITORY

Fork this module on GitHub: https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment

BUGS

To report bugs or request features, please use https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment/issues

If you can't access GitHub, you can email your request to bug-POE-Component-Bundle-WebDevelopment at rt.cpan.org

AUTHOR

Zoffix Znet <zoffix at cpan.org> (http://zoffix.com/, http://haslayout.net/)

LICENSE

You can use and distribute this module under the same terms as Perl itself. See the LICENSE file included in this distribution for complete details.