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 10     10   45 use strict;
  10         13  
  10         317  
3 10     10   39 use warnings;
  10         11  
  10         210  
4              
5 10     10   35 use Moo;
  10         24  
  10         44  
6 10     10   2513 use Scalar::Util qw(blessed);
  10         14  
  10         759  
7              
8 10     10   53 use Exporter::Declare;
  10         14  
  10         52  
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 20 my ( $type, $data ) = @_;
31 4 50       10 my $http_code =
32             $type eq 'error'
33             ? 500
34             : 200;
35 4 50       10 return __PACKAGE__->new(
36             {
37             type => $type,
38             http_code => $http_code,
39             }
40             ) unless defined $data;
41 4 50       13 if ( ref $data eq ref {} ) {
42 4 50 66     28 if ( defined $data->{$type}
      33        
43             and blessed $data->{$type}
44             and $data->{$type}->can('http_code') )
45             {
46 0         0 $http_code = $data->{$type}->http_code;
47             }
48             }
49 4         87 return __PACKAGE__->new(
50             {
51             type => $type,
52             data => $data,
53             http_code => $http_code,
54             }
55             );
56             }
57              
58             =head1 METHODS
59              
60             =head3 new
61              
62             An unremarkable Moo constructor.
63              
64             =cut
65              
66             =head3 serialise
67              
68             Sends the response to Articulate::Serialisation->serialise.
69              
70             Note: the behaviour of this method may change!
71              
72             =cut
73              
74             sub serialise
75             { # this is convenient as it is probably the next thing which will always be done.
76              
77             # Articulate::Serialisation::serialisation->serialise (shift);
78 0     0 1   shift;
79             }
80              
81             =head1 ATTRIBUTES
82              
83             =head3 http_code
84              
85             The HTTP response code which best applies to the response. The default is 500.
86              
87             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).
88              
89             =cut
90              
91             has http_code => (
92             is => 'rw',
93             default => 500,
94             coerce => sub { 0 + shift }
95             );
96              
97             =head3 type
98              
99             The type of response, which will be used by serialisers etc. to determine how to complete processing (e.g. which template to use).
100              
101             =cut
102              
103             has type => (
104             is => 'rw',
105             default => sub { 'error' }
106             );
107              
108             =head3 data
109              
110             The actual content of the response, including any metadata. Typically this will be of the form
111              
112             {
113             item => {
114             article => {
115             meta => { ... }
116             content => " ... "
117             }
118             }
119             }
120              
121             =cut
122              
123             has data => (
124             is => 'rw',
125             default => sub { {} }
126             );
127              
128             1;