File Coverage

blib/lib/Dancer2/Core/Role/StandardResponses.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 18 18 100.0


line stmt bran cond sub pod time code
1             package Dancer2::Core::Role::StandardResponses;
2             # ABSTRACT: Role to provide commonly used responses
3             $Dancer2::Core::Role::StandardResponses::VERSION = '1.0.0';
4 146     146   83377 use Moo::Role;
  146         439  
  146         1014  
5 146     146   61558 use Dancer2::Core::HTTP;
  146         467  
  146         22786  
6              
7             sub response {
8 2     2 1 33599 my ( $self, $app, $code, $message ) = @_;
9 2         12 $app->response->status($code);
10 2         1791 $app->response->header( 'Content-Type', 'text/plain' );
11 2         2778 return $message;
12             }
13              
14             sub standard_response {
15 1     1 1 911 my ( $self, $app, $status_code ) = @_;
16              
17 1         10 return $self->response(
18             $app,
19             $status_code,
20             Dancer2::Core::HTTP->status_message($status_code),
21             );
22             }
23              
24             1;
25              
26             __END__
27              
28             =pod
29              
30             =encoding UTF-8
31              
32             =head1 NAME
33              
34             Dancer2::Core::Role::StandardResponses - Role to provide commonly used responses
35              
36             =head1 VERSION
37              
38             version 1.0.0
39              
40             =head1 METHODS
41              
42             =head2 response
43              
44             Generic method that produces a custom response given with a code and a message:
45              
46             $self->response( $app, 404, 'Not Found' );
47              
48             This could be used to create your own, which is separate from the standard one:
49              
50             $self->response( $app, 404, 'File missing in action' );
51              
52             =head2 standard_response
53              
54             Produces a standard response using the code.
55              
56             # first example can be more easily written as
57             $self->standard_response( $app, 404 );
58              
59             =head1 AUTHOR
60              
61             Dancer Core Developers
62              
63             =head1 COPYRIGHT AND LICENSE
64              
65             This software is copyright (c) 2023 by Alexis Sukrieh.
66              
67             This is free software; you can redistribute it and/or modify it under
68             the same terms as the Perl 5 programming language system itself.
69              
70             =cut