File Coverage

blib/lib/HTTP/Throwable/Role/JSONBody.pm
Criterion Covered Total %
statement 14 16 87.5
branch 2 2 100.0
condition n/a
subroutine 5 6 83.3
pod 0 3 0.0
total 21 27 77.7


line stmt bran cond sub pod time code
1             package HTTP::Throwable::Role::JSONBody;
2             # ABSTRACT - JSON Body
3             $HTTP::Throwable::Role::JSONBody::VERSION = '0.002';
4 1     1   9935 use Moo::Role;
  1         2  
  1         4  
5 1     1   556 use JSON::MaybeXS;
  1         796  
  1         119  
6              
7             has payload => (
8             is => 'ro',
9             );
10              
11             sub body {
12 2     2 0 7667 my $self = shift;
13              
14             # Preempt bad clients that can't handle application/json with empty
15             # body
16 2 100       8 return "{}" unless $self->payload;
17              
18 1         15 return encode_json($self->payload);
19             }
20              
21             sub body_headers {
22 2     2 0 10 my ($self, $body) = @_;
23              
24             return [
25 2         6 'Content-Type' => 'application/json',
26             'Content-Length' => length $body,
27             ];
28             }
29              
30             sub as_string {
31 0     0 0   my $self = shift;
32              
33 0           return $self->body;
34             }
35              
36 1     1   5 no Moo::Role; 1;
  1         1  
  1         4  
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             HTTP::Throwable::Role::JSONBody
45              
46             =head1 VERSION
47              
48             version 0.002
49              
50             =head1 OVERVIEW
51              
52             This role does two things - accepts an optional C argument that
53             should be anything you can pass to L, and then encodes
54             it as the body, specifying a C of C. If no
55             C is provided, the body will be C<{}>.
56              
57             =head1 AUTHOR
58              
59             Matthew Horsfall
60              
61             =head1 COPYRIGHT AND LICENSE
62              
63             This software is copyright (c) 2016 by Matthew Horsfall.
64              
65             This is free software; you can redistribute it and/or modify it under
66             the same terms as the Perl 5 programming language system itself.
67              
68             =cut
69              
70             __END__