| 1 | package Articulate::Response; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Exporter::Declare; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | 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<type> is C<error>. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | sub new_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_response: called CC=7 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | my ( $type, $data ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | my $http_code = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | $type eq 'error' | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ? 500 | ||||||||||||||||||||||||||||||||||||||||||
| 36 | : 200; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | return __PACKAGE__->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | http_code => $http_code, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ) unless defined $data; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | if ( ref $data eq ref {} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | if ( defined $data->{$type} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $data->{$type} and builtin::blessed($data->{$type}) and $data->{$type}->can("http_code")
defined $data->{$type} and builtin::blessed($data->{$type})
MC/DC0%0/3
defined $data->{$type} and builtin::blessed($data->{$type}) and $data->{$type}->can("http_code")
defined $data->{$type}
builtin::blessed($data->{$type})
$data->{$type}->can("http_code")
Truth table50%2/4
A and B and C
A= defined $data->{$type}B= builtin::blessed($data->{$type})C= $data->{$type}->can("http_code")
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | and blessed $data->{$type} | ||||||||||||||||||||||||||||||||||||||||||
| 46 | and $data->{$type}->can('http_code') ) | ||||||||||||||||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $http_code = $data->{$type}->http_code; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | 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 | 0 | ▶ | { # this is convenient as it is probably the next thing which will always be done. | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialise: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # Articulate::Serialisation::serialisation->serialise (shift); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | 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 | 4 | ▶ | coerce => sub { 0 + shift } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 11 | ); | |||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | default => sub { 'error' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 11 | ); | |||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | default => sub { {} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 11 | ); | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||