blib/lib/Plack/Middleware/JSON/ForBrowsers.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 87.5% 14 / 16 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 95.2% 119 / 125 CC 11 SCAR 8.3
CC
11
Cov
95%
looks_like_browser_request
5.0
__ANON__
3.1
new
3.0
CRAP 2.3
↑ Summary examples/app.psgi »
1 package Plack::Middleware::JSON::ForBrowsers;
2 {
3 2 $Plack::Middleware::JSON::ForBrowsers::VERSION = '0.002000';
4 }
5 2 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Plack middleware which turns application/json responses into HTML
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Plack::Util::Accessor qw(json html_head html_foot);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use HTML::Entities qw(encode_entities_numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 2 chomp(my $html_head = <<'EOHTML');
21 <?xml version="1.0"?>
22 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
23 <html xmlns="http://www.w3.org/1999/xhtml">
24 <head>
25 <title>JSON::ForBrowsers</title>
26 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
27 <style type="text/css">
28 html, body {
29 padding: 0px;
30 margin: 0px;
31 }
32 pre {
33 background-color: #FAFAFA;
34 border: 1px solid #E9E9E9;
35 padding: 4px;
36 margin: 12px;
37 }
38 </style>
39 </head>
40 <body>
41 <pre><code>
42 EOHTML
43  
44 2 (my $html_foot = <<'EOHTML') =~ s/^\s+//x;
45 </code></pre>
46 </body>
47 </html>
48 EOHTML
49  
50 2 my @json_types = qw(application/json);
51 2 my @html_types = qw(text/html application/xhtml+xml);
52  
53  
54  
55 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
56 8 my ($class, $arg_ref) = @_;
57  
58 8 my $self = $class->next::method($arg_ref);
59 8 $self->json(JSON->new()->utf8()->pretty());
60  
61 8 ▶ unless (defined $self->html_head()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->html_head) 4 4 8
62 4 $self->html_head($html_head);
63 }
64 8 ▶ unless (defined $self->html_foot()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->html_foot) 4 4 8
65 4 $self->html_foot($html_foot);
66 }
67  
68 8 return $self;
69 }
70  
71  
72  
73 4 ▶sub call {
Subroutine call: called CC=2 SCAR=7.0
Pod documented
74 4 my($self, $env) = @_;
75  
76 4 my $res = $self->app->($env);
77  
78 4 ▶ unless ($self->looks_like_browser_request($env)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->looks_like_browser_request($env)) 3 1 4
79 3 return $res;
80 }
81  
82 1 ▶ return $self->response_cb($res, sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
83 1 my ($cb_res) = @_;
84  
85 1 my $h = Plack::Util::headers($cb_res->[1]);
86 # Ignore stuff like '; charset=utf-8' for now, just assume UTF-8 input
87 1 ▶ if (any { index($h->get('Content-Type'), $_) >= 0 } @json_types) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { index($h->get("Content-Type"), $_) != -1; } , @json_types)) 1 0 1
88 1 $h->set('Content-Type' => 'text/html; charset=utf-8');
89  
90 1 my $json = '';
91 1 my $seen_last = 0;
92 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
93 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 1 1 2
94 1 $json .= $_[0];
95 1 return '';
96 }
97 else {
98 1 ▶ if ($seen_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_last) { } 0 1 1
99 0 return;
100 }
101 else {
102 1 $seen_last = 1;
103 1 return $self->json_to_html($json);
104 }
105 }
106 1 };
107 }
108 0 return;
109 1 });
110 }
111  
112  
113  
114 10 ▶sub looks_like_browser_request {
Subroutine looks_like_browser_request: called CC=5 SCAR=16.1
Pod documented
115 10 my ($self, $env) = @_;
116  
117 10 ▶ if (defined $env->{HTTP_X_REQUESTED_WITH}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"HTTP_X_REQUESTED_WITH"} and $env->{"HTTP_X_REQUESTED_WITH"} eq "XMLHttpRequest") 3 7 10
Condition66%2/3
defined $env->{"HTTP_X_REQUESTED_WITH"} and $env->{"HTTP_X_REQUESTED_WITH"} eq "XMLHttpRequest"
!ll&&!rl&&r
703
MC/DC50%1/2
defined $env->{"HTTP_X_REQUESTED_WITH"} and $env->{"HTTP_X_REQUESTED_WITH"} eq "XMLHttpRequest"
defined $env->{"HTTP_X_REQUESTED_WITH"} $env->{"HTTP_X_REQUESTED_WITH"} eq "XMLHttpRequest"
Truth table66%2/3
A and B
A= defined $env->{"HTTP_X_REQUESTED_WITH"}B= $env->{"HTTP_X_REQUESTED_WITH"} eq "XMLHttpRequest"
ABresult
0X0
100
111
118 && $env->{HTTP_X_REQUESTED_WITH} eq 'XMLHttpRequest') {
119 3 return 0;
120 }
121  
122 7 ▶ if (defined $env->{HTTP_ACCEPT}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env->{"HTTP_ACCEPT"} and &any(sub { index($env->{"HTTP_ACCEPT"}, $_) != -1; } , @html_types)) 3 4 7
Condition100%3/3
defined $env->{"HTTP_ACCEPT"} and &any(sub { index($env->{"HTTP_ACCEPT"}, $_) != -1; } , @html_types)
!ll&&!rl&&r
313
MC/DC100%2/2
defined $env->{"HTTP_ACCEPT"} and &any(sub { index($env->{"HTTP_ACCEPT"}, $_) != -1; } , @html_types)
defined $env->{"HTTP_ACCEPT"} &any(sub { index($env->{"HTTP_ACCEPT"}, $_) != -1; } , @html_types)
Truth table100%3/3
A and B
A= defined $env->{"HTTP_ACCEPT"}B= &any(sub { index($env->{"HTTP_ACCEPT"}, $_) != -1; } , @html_types)
ABresult
0X0
100
111
123 5 ▶ && any { index($env->{HTTP_ACCEPT}, $_) >= 0 } @html_types) {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 3 return 1;
125 }
126  
127 4 return 0;
128 }
129  
130  
131  
132 7 ▶sub json_to_html {
Subroutine json_to_html: called CC=1 SCAR=0.0
Pod documented
133 7 my ($self, $json) = @_;
134  
135 7 my $pretty_json_string = decode(
136 'UTF-8',
137 $self->json()->encode(
138 $self->json()->decode($json)
139 )
140 );
141 7 chomp $pretty_json_string;
142 7 return encode(
143 'UTF-8',
144 $self->html_head()
145 . encode_entities_numeric($pretty_json_string) .
146 $self->html_foot()
147 );
148 }
149  
150  
151 2 1;
152  
153  
154 __END__
↑ Summary examples/app.psgi »