blib/lib/Web/Request/Role/JSON.pm

filter: Statementstmt 100% 32 / 32 Branchbran 87.5% 7 / 8 Conditioncond 85.7% 6 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 93.4% 57 / 61 CC 8 SCAR 12.8
CC
8
Cov
96%
new_json_response
6.0
new_json_error
3.0
BEGIN
1.0
CRAP 3.6
« Build ↑ Summary
1 package Web::Request::Role::JSON;
2  
3 # ABSTRACT: Make handling JSON easier in Web::Request
4  
5 1 our $VERSION = '1.000';
6  
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub decoded_json_content {
13 5 ▶ my $self = shift;
Subroutine decoded_json_content: called CC=1 SCAR=0.0
Pod documented
14  
15 # Web::Request->content will decode content based on
16 # $req->encoding, which is utf8 for JSON. So $content has UTF8 flag
17 # on, which means we have to tell JSON::MaybeXS to turn
18 # utf8-handling OFF
19  
20 5 return JSON::MaybeXS->new(utf8=>0)->decode($self->content);
21  
22 # Alternatives:
23 # - reencode the content (stupid because double the work)
24 # decode_json(encode_utf8($self->content))
25 # - set $self->encoding(undef), and set it back after decoding
26 }
27  
28 sub new_json_response {
29 5 ▶ my ( $self, $data, $header_ref, $status ) = @_;
Subroutine new_json_response: called CC=6 SCAR=18.0
Pod documented
30  
31 5 ▶ $status ||= 200;
Condition100%2/2
$status ||= 200
l!l
14
MC/DC100%1/1
$status ||= 200
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
32 5 my $headers;
33 5 ▶ if ($header_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_ref) 2 3 5
34 2 ▶ if (ref($header_ref) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $header_ref eq 'ARRAY') { } 1 1 2
elsif (ref $header_ref eq 'HASH') { } 1 0 1
35 1 $headers = HTTP::Headers->new(@$header_ref);
36 }
37 elsif (ref($header_ref) eq 'HASH') {
38 1 $headers = HTTP::Headers->new(%$header_ref);
39 }
40 }
41 5 ▶ $headers ||= HTTP::Headers->new;
Condition66%2/3
$headers ||= "HTTP::Headers"->new
l!l&&r!l&&!r
230
MC/DC0%0/2
$headers ||= "HTTP::Headers"->new
$headers "HTTP::Headers"->new
Truth table66%2/3
A ||= B
A= $headersB= "HTTP::Headers"->new
ABresult
1X1
011
000
42 5 $headers->header( 'content-type' => 'application/json' );
43  
44 5 return $self->new_response(
45 headers => $headers,
46 status => $status,
47 content => decode_utf8( encode_json($data) ),
48 );
49 }
50  
51 sub new_json_error {
52 3 ▶ my ( $self, $message, $status ) = @_;
Subroutine new_json_error: called CC=3 SCAR=11.0
Pod documented
53 3 ▶ $status ||= 400;
Condition100%2/2
$status ||= 400
l!l
12
MC/DC100%1/1
$status ||= 400
$status
Truth table100%2/2
A
A= $status
Aresult
11
00
54 3 my $body;
55 3 ▶ if ( ref($message) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message) { } 1 2 3
56 1 $body = $message;
57 }
58 else {
59 2 $body = { status => 'error', message => "$message" };
60 }
61  
62 3 return $self->new_response(
63 headers => [ content_type => 'application/json' ],
64 status => $status,
65 content => decode_utf8( encode_json($body) ),
66 );
67 }
68  
69 1 1;
70  
71 __END__
72  
73 =pod
74  
75 =encoding UTF-8
76  
77 =head1 NAME
78  
79 Web::Request::Role::JSON - Make handling JSON easier in Web::Request
80  
81 =head1 VERSION
82  
83 version 1.000
84  
85 =head1 SYNOPSIS
86  
87 # Create a request handler
88 package My::App::Request;
89 use Moose;
90 extends 'Web::Request';
91 with 'Web::Request::Role::JSON';
92  
93 # Make sure your app uses your request handler, e.g. using OX:
94 package My::App::OX;
95 sub request_class {'My::App::Request'}
96  
97 # Finally, in some controller action
98 sub create_POST {
99 my ($self, $req) = @_;
100  
101 my $data = $req->decoded_json_content;
102 my $created = $self->model->create($data);
103 return $self->new_json_response($created, undef, 201);
104 }
105  
106 =head1 DESCRIPTION
107  
108 C<Web::Request::Role::JSON> provides a few methods that make handling
109 JSON in L<Web::Request> a bit easier.
110  
111 Please note that all methods return a L<Web::Response> object.
112 Depending on the framework you use (or lack thereof), you might have
113 to call C<finalize> on the response object to turn it into a valid
114 PSGI response.
115  
116 =head2 METHODS
117  
118 =head3 decoded_json_content
119  
120 my $perl_hash = $req->decoded_json_content;
121  
122 Extracts and decodes a JSON payload from the request.
123  
124 =head3 new_json_response
125  
126 $req->new_json_response( $data );
127 $req->new_json_response( $data, $header_ref );
128 $req->new_json_response( $data, $header_ref, $http_status );
129  
130 Convert your data to JSON and generate a new response with correct HTTP headers.
131  
132 You can pass in more headers as the second argument (either hashref or
133 arrayref). These headers will be passed straight on to
134 C<< HTTP::Headers->new() >>.
135  
136 You can also pass a HTTP status code as the third parameter. If none
137 is provided, we default to C<200>.
138  
139 =head3 new_json_error
140  
141 $req->new_json_response( 'something is wrong' );
142 $req->new_json_response( $error_data );
143 $req->new_json_response( $error, $status );
144  
145 Generate a JSON object out of your error message, if the message is a
146 plain string. But you can also pass in a data structure that will be
147 converte to JSON.
148  
149 Per default, HTTP status is set to C<400>, but you can pass any other
150 status as a second argument. (Yes, there is no checking if you pass a
151 valid status code or not. You're old enough to not do stupid things..)
152  
153 =head1 THANKS
154  
155 Thanks to
156  
157 =over
158  
159 =item *
160  
161 L<validad.com|https://www.validad.com/> for supporting Open Source.
162  
163 =back
164  
165 =head1 AUTHOR
166  
167 Thomas Klausner <domm@cpan.org>
168  
169 =head1 COPYRIGHT AND LICENSE
170  
171 This software is copyright (c) 2017 by Thomas Klausner.
172  
173 This is free software; you can redistribute it and/or modify it under
174 the same terms as the Perl 5 programming language system itself.
175  
176 =cut
« Build ↑ Summary