File Coverage

blib/lib/Plack/Middleware/Signposting/JSON.pm
Criterion Covered Total %
statement 41 41 100.0
branch 8 12 66.6
condition 3 5 60.0
subroutine 9 9 100.0
pod 1 1 100.0
total 62 68 91.1


line stmt bran cond sub pod time code
1              
2             our $VERSION = '0.06';
3              
4             use Catmandu::Sane;
5 1     1   139335 use Catmandu;
  1         144572  
  1         6  
6 1     1   682 use Catmandu::Fix;
  1         89744  
  1         5  
7 1     1   188 use JSON qw(decode_json);
  1         2  
  1         20  
8 1     1   530 use Plack::Request;
  1         6965  
  1         5  
9 1     1   496 use Plack::Util::Accessor qw(fix);
  1         40318  
  1         35  
10 1     1   6 use Moo;
  1         4  
  1         9  
11 1     1   46  
  1         2  
  1         8  
12             extends 'Plack::Middleware::Signposting';
13              
14             my ($self, $env) = @_;
15              
16 2     2 1 26185 my $request = Plack::Request->new($env);
17             my $res = $self->app->($env);
18 2         16  
19 2         30 # only get/head requests
20             return $res unless $request->method =~ m{^get|head$}i;
21              
22 2 50       60 # see http://search.cpan.org/~miyagawa/Plack-1.0044/lib/Plack/Middleware.pm#RESPONSE_CALLBACK
23             return $self->response_cb($res, sub {
24             my $res = shift;
25              
26 2     2   46 my $content_type = Plack::Util::header_get($res->[1], 'Content-Type') || '';
27             # only json responses
28 2   50     10 return unless $content_type =~ m{^application/json|application\/vnd\.api\+json}i;
29             # ignore streaming response for now
30 2 50       35 return unless ref $res->[2] eq 'ARRAY';
31              
32 2 50       7 my $body = join('', @{$res->[2]});
33             my $data = decode_json($body);
34 2         5  
  2         17  
35 2         170 if (ref $data && ref $data eq 'ARRAY') {
36             $data = $data->[0];
37 2 100 66     15 }
38 1         3  
39             my $fix = $self->fix ? $self->fix : 'nothing()';
40             my $fixer = Catmandu::Fix->new(fixes => [$fix]);
41 2 100       8 $fixer->fix($data);
42 2         56  
43 2         1470 # add information to the 'Link' header
44             if ($data->{signs}) {
45             Plack::Util::header_push(
46 2 50       160632 $res->[1],
47             'Link' => $self->to_link_format( @{$data->{signs}} )
48             );
49 2         9 }
  2         21  
50             });
51             }
52 2         40  
53             1;
54              
55              
56             =encoding utf-8
57              
58             =head1 NAME
59              
60             Plack::Middleware::Signposting::JSON - A Signposting implementation for JSON content
61              
62             =head1 SYNOPSIS
63              
64             my $json_string = '{"hello":"world",....}';
65             builder {
66             enable "Plack::Middleware::Signposting::JSON";
67              
68             sub { 200, [ 'Content-Type' => 'text/plain' ], [ $json_string ] };
69             };
70              
71             =head1 SEE ALSO
72              
73             L<Plack::Middleware>
74              
75             =cut