MVC-Neaf

 view release on metacpan or  search on metacpan

lib/MVC/Neaf.pm  view on Meta::CPAN

MVC::Neaf - Not Even A (Web Application) Framework

=head1 OVERVIEW

Neaf C<[ni:f]> stands for Not Even A Framework.

The B<Model> is assumed to be just a regular Perl module,
no restrictions are imposed on it.

The B<View> is an object with one method, C<render>, receiving a hashref
and returning rendered content as string plus optional content-type header.

The B<Controller> is a prefix tree of subroutines called I<handlers>.
Each such handler receives a L<MVC::Neaf::Request> object
containing I<all> it needs to know about the outside world,
and returns a simple C<\%hashref> which is forwarded to View.

Alternatively, it can die. C<die 404> is a valid way to return
a customizable "404 Not Found" page.

Please see the C<example> directory in this distribution

lib/MVC/Neaf.pm  view on Meta::CPAN


If parameter or cookie is missing, just return default.
This MAY change in the future.

B<[EXPERIMENTAL]> Name and meaning MAY change in the future.

=item * C<view> - default View object for this Controller.
Must be a name of preloaded view,
an object with a C<render> method, or a CODEREF
receiving hashref and returning a list of two scalars
(content and content-type).

B<[DEPRECATED]> Use C<-view> instead, meaning is exactly the same.

=item * C<cache_ttl> - if set, set Expires: HTTP header accordingly.

B<[EXPERIMENTAL]> Name and semantics MAY change in the future.

=item * C<default> - a C<\%hash> of fallback values to be added to hash
returned by the handler.
Consider using C<neaf default ...> below if you need to append

lib/MVC/Neaf.pm  view on Meta::CPAN

Short aliases C<JS>, C<TT>, and C<Dumper> may be used
for corresponding C<MVC::Neaf::View::*> modules.

The templates that allow for paths
(i.e. currently just L<MVC::Neaf::View::TT>)
will have non-absolute paths calculated relative to the file where
static() was called, not to the current directory.

=item * if coderef is given, use it as a C<render> method.
The coderef must take 1 argument - the hash returned from application -
and return a string + optional content-type.

=back

Returns the view object, NOT the calling Neaf object.

=cut

=head2 set_session_handler()

    neaf session => $engine => %options

lib/MVC/Neaf.pm  view on Meta::CPAN


=item * method - set method (default is GET)

=item * cookie = \%hash - force HTTP_COOKIE header

=item * header = \%hash - override some headers
This gets overridden by type, cookie etc. in case of conflict

=item * body = 'DATA' - force body in request

=item * type - content-type of body

=item * uploads - a hash of L<MVC::Neaf::Upload> objects.

=item * secure = 0|1 - C<http> vs C<https>

=item * override = \%hash - force certain data in C<ENV>
Gets overridden by all of the above.

=back

lib/MVC/Neaf/Route/Main.pm  view on Meta::CPAN


If parameter or cookie is missing, just return default.
This MAY change in the future.

B<[EXPERIMENTAL]> Name and meaning MAY change in the future.

=item * C<view> - default View object for this Controller.
Must be a name of preloaded view,
an object with a C<render> method, or a CODEREF
receiving hashref and returning a list of two scalars
(content and content-type).

B<[DEPRECATED]> Use C<-view> instead, meaning is exactly the same.

=item * C<cache_ttl> - if set, set Expires: HTTP header accordingly.

B<[EXPERIMENTAL]> Name and semantics MAY change in the future.

=item * C<default> - a C<\%hash> of values that will be added to results
EVERY time the handler returns.
Consider using C<neaf default ...> below if you need to append

lib/MVC/Neaf/Route/Main.pm  view on Meta::CPAN


    delete $self->{route_re};
    $self->{route}{ $path }{$_} = MVC::Neaf::Route->new( %$profile, method => $_ )
        for @real_method;
};

=head2 static()

    $neaf->static( '/path' => $local_path, %options );

    $neaf->static( '/other/path' => [ "content", "content-type" ] );

Serve static content located under C<$file_path>.
Both directories and single files may be added.

