File Coverage

blib/lib/Trickster/Response.pm
Criterion Covered Total %
statement 30 38 78.9
branch n/a
condition 4 8 50.0
subroutine 7 9 77.7
pod 4 5 80.0
total 45 60 75.0


line stmt bran cond sub pod time code
1             package Trickster::Response;
2              
3 5     5   32 use strict;
  5         10  
  5         183  
4 5     5   22 use warnings;
  5         12  
  5         271  
5 5     5   60 use v5.14;
  5         25  
6              
7 5     5   24 use parent 'Plack::Response';
  5         10  
  5         29  
8              
9             sub json {
10 12     12 1 452 my ($self, $data, $status) = @_;
11            
12 12         94 require JSON::PP;
13 12         41 require Encode;
14            
15 12   100     69 $self->status($status || 200);
16 12         88 $self->content_type('application/json; charset=utf-8');
17            
18 12         354 my $json = JSON::PP::encode_json($data);
19 12         1663 $self->body(Encode::encode_utf8($json));
20            
21 12         95 return $self;
22             }
23              
24             sub html {
25 0     0 1 0 my ($self, $content, $status) = @_;
26            
27 0         0 require Encode;
28            
29 0   0     0 $self->status($status || 200);
30 0         0 $self->content_type('text/html; charset=utf-8');
31 0         0 $self->body(Encode::encode_utf8($content));
32            
33 0         0 return $self;
34             }
35              
36             sub text {
37 1     1 1 23 my ($self, $content, $status) = @_;
38            
39 1         9 require Encode;
40            
41 1   50     10 $self->status($status || 200);
42 1         10 $self->content_type('text/plain; charset=utf-8');
43 1         35 $self->body(Encode::encode_utf8($content));
44            
45 1         8 return $self;
46             }
47              
48             sub redirect {
49 1     1 1 53 my ($self, $location, $status) = @_;
50            
51 1   50     11 $self->status($status || 302);
52 1         16 $self->header('Location' => $location);
53 1         83 $self->body('');
54            
55 1         8 return $self;
56             }
57              
58             sub render {
59 0     0 0   my ($self, $template, $vars, $status) = @_;
60            
61             # This method requires a template engine to be set
62             # It will be called by the template engine's response helper
63 0           die "Template engine not configured. Use Trickster::Template->response_helper()";
64             }
65              
66             1;
67              
68             __END__