| 1 | package Articulate::Response; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Exporter::Declare; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 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<type> is C<error>. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | sub response { | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | my ($type, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response: called CC=7 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | my $http_code = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | $type eq 'error' | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ? 500 | ||||||||||||||||||||||||||||||||||||||||||
| 34 | : 200; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | return __PACKAGE__->new( { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | type => $type, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | http_code => $http_code, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | } ) unless defined $data; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | if (ref $data eq ref {}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | defined $data->{$type} | ||||||||||||||||||||||||||||||||||||||||||
| 42 | and blessed $data->{$type} | ||||||||||||||||||||||||||||||||||||||||||
| 43 | and $data->{$type}->can('http_code') | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $http_code = $data->{$type}->http_code; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | 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 | ▶ | shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialise: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 4 | ▶ | coerce => sub { 0+shift }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | default => sub { 'error' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | default => sub { { } }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||