Kelp

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.2191 - 2013-05-14
            Add arguments to load_module
            Fix tests and small issues

0.2190 - 2013-05-05
            Add property for partial rendering
            Address an issue with nginx and headers
            Add more pod

0.2182 - 2013-05-02
            Fix issue with content-type params
            Set explicit utf8 encoding for module Template
            Replace wizardly Perl code with Class::Inspector

0.2181 - 2013-04-20
            Fix test failing under Perl 5.10

0.218 - 2013-04-19
            Add auto load class when adding a route.

0.217 - 2013-04-17

lib/Kelp/Manual.pod  view on Meta::CPAN

    }

=head3 Templates

    sub hello {
        my ( $self, $name ) = @_;
        $self->res->template( 'hello.tt', { name => $name } );
    }

The above example will render the contents of C<hello.tt>, and it will set the
content-type to C<text/html>. To set a different content-type, use
C<set_content_type> or any of its aliases:

    sub hello_txt {
        my ( $self, $name ) = @_;
        $self->res->text->template( 'hello_txt.tt', { name => $name } );
    }

You can also simply get template string and return it, which will work the same:

    sub hello {

lib/Kelp/Request.pm  view on Meta::CPAN


# The named hash contains the values of the named placeholders
attr named => sub { {} };

# charset which came with the request
attr -charset => sub {
    my $self = shift;

    return undef unless $self->content_type;
    return undef unless $self->content_type =~ m{
        ^(?:                  # only on some content-types
            text/ | application/
        )
        .+
        ;\s*charset=([^;\$]+) # get the charset
    }xi;
    return $1;
};

# The name of the matched route for this request
attr route_name => sub { undef };

lib/Kelp/Response.pm  view on Meta::CPAN

is does:

=over

=item

If the response code was not previously set, this method will set it to 200.

=item

If no content-type is previously set, C<render> will set is based on the type
of the data rendered. If it's a reference, then the content-type will be set to
C<application/json>, otherwise it will be set to C<text/html>.

    # Will set the content-type to json
    $res->render({ numbers => [1, 2, 3] });

=item

Last, the data will be encoded with the charset from L</charset> or the one
specified by the app - see L<Kelp/charset>. Any string you pass here should not
already be encoded, unless your application has its charset set to undef.

=back

lib/Kelp/Response.pm  view on Meta::CPAN


    # Inside a route definition
    $self->res->set_content_type('image/png');

An optional second argument can be passed, which will be used for C<charset>
part of C<Content-Type> (will set L</charset> field).

=head2 text, html, json, xml

These methods are shortcuts for C<set_content_type> with the corresponding type.
All of them set the content-type header and return C<$self> so they can be
chained.

    $self->res->text->render("word");
    $self->res->html->render("<p>word</p>");
    $self->res->json->render({ word => \1 });

NOTE: These methods will also call L</charset> and set it to application's
charset (unless it was previously set).

=head2 set_header

lib/Kelp/Test.pm  view on Meta::CPAN

    };
    return $result;
}

sub json_cmp
{
    my ($self, $expected, $test_name) = @_;
    local $Test::Builder::Level = $Test::Builder::Level + 1;

    $test_name ||= "JSON structure matches";
    like $self->res->header('content-type'), qr/json/, 'Content-Type is JSON'
        or return $self;
    my $json = $self->json_content;
    cmp_deeply($json, $expected, $test_name) or diag explain $json;
    return $self;
}

sub note
{
    my $self = shift;
    Test::More::note @_;

lib/Kelp/Test.pm  view on Meta::CPAN

Tests if the last response returned content that matches or doesn't match C<$regexp>.
An optional name of the test can be added as a second parameter.

    $t->request( GET '/path' )->content_like(qr{Amsterdam});
    $t->request( GET '/path' )->content_unlike(qr{Rotterdam});

=head2 content_type_is, content_type_isnt

C<content_type_is( $value, $test_name )>, C<content_type_isnt( $value, $test_name )>

Tests if the last response's content-type header is equal or not equal to C<$value>.
An optional name of the test can be added as a second parameter.

    $t->request( GET '/path' )->content_type_is("text/plain");
    $t->request( GET '/path' )->content_type_isnt("text/html");

=head2 full_content_type_is

Like L</content_type_is>, but checks the full content type (with charset).

=head2 header_is, header_isnt

lib/Kelp/Test.pm  view on Meta::CPAN


C<json_content()>

Returns the content decoded as JSON. Does not perform any checks, but may
C<fail()> and return C<undef> if the JSON decoding fails.

=head2 json_cmp

C<json_cmp( $expected, $test_name )>

This tests for two things: If the returned C<content-type> is
C<application-json>, and if the returned JSON structure matches the structure
specified in C<$expected>. To compare the two structures this method uses
C<cmp_deeply> from L<Test::Deep>, so you can use all the goodies from the
C<SPECIAL-COMPARISONS-PROVIDED> section of the Test::Deep module.

    $t->request( GET '/api' )->json_cmp(
        {
            auth      => 1,
            timestamp => ignore(),
            info      => subhashof( { name => 'Rick James' } )



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