JSON.pm

filter: Statementstmt 81.2% 78 / 96 Branchbran 43.7% 14 / 32 Conditioncond 25.0% 4 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 14 / 21 Podpod 50.0% 6 / 12 totaltotal 62.7% 116 / 185 CC 26 SCAR 28.4
CC
26
Cov
67%
response
42.9
uri
12.0
__ANON__
5.2
CRAP 17.1
↑ Summary JSON/_ClassInfo.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Catalyst::View::Base::JSON;
5  
6 1 ▶use base 'Catalyst::View';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = 0.003;
11 1 our $CLASS_INFO = 'Catalyst::View::Base::JSON::_ClassInfo';
12  
13 1 ▶my $inject_http_status_helpers = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
14 1 my ($class, $args) = @_;
15 1 ▶ return unless $args->{returns_status};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"returns_status"} 0 1 1
16 1 foreach my $helper( grep { $_=~/^http/i} @HTTP::Status::EXPORT_OK) {
17 71 my $subname = lc $helper;
18 71 my $code = HTTP::Status->$helper;
19 71 my $codename = "http_".$code;
20 71 ▶ if(grep { $code == $_ } @{ $args->{returns_status}||[]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$code == $_;} @{$args->{"returns_status"} or [];}) 2 69 71
Condition50%1/2
$args->{'returns_status'} || []
l!l
710
MC/DC0%0/1
$args->{'returns_status'} || []
$args->{'returns_status'}
Truth table50%1/2
A
A= $args->{'returns_status'}
Aresult
11
00
21 0 ▶ eval "sub ${\$class}::${\$subname} { return shift->response(HTTP::Status::$helper,\@_) }";
Subroutine http_not_found: not called CC=1 SCAR=0.1
Subroutine http_ok: called CC=1 SCAR=0.1
Pod undocumented
Pod undocumented
22 0 ▶ eval "sub ${\$class}::${\$codename} { return shift->response(HTTP::Status::$helper,\@_) }";
Subroutine http_200: not called CC=1 SCAR=0.6
Subroutine http_404: not called CC=1 SCAR=0.6
Pod undocumented
Pod undocumented
23 }
24 }
25 1 };
26  
27 1 ▶my $find_fields = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
28 1 my $class = shift;
29 1 my @fields = ();
30 1 for ($class->meta->get_all_attributes) {
31 4 ▶ next unless $_->has_init_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->has_init_arg 0 4 4
32 4 push @fields, $_->init_arg;
33 }
34 1 return @fields;
35 1 };
36  
37 1 ▶sub _build_class_info {
Subroutine _build_class_info: called CC=1 SCAR=0.0
38 1 my ($class, $args) = @_;
39 1 Catalyst::Utils::ensure_class_loaded($CLASS_INFO);
40 1 return $CLASS_INFO->new($args);
41 }
42  
43 1 ▶sub COMPONENT {
Subroutine COMPONENT: called CC=1 SCAR=0.0
Pod documented
44 1 my ($class, $app, $args) = @_;
45 1 $args = $class->merge_config_hashes($class->config, $args);
46 1 $args->{_instance_class} = $class;
47 1 $args->{_original_args} = $args;
48 1 $args->{_fields} = [$class->$find_fields];
49 1 $class->$inject_http_status_helpers($args);
50  
51 1 return $class->_build_class_info($args);
52 }
53  
54 1 ▶sub ctx { return $_[0]->{__ctx} }
Subroutine ctx: called CC=1 SCAR=0.0
Pod documented
55 0 ▶sub process { return shift->response(200, @_) }
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
56 0 ▶sub detach { shift->ctx->detach(@_) }
Subroutine detach: not called CC=1 SCAR=6.9
Pod undocumented
57  
58 3 ▶my $class_info = sub { return $_[0]->{__class_info} };
Subroutine __ANON__: called CC=1 SCAR=0.0
59  
60 1 ▶sub response {
Subroutine response: called CC=16 SCAR=37.6
Pod documented
61 1 my ($self, @proto) = @_;
62
63 1 my $status = 200;
64 1 ▶ if( (ref \$proto[0] eq 'SCALAR') and
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$proto[0] eq "SCALAR" and &Scalar::Util::looks_like_number($proto[0])) 1 0 1
Condition33%1/3
ref \$proto[0] eq "SCALAR" and &Scalar::Util::looks_like_number($proto[0])
!ll&&!rl&&r
001
MC/DC0%0/2
ref \$proto[0] eq "SCALAR" and &Scalar::Util::looks_like_number($proto[0])
ref \$proto[0] eq "SCALAR" &Scalar::Util::looks_like_number($proto[0])
Truth table33%1/3
A and B
A= ref \$proto[0] eq "SCALAR"B= &Scalar::Util::looks_like_number($proto[0])
ABresult
0X0
100
111
65 Scalar::Util::looks_like_number($proto[0])
66 ){
67 1 $status = shift @proto;
68 }
69  
70 1 my $possible_override_data = '';
71 1 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (@proto and (ref $proto[-1] || "") eq "HASH" || builtin::blessed($proto[-1])) 0 1 1
Condition12%1/8
@proto and (ref $proto[-1] || "") eq "HASH" || builtin::blessed($proto[-1])
!ll&&!rl&&r
100
(ref $proto[-1] || "") eq "HASH" or builtin::blessed($proto[-1])
l!l&&r!l&&!r
000
ref $proto[-1] || ''
l!l
00
MC/DC0%0/3
@proto and (ref $proto[-1] || "") eq "HASH" || builtin::blessed($proto[-1])
@proto ref $proto[-1] builtin::blessed($proto[-1])
Truth table25%1/4
A and B or C
A= @protoB= ref $proto[-1]C= builtin::blessed($proto[-1])
ABCresult
0XX0
1000
11X1
1011
72 @proto &&
73 (
74 ((ref($proto[-1])||'') eq 'HASH') ||
75 Scalar::Util::blessed($proto[-1])
76 )
77 ) {
78 0 $possible_override_data = pop(@proto);
79 }
80
81 1 my @headers = ();
82 1 ▶ if(@proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (@proto) 0 1 1
83 0 @headers = @proto;
84 }
85  
86 1 for($self->ctx->response) {
87 1 ▶ $_->headers->push_header(@headers) if @headers;
Branch: 1 branch
BranchTrueFalseTotal
if @headers 0 1 1
88 1 ▶ $_->status($status) unless $_->status != 200; # Catalyst default is 200...
Branch: 1 branch
BranchTrueFalseTotal
unless $_->status != 200 1 0 1
89 1 ▶ $_->content_type($self->$class_info->content_type)
Branch: 1 branch
BranchTrueFalseTotal
unless $_->content_type 1 0 1
90 unless $_->content_type;
91  
92 1 ▶ $self->amend_headers($_->headers)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("amend_headers") 0 1 1
93 if $self->can('amend_headers');
94  
95 1 ▶ unless($_->has_body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_->has_body) 1 0 1
96 1 my $json = $self->render($possible_override_data);
97 1 ▶ if(my $param = $self->$class_info->callback_param) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param = $self->$class_info->callback_param) 0 1 1
98 0 my $cb = $_->query_parameter($self->$class_info->callback_param);
99 0 ▶ $cb =~ /^[a-zA-Z0-9\.\_\[\]]+$/ || die "Invalid callback parameter $cb";
Branch: 1 branch
BranchTrueFalseTotal
$cb =~ /^[a-zA-Z0-9\.\_\[\]]+$/ or die "Invalid callback parameter $cb" 0 0 0
100 0 $json = "$cb($json)";
101 }
102 1 $_->body($json);
103 }
104 }
105 }
106  
107 1 ▶sub render {
Subroutine render: called CC=3 SCAR=13.0
Pod documented
108 1 my ($self, $possible_override_data) = @_;
109 1 ▶ my $to_json_encode = $possible_override_data ? $possible_override_data : $self;
Branch: 1 branch
BranchTrueFalseTotal
$possible_override_data ? : 0 1 1
110 my $json = eval {
111 1 $self->$class_info->json->encode($to_json_encode);
112 1 ▶ } || do {
Condition33%1/3
eval { do { $self->$class_info->json->encode($to_json_encode) } } || do { $self->$class_info->HANDLE_ENCODE_ERROR($self, $to_json_encode, $@); return }
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { $self->$class_info->json->encode($to_json_encode) } } || do { $self->$class_info->HANDLE_ENCODE_ERROR($self, $to_json_encode, $@); return }
eval { do { $self->$class_info->json->encode($to_json_encode) } } do { $self->$class_info->HANDLE_ENCODE_ERROR($self, $to_json_encode, $@); return }
Truth table33%1/3
A || B
A= eval { do { $self->$class_info->json->encode($to_json_encode) } }B= do { $self->$class_info->HANDLE_ENCODE_ERROR($self, $to_json_encode, $@); return }
ABresult
1X1
011
000
113 0 $self->$class_info->HANDLE_ENCODE_ERROR($self, $to_json_encode, $@);
114 0 return;
115 };
116 1 return $json;
117 }
118  
119 0 ▶sub uri {
Subroutine uri: not called CC=3 SCAR=24.8
Pod documented
120 0 my ($self, $action_proto, @args) = @_;
121  
122 # Is an action object
123 0 ▶ return $self->ctx->uri_for($action_proto, @args)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($action_proto) 0 0 0
124 if Scalar::Util::blessed($action_proto);
125  
126 # Is an absolute or relative (to the current controller) action private name.
127 0 ▶ my $action = $action_proto=~m/^\// ?
Branch: 1 branch
BranchTrueFalseTotal
$action_proto =~ m[^/] ? : 0 0 0
128 $self->ctx->dispatcher->get_action_by_path($action_proto) :
129 $self->ctx->controller->action_for($action_proto);
130
131 0 return $self->ctx->uri_for($action, @args);
132 }
133  
134 0 ▶sub TO_JSON { die "View ${\$_[0]->catalyst_component_name} must define a 'TO_JSON' method!" }
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
135  
136 1 1;
137  
138 =head1 NAME
139  
140 Catalyst::View::Base::JSON - a 'base' JSON View
141  
142 =for html
143 <a href="https://badge.fury.io/pl/Catalyst-View-Base-JSON"><img src="https://badge.fury.io/pl/Catalyst-View-Base-JSON.svg" alt="CPAN version" height="18"></a>
144 <a href="https://travis-ci.org/jjn1056/Catalyst-View-Base-JSON/"><img src="https://api.travis-ci.org/jjn1056/Catalyst-View-Base-JSON.png" alt="https://api.travis-ci.org/jjn1056/Catalyst-View-Base-JSON.png"></a>
145 <a href="http://cpants.cpanauthors.org/dist/Catalyst-View-Base-JSON"><img src="http://cpants.cpanauthors.org/dist/Catalyst-View-Base-JSON.png" alt='Kwalitee Score' /></a>
146  
147 =head1 SYNOPSIS
148  
149 package MyApp::View::Person;
150  
151 use Moo;
152 use Types::Standard;
153 use MyApp::Types qw/Version/;
154  
155 extends 'Catalyst::View::Base::JSON';
156  
157 has name => (
158 is=>'ro',
159 isa=>Str,
160 required=>1);
161  
162 has age => (
163 is=>'ro',
164 isa=>Int,
165 required=>1);
166  
167 has api_version => (
168 is=>'ro',
169 isa=>Version,
170 required=>1);
171  
172 sub amend_headers {
173 my ($self, $headers) = @_;
174 $headers->push_header(Accept => 'application/json');
175 }
176  
177 sub TO_JSON {
178 my $self = shift;
179 return +{
180 name => $self->name,
181 age => $self->age,
182 api => $self->api_version,
183 };
184 }
185  
186 package MyApp::Controller::Root;
187 use base 'Catalyst::Controller';
188  
189 sub example :Local Args(0) {
190 my ($self, $c) = @_;
191 $c->stash(age=>32);
192 $c->view('Person', name=>'John')->http_ok;
193 }
194  
195 package MyApp;
196
197 use Catalyst;
198  
199 MyApp->config(
200 'Controller::Root' => { namespace => '' },
201 'View::Person' => {
202 returns_status => [200, 404],
203 api_version => '1.1',
204 },
205 );
206  
207 MyApp->setup;
208  
209  
210 =head1 DESCRIPTION
211  
212 This is a Catalyst view that lets you create one view per reponse type of JSON
213 you are generating. Because you are creating one view per reponse type that means
214 you can define an interface for that view which is strongly typed. Also, since
215 the view is per request, it has access to the context, as well as some helpers
216 for creating URLs. You may find that this helps make your controllers more
217 simple and promote reuse of view code.
218  
219 I consider this work partly a thought experiment. Documentation and test coverage
220 are currently light and I might change parts of the way exceptions are handled. If
221 you are producing JSON with L<Catalyst> and new to the framework you might want to
222 consider 'tried and true' approaches such as L<Catalyst::View:::JSON> or
223 L<Catalyst::Action::REST>. My intention here is to get people to start thinking
224 about views with stronger interfaces.
225  
226 =head1 METHODS
227  
228 This view defines the following methods
229  
230 =head2 response
231  
232 $view->response($status);
233 $view->response($status, @headers);
234 $view->response(@headers);
235  
236  
237 Used to setup a response. Calling this method will setup an http status, finalize
238 headers and set a body response for the JSON. Content type will be set based on
239 your 'content_type' configuration value (or 'application/json' by default).
240  
241 =head2 Method '->response' Helpers
242  
243 We map status codes from L<HTTP::Status> into methods to make sending common
244 request types more simple and more descriptive. The following are the same:
245  
246 $c->view->response(200, @args);
247 $c->view->http_ok(@args);
248  
249 do { $c->view->response(200, @args); $c->detach };
250 $c->view->http_ok(@args)->detach;
251  
252 See L<HTTP::Status> for a full list of all the status code helpers.
253  
254 =head2 ctx
255  
256 Returns the current context associated with the request creating this view.
257  
258 =head2 uri ($action|$action_name|$relative_action_name)
259  
260 Helper used to create links. Example:
261  
262 sub TO_JSON {
263 my $self = shift;
264 return +{
265 name => $self->name,
266 age => $self->age,
267 friends => $self->uri('friends', $self->id),
268 };
269 }
270  
271 The arguments are basically the same as $c->uri_for except that the first argument
272 may be a full or relative action path.
273  
274 =head2 render
275  
276 Returns a string which is the JSON represenation of the current View. Usually you
277 won't need to call this directly.
278  
279 =head2 process
280  
281 used as a target for $c->forward. This is mostly here for compatibility with some
282 existing methodology. For example allows using this View with the RenderView action
283 class (or L<Catalyst::Action::RenderView>).
284  
285 =head1 ATTRIBUTES
286  
287 See L<Catalyst::View::Base::JSON::_ClassInfo> for application level configuration.
288 You may also defined custom attributes in your base class and assign values via
289 configuration.
290  
291 =head1 UTF-8 NOTES
292  
293 Generally a view should not do any encoding since the core L<Catalyst>
294 framework handles all this for you. However, historically the popular
295 Catalyst JSON views and related ecosystem (such as L<Catalyst::Action::REST>)
296 have done UTF8 encoding and as a result for compatibility core Catalyst code
297 will assume a response content type of 'application/json' is already UTF8
298 encoded. So even though this is a new module, we will continue to maintain this
299 historical situation for compatibility reasons. As a result the UTF8 encoding
300 flags will be enabled and expect the contents of $c->res->body to be encoded
301 as expected. If you set your own JSON class for encoding, or set your own
302 initialization arguments, please keep in mind this expectation.
303  
304 =head1 SEE ALSO
305  
306 L<Catalyst>, L<Catalyst::View>, L<Catalyst::View::JSON>,
307 L<JSON::MaybeXS>
308  
309 =head1 AUTHOR
310
311 John Napiorkowski L<email:jjnapiork@cpan.org>
312
313 =head1 COPYRIGHT & LICENSE
314
315 Copyright 2016, John Napiorkowski L<email:jjnapiork@cpan.org>
316
317 This library is free software; you can redistribute it and/or modify it under
318 the same terms as Perl itself.
319  
320 =cut
↑ Summary JSON/_ClassInfo.pm »