View/JSON.pm

filter: Statementstmt 94.3% 83 / 88 Branchbran 92.1% 35 / 38 Conditioncond 82.3% 14 / 17 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 94.1% 16 / 17 Podpod 60.0% 3 / 5 totaltotal 90.2% 158 / 175 CC 28 SCAR 23.8
CC
28
Cov
92%
process
20.2
new
7.1
user_agent_bom_test
2.0
CRAP 10.8
↑ Summary Helper/View/JSON.pm »
1 package Catalyst::View::JSON;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.37';
6 2 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw( Catalyst::View );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Catalyst::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 __PACKAGE__->mk_accessors(qw(
14 allow_callback callback_param expose_stash
15 encoding json_dumper no_x_json_header json_encoder_args
16 use_force_bom));
17  
18 2 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
19 2 my($class, $c, $arguments) = @_;
20 2 my $self = $class->next::method($c);
21  
22 2 for my $field (keys %$arguments) {
23
24 # Remove catalyst_component_name (and future Cat specific params)
25 6 ▶ next if $field =~ /^catalyst/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /^catalyst/ 2 4 6
26
27 # no longer supported
28 4 ▶ warn('json_driver is no longer supported'), next if $field eq 'json_driver';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "json_driver" 0 4 4
29  
30 4 ▶ if ($self->can($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($field)) { } 4 0 4
31 4 $self->$field($arguments->{$field});
32 } else {
33 0 $c->log->debug("Unknown config parameter '$field'");
34 }
35 }
36  
37 2 ▶ if (my $method = $self->can('encode_json')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can('encode_json')) { } 1 1 2
38 1 ▶ $self->json_dumper( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 my($data, $self, $c) = @_;
40 1 $method->($self, $c, $data);
41 1 } );
42 } else {
43 1 require JSON::MaybeXS;
44 1 ▶ my %args = (utf8=>1, %{$self->json_encoder_args ||+{}});
Condition50%1/2
$self->json_encoder_args || {}
l!l
01
MC/DC0%0/1
$self->json_encoder_args || {}
$self->json_encoder_args
Truth table50%1/2
A
A= $self->json_encoder_args
Aresult
11
00
45 1 my $json = JSON::MaybeXS->new(%args);
46 1 ▶ $self->json_dumper(sub { $json->encode($_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48  
49 2 return $self;
50 }
51  
52 12 ▶sub process {
Subroutine process: called CC=20 SCAR=30.0
Pod documented
53 12 my($self, $c) = @_;
54  
55 # get the response data from stash
56 12 ▶ my $cond = sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
57  
58 12 my $single_key;
59 12 ▶ if (my $expose = $self->expose_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expose = $self->expose_stash) 11 1 12
60 11 ▶ if (ref($expose) eq 'Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $expose eq 'Regexp') { } 9 2 11
elsif (ref $expose eq 'ARRAY') { } 0 2 2
elsif (not ref $expose) { } 1 1 2
61 9 ▶ $cond = sub { $_[0] =~ $expose };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 } elsif (ref($expose) eq 'ARRAY') {
63 0 my %match = map { $_ => 1 } @$expose;
64 0 ▶ $cond = sub { $match{$_[0]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 } elsif (!ref($expose)) {
66 1 $single_key = $expose;
67 } else {
68 1 $c->log->warn("expose_stash should be an array reference, Regexp object, or key for a single stash entry.");
69 1 $c->log->warn("Returning all stash entries");
70 }
71 }
72  
73 12 my $data;
74 12 ▶ if ($single_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single_key) { } 1 11 12
75 1 $data = $c->stash->{$single_key};
76 } else {
77 19 ▶ $data = { map { $cond->($_) ? ($_ => $c->stash->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
&$cond($_) ? : 16 3 19
78 11 keys %{$c->stash} };
79 }
80  
81 12 ▶ my $cb_param = $self->allow_callback
Branch: 1 branch
BranchTrueFalseTotal
$self->allow_callback ? : 11 1 12
Condition50%1/2
$self->callback_param or "callback"
l!l
110
MC/DC0%0/1
$self->callback_param or "callback"
$self->callback_param
Truth table50%1/2
A
A= $self->callback_param
Aresult
11
00
82 ? ($self->callback_param || 'callback') : undef;
83 12 ▶ my $cb = $cb_param ? $c->req->param($cb_param) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cb_param ? : 11 1 12
84 12 ▶ $self->validate_callback_param($cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 2 10 12
85  
86 # When you set encoding option in View::JSON, this plugin DWIMs
87 11 ▶ my $encoding = $self->encoding || 'utf-8';
Condition100%2/2
$self->encoding || 'utf-8'
l!l
74
MC/DC100%1/1
$self->encoding || 'utf-8'
$self->encoding
Truth table100%2/2
A
A= $self->encoding
Aresult
11
00
88  
89 11 $c->res->content_type("application/json; charset=$encoding");
90  
91 11 ▶ if ($c->req->header('X-Prototype-Version') && !$self->no_x_json_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->header("X-Prototype-Version") and not $self->no_x_json_header) 1 10 11
Condition100%3/3
$c->req->header("X-Prototype-Version") and not $self->no_x_json_header
!ll&&!rl&&r
911
MC/DC100%2/2
$c->req->header("X-Prototype-Version") and not $self->no_x_json_header
$c->req->header("X-Prototype-Version") not $self->no_x_json_header
Truth table100%3/3
A and not(B)
A= $c->req->header("X-Prototype-Version")B= not $self->no_x_json_header
ABresult
0X0
100
111
92 1 $c->res->header('X-JSON' => 'eval("("+this.transport.responseText+")")');
93 }
94  
95 11 my $json = $self->render($c, $data);
96 11 my $output;
97  
98 ## add UTF-8 BOM if the client meets a test and the application wants it.
99 11 ▶ if ($self->use_force_bom && $encoding eq 'utf-8') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_force_bom and $encoding eq "utf-8") 9 2 11
Condition100%3/3
$self->use_force_bom and $encoding eq "utf-8"
!ll&&!rl&&r
119
MC/DC100%2/2
$self->use_force_bom and $encoding eq "utf-8"
$self->use_force_bom $encoding eq "utf-8"
Truth table100%3/3
A and B
A= $self->use_force_bomB= $encoding eq "utf-8"
ABresult
0X0
100
111
100 9 ▶ my $user_agent = $c->req->user_agent || '';
Condition100%2/2
$c->req->user_agent || ''
l!l
27
MC/DC100%1/1
$c->req->user_agent || ''
$c->req->user_agent
Truth table100%2/2
A
A= $c->req->user_agent
Aresult
11
00
101 9 ▶ if ($self->user_agent_bom_test($user_agent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user_agent_bom_test($user_agent)) 2 7 9
102 2 $output = "\xEF\xBB\xBF";
103 }
104 }
105  
106 11 ▶ $output .= "$cb(" if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 1 10 11
107 11 $output .= $json;
108 11 ▶ $output .= ");" if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 1 10 11
109  
110 11 $c->res->output($output);
111 }
112  
113 # allow for called as $c, $template, $data || $c, $data so that we are compatible
114 # with the semi standard render method that a lot of views use.
115  
116 11 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
117 11 my $self = shift;
118 11 my $c = shift;
119 11 my $data = pop;
120  
121 11 return $self->json_dumper->($data, $self, $c); # weird order to be backward compat
122 }
123  
124 9 ▶sub user_agent_bom_test {
Subroutine user_agent_bom_test: called CC=2 SCAR=7.1
Pod undocumented
125 9 my ($self, $user_agent) = @_;
126 9 ▶ return(($user_agent =~ m/\bSafari\b/) and ($user_agent !~ m/\bChrome\b/));
Condition66%2/3
$user_agent =~ /\bSafari\b/ && !($user_agent =~ /\bChrome\b/)
!ll&&!rl&&r
702
MC/DC50%1/2
$user_agent =~ /\bSafari\b/ && !($user_agent =~ /\bChrome\b/)
$user_agent =~ /\bSafari\b/ !($user_agent =~ /\bChrome\b/)
Truth table66%2/3
A && not(B)
A= $user_agent =~ /\bSafari\b/B= !($user_agent =~ /\bChrome\b/)
ABresult
0X0
100
111
127 }
128  
129  
130 2 ▶sub validate_callback_param {
Subroutine validate_callback_param: called CC=2 SCAR=6.9
Pod undocumented
131 2 my($self, $param) = @_;
132 2 ▶ $param =~ /^[a-zA-Z0-9\.\_\[\]]+$/
Branch: 1 branch
BranchTrueFalseTotal
$param =~ /^[a-zA-Z0-9\.\_\[\]]+$/ or "Catalyst::Exception"->throw("Invalid callback parameter $param") 1 1 2
133 or Catalyst::Exception->throw("Invalid callback parameter $param");
134 }
135  
136 2 1;
137 __END__
↑ Summary Helper/View/JSON.pm »