| 1 | package Net::HTTP::Knork::Response; | ||
| 2 | |||
| 3 | # ABSTRACT: Portable HTTP Response object for SPORE response | ||
| 4 | 6 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 6 | extends 'HTTP::Response'; | |
| 6 | |||
| 7 | use overload | ||
| 8 | 6 | '@{}' => \&finalize, | |
| 9 | '""' => \&to_string, | ||
| 10 | 6 | ▶ | fallback => 1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | |||
| 13 | has 'body' => ( | ||
| 14 | is => 'rw', | ||
| 15 | lazy => 1, | ||
| 16 | builder => sub { | ||
| 17 | 0 | ▶ | return $_[0]->content; |
|
Subroutine
_build_body: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 18 | }, | ||
| 19 | trigger => sub { | ||
| 20 | 0 | ▶ | my ( $self, $body_new_value ) = @_; |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 21 | 0 | $self->raw_body($body_new_value); | |
| 22 | } | ||
| 23 | 6 | ); | |
| 24 | |||
| 25 | 6 | has 'raw_body' => ( | |
| 26 | is => 'rw', | ||
| 27 | ); | ||
| 28 | |||
| 29 | 6 | has 'request' => ( | |
| 30 | is => 'rw', | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub FOREIGNBUILDARGS { | ||
| 34 | 13 | ▶ | my $class = shift; |
|
Subroutine
FOREIGNBUILDARGS: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 35 | 13 | my ( $rc, $message, $headers, $body ) = @_; | |
| 36 | 13 | return ( $rc, $message, $headers, $body ); | |
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | 0 | ▶ | sub env { shift->request->env } |
|
Subroutine
env: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 41 | 8 | ▶ | sub content_type { shift->headers->content_type(@_) } |
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
documented
| |||
| 42 | 8 | ▶ | sub content_length { shift->headers->content_length(@_) } |
|
Subroutine
content_length: called CC=1 SCAR=0.0
Pod
documented
| |||
| 43 | 0 | ▶ | sub location { shift->headers->header( 'Location' => @_ ) } |
|
Subroutine
location: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 44 | 8 | ▶ | sub header { shift->headers->header(@_) } |
|
Subroutine
header: called CC=1 SCAR=0.0
Pod
documented
| |||
| 45 | 8 | ▶ | sub to_string { shift->as_string } |
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 46 | 0 | ▶ | sub status { shift->code(@_) } |
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 47 | |||
| 48 | |||
| 49 | sub finalize { | ||
| 50 | 0 | ▶ | my $self = shift; |
|
Subroutine
finalize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 51 | return [ | ||
| 52 | $self->status, | ||
| 53 | +[ map { | ||
| 54 | 0 | my $k = $_; | |
| 55 | 0 | map { ( $k => $_ ) } $self->headers->header($_); | |
| 56 | } $self->headers->header_field_names | ||
| 57 | ], | ||
| 58 | $self->body, | ||
| 59 | ]; | ||
| 60 | } | ||
| 61 | |||
| 62 | 6 | 1; | |
| 63 | |||
| 64 | |||
| 65 | =pod | ||
| 66 | |||
| 67 | =head1 NAME | ||
| 68 | |||
| 69 | Net::HTTP::Knork::Response - Portable HTTP Response object for SPORE response | ||
| 70 | |||
| 71 | =head1 VERSION | ||
| 72 | |||
| 73 | version 0.11 | ||
| 74 | |||
| 75 | =head1 SYNOPSIS | ||
| 76 | |||
| 77 | use Net:HTTP::Knork::Response; | ||
| 78 | |||
| 79 | my $response = Net::HTTP::Knork::Response->new( | ||
| 80 | 200, ['Content-Type', 'application/json'], '{"foo":1}'; | ||
| 81 | ); | ||
| 82 | $response->request($request); | ||
| 83 | |||
| 84 | =head1 DESCRIPTION | ||
| 85 | |||
| 86 | Net::HTTP::Knork::Response : create a HTTP response | ||
| 87 | Most of the code was adapted from Net::HTTP::Spore::Response, with two main differences : | ||
| 88 | - it uses Moo | ||
| 89 | - it is a subclass of HTTP::Response | ||
| 90 | |||
| 91 | =head1 METHODS | ||
| 92 | |||
| 93 | =over 4 | ||
| 94 | |||
| 95 | =item new | ||
| 96 | |||
| 97 | my $res = Net::HTTP::Knork::Response->new; | ||
| 98 | my $res = Net::HTTP::Knork::Response->new($status); | ||
| 99 | my $res = Net::HTTP::Knork::Response->new($status, $message, $headers); | ||
| 100 | my $res = Net::HTTP::Knork::Response->new($status, $message, $headers, $body); | ||
| 101 | |||
| 102 | Creates a new Net::HTTP::Knork::Response object. | ||
| 103 | |||
| 104 | =item code | ||
| 105 | |||
| 106 | =item status | ||
| 107 | |||
| 108 | $res->status(200); | ||
| 109 | my $status = $res->status; | ||
| 110 | |||
| 111 | Gets or sets the HTTP status of the response | ||
| 112 | |||
| 113 | =item env | ||
| 114 | $res->env($env); | ||
| 115 | my $env = $res->env; | ||
| 116 | |||
| 117 | Gets or sets the environment for the response. Shortcut to C<< $res->request->env >> | ||
| 118 | |||
| 119 | =item content | ||
| 120 | |||
| 121 | =item body | ||
| 122 | |||
| 123 | $res->body($body); | ||
| 124 | my $body = $res->body; | ||
| 125 | |||
| 126 | Gets or sets the body for the response | ||
| 127 | |||
| 128 | =item raw_body | ||
| 129 | |||
| 130 | my $raw_body = $res->raw_body | ||
| 131 | |||
| 132 | The raw_body value is the same as body when the body is sets for the first time. | ||
| 133 | |||
| 134 | =item content_type | ||
| 135 | |||
| 136 | $res->content_type('application/json'); | ||
| 137 | my $ct = $res->content_type; | ||
| 138 | |||
| 139 | Gets or sets the content type of the response body | ||
| 140 | |||
| 141 | =item content_length | ||
| 142 | |||
| 143 | $res->content_length(length($body)); | ||
| 144 | my $cl = $res->content_length; | ||
| 145 | |||
| 146 | Gets or sets the content type of the response body | ||
| 147 | |||
| 148 | =item location | ||
| 149 | |||
| 150 | $res->location('http://example.com'); | ||
| 151 | my $location = $res->location; | ||
| 152 | |||
| 153 | Gets or sets the location header for the response | ||
| 154 | |||
| 155 | =item request | ||
| 156 | |||
| 157 | $res->request($request); | ||
| 158 | $request = $res->request; | ||
| 159 | |||
| 160 | Gets or sets the HTTP request that created the current HTTP response. | ||
| 161 | |||
| 162 | =item headers | ||
| 163 | |||
| 164 | $headers = $res->headers; | ||
| 165 | $res->headers(['Content-Type' => 'application/json']); | ||
| 166 | |||
| 167 | Gets or sets HTTP response headers. | ||
| 168 | |||
| 169 | =item header | ||
| 170 | |||
| 171 | my $cl = $res->header('Content-Length'); | ||
| 172 | $res->header('Content-Type' => 'application/json'); | ||
| 173 | |||
| 174 | Shortcut for C<< $res->headers->header >>. | ||
| 175 | |||
| 176 | =item finalise | ||
| 177 | |||
| 178 | my $res = Net::HTTP::Knork::Response->new($status, $headers, $body); | ||
| 179 | say "http status is ".$res->[0]; | ||
| 180 | |||
| 181 | Return an arrayref: | ||
| 182 | |||
| 183 | =over 2 | ||
| 184 | |||
| 185 | =item status | ||
| 186 | |||
| 187 | The first element of the array ref is the HTTP status | ||
| 188 | |||
| 189 | =item headers | ||
| 190 | |||
| 191 | The second element is an arrayref containing the list of HTTP headers | ||
| 192 | |||
| 193 | =item body | ||
| 194 | |||
| 195 | The third and final element is the body | ||
| 196 | |||
| 197 | =back | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =head1 AUTHOR | ||
| 202 | |||
| 203 | Emmanuel Peroumalnaïk | ||
| 204 | |||
| 205 | =head1 COPYRIGHT AND LICENSE | ||
| 206 | |||
| 207 | This software is copyright (c) 2014 by E. Peroumalnaik. | ||
| 208 | |||
| 209 | This is free software; you can redistribute it and/or modify it under | ||
| 210 | the same terms as the Perl 5 programming language system itself. | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | |||
| 215 | __END__ | ||