File Coverage

blib/lib/Articulate/Response.pm
Criterion Covered Total %
statement 21 23 91.3
branch 4 8 50.0
condition 3 6 50.0
subroutine 6 7 85.7
pod 2 2 100.0
total 36 46 78.2


line stmt bran cond sub pod time code
1             package Articulate::Response;
2 9     9   45 use strict;
  9         13  
  9         370  
3 9     9   44 use warnings;
  9         47  
  9         260  
4              
5 9     9   46 use Moo;
  9         18  
  9         67  
6 9     9   2437 use Scalar::Util qw(blessed);
  9         17  
  9         594  
7              
8 9     9   51 use Exporter::Declare;
  9         11  
  9         49  
9             default_exports qw(response);
10              
11             =head1 NAME
12              
13             Articulate::Response - represent a response
14              
15             =cut
16              
17             =head1 FUNCTIONS
18              
19             =head3 response
20              
21             =head3 articulate_response
22              
23             my $response = response type => $data;
24              
25             Creates a new response, using the type and data supplied as the respective arguments. Using this constructor, the error code will be 200 unless C is C.
26              
27             =cut
28              
29             sub response {
30 4     4 1 33 my ($type, $data) = @_;
31 4 50       15 my $http_code =
32             $type eq 'error'
33             ? 500
34             : 200;
35 4 50       16 return __PACKAGE__->new( {
36             type => $type,
37             http_code => $http_code,
38             } ) unless defined $data;
39 4 50       16 if (ref $data eq ref {}) {
40 4 50 66     35 if (
      33        
41             defined $data->{$type}
42             and blessed $data->{$type}
43             and $data->{$type}->can('http_code')
44             ) {
45 0         0 $http_code = $data->{$type}->http_code;
46             }
47             }
48 4         125 return __PACKAGE__->new( {
49             type => $type,
50             data => $data,
51             http_code => $http_code,
52             } );
53             };
54              
55             =head1 METHODS
56              
57             =head3 new
58              
59             An unremarkable Moo constructor.
60              
61             =cut
62              
63             =head3 serialise
64              
65             Sends the response to Articulate::Serialisation->serialise.
66              
67             Note: the behaviour of this method may change!
68              
69             =cut
70              
71             sub serialise { # this is convenient as it is probably the next thing which will always be done.
72             # Articulate::Serialisation::serialisation->serialise (shift);
73 0     0 1   shift;
74             }
75              
76              
77             =head1 ATTRIBUTES
78              
79             =head3 http_code
80              
81             The HTTP response code which best applies to the response. The default is 500.
82              
83             It is not guaranteed that this will be passed to the ultimate client (e.g. a later error may cause a 500; the service may be accessed in a way other than HTTP).
84              
85             =cut
86              
87             has http_code =>
88             is => 'rw',
89             default => 500,
90             coerce => sub { 0+shift };
91              
92              
93             =head3 type
94              
95             The type of response, which will be used by serialisers etc. to determine how to complete processing (e.g. which template to use).
96              
97             =cut
98              
99             has type =>
100             is => 'rw',
101             default => sub { 'error' };
102              
103             =head3 data
104              
105             The actual content of the response, including any metadata. Typically this will be of the form
106              
107             {
108             item => {
109             article => {
110             meta => { ... }
111             content => " ... "
112             }
113             }
114             }
115              
116             =cut
117              
118             has data =>
119             is => 'rw',
120             default => sub { { } };
121              
122             1;