If an arrayref of C<[ $content, $content_type ]> is given as second argument,
serve content from memory instead.

%options may include:

=over

lib/MVC/Neaf/Route/Main.pm  view on Meta::CPAN


=item * method - set method (default is GET)

=item * cookie = \%hash - force HTTP_COOKIE header

=item * header = \%hash - override some headers
This gets overridden by type, cookie etc. in case of conflict

=item * body = 'DATA' - force body in request

=item * type - content-type of body

=item * uploads - a hash of L<MVC::Neaf::Upload> objects.

=item * secure = 0|1 - C<http> vs C<https>

=item * override = \%hash - force certain data in C<ENV>
Gets overridden by all of the above.

=back

lib/MVC/Neaf/X/Files.pm  view on Meta::CPAN

};

=head2 serve_file( $path )

Create a Neaf-compatible response using given path.
The response is like follows:

    {
        -content => (file content),
        -headers => (length, name etc),
        -type => (content-type),
        -continue => (serve the rest of the file, if needed),
    };

Will C<die 404;> if file is not there.

This MAY be used to create more fine-grained control over static files.

B<EXPERIMENTAL>. New options MAY be added.

=cut

t/225-Error-with-id.t  view on Meta::CPAN


{
    undef $capture;
    my @warn;
    local $SIG{__WARN__} = sub { push @warn, shift };

    my ($st, $head, $content) = neaf->run_test( "/foo" );
    my $id = $capture->id;

    is $st, 500, "Status 500 if died";
    like $head->header("content-type"), qr(^text/html\b), "HTML in reply";

    like $content, qr(<span>500</span>), "Status preserved";
    like $content, qr(<b>\Q$id\E</b>), "Id sent to user";

    is scalar @warn, 1, "1 warning issued";
    like $warn[0], qr/\Q$id\E/, "req_id in log";

    note "WARN: $_" for @warn;
}

{
    undef $capture;
    my @warn;
    local $SIG{__WARN__} = sub { push @warn, shift };

    my ($st, $head, $content) = neaf->run_test( "/tpl" );
    my $id = $capture->id;

    is $st, 500, "Status 500 if died";
    like $head->header("content-type"), qr(^text/html\b), "HTML in reply";

    like $content, qr(<span>500</span>), "Status preserved";
    like $content, qr(<b>\Q$id\E</b>), "Id sent to user";
    # TODO 0.25 must also explain reason via Exception
    # like $ref->{reason}, qr/render/i, "Rendering error or smth";

    is scalar @warn, 1, "1 warning issued";
    like $warn[0], qr/\Q$id\E/, "req_id in log";

    note $content;

t/230-preload-resource.t  view on Meta::CPAN

warnings_like {
    neaf->load_resources(\*DATA);
} [{ carped => qr/Xslate/ }], "Xslate hasn't been loaded = warn";

my ($status, $head, $content) = neaf->run_test( '/render?tpl=foo.html' );
is $status, 200, "Template found now";
is $content, 'FOO=42', "Rendered as expected";

($status, $head, $content) = neaf->run_test( '/favicon.ico' );
is $status, 200, "Static found";
is $head->header("content-type"), 'image/png', "Content-type survived";

($status, $head, $content) = neaf->run_test( '/render?tpl=base64.html' );
is $status, 200, "Status ok";
is $content, "<b>so?</b>", "Template decoded (base64)";

# NOTE PGI+c28/PC9iPg== is base64 encoding of "<b>so?</b>".
#    ">" and "?" are 62nd and 63rd symbols in ascii, respectively
#    They are put at the end of a triad to guarantee their last 6 bits
#    get encoded in one "digit".
#    Thus, correct decoding of base64 is proven.

t/231-static-mem.t  view on Meta::CPAN


use MVC::Neaf;

neaf static => '/foo' => [ 'Food Bard', 'text/lame' ];

my ($status, $head, $content) = neaf->run_test( '/foo' );

is   $status,   200,        "Found file";
is   $content, 'Food Bard', "Content survived";

like $head->header("content-type"), qr(text/lame), "Type survived";

done_testing;



( run in 1.946 second using v1.01-cache-2.11-cpan-d7f47b0818f )