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