File Coverage

blib/lib/SeeAlso/Format.pm
Criterion Covered Total %
statement 49 51 96.0
branch 10 16 62.5
condition 11 18 61.1
subroutine 12 14 85.7
pod 0 5 0.0
total 82 104 78.8


line stmt bran cond sub pod time code
1             package SeeAlso::Format;
2             $SeeAlso::Format::VERSION = '0.14';
3 3     3   16 use strict;
  3         6  
  3         123  
4 3     3   16 use warnings;
  3         4  
  3         68  
5              
6 3     3   15 use Plack::Request;
  3         4  
  3         56  
7 3     3   14 use Try::Tiny;
  3         6  
  3         154  
8 3     3   3244 use Data::Dumper;
  3         20546  
  3         237  
9              
10 3     3   28 use Scalar::Util qw(reftype);
  3         6  
  3         133  
11              
12 3     3   2094 use SeeAlso::Format::seealso;
  3         10  
  3         83  
13 3     3   1561 use SeeAlso::Format::redirect;
  3         7  
  3         1321  
14              
15             sub valid {
16 7     7 0 12 my $resp = shift;
17 7 100 100     74 return unless (reftype($resp) || '') eq 'ARRAY' and @$resp == 4;
      66        
18 6 50 50     100 return if ref($resp->[0] // []); # identifier must be string
19 6 50 50     32 return unless (reftype($resp) || '') eq 'ARRAY';
20 6         15 foreach (1,2,3) {
21 18         43 my $a = $resp->[$_];
22 18 50 50     74 return unless (reftype($a) || '') eq 'ARRAY';
23 18 50 50     32 return if grep { ref($_ // []) } @$a;
  17         75  
24             }
25 6         32 $resp;
26             }
27              
28              
29             sub new {
30 3     3 0 15 my $class = shift;
31              
32 3 50       13 if (@_) {
33 3         10 $class = "SeeAlso::Format::".lc($_[0]);
34             # TODO
35             # require $class;
36             }
37              
38 3         18 bless { }, $class;
39             }
40              
41             sub type {
42 0     0 0 0 die __PACKAGE__ . '->type must return a MIME type';
43             }
44              
45             sub psgi {
46 0     0 0 0 die __PACKAGE__ . '->psgi must return a PSGI response';
47             }
48              
49             sub app {
50 3     3 0 9 my ($self, $query) = @_;
51             sub {
52 5     5   1722 my $env = shift;
53 5         21 my $id = Plack::Request->new($env)->param('id');
54 5         92 my $result;
55             try {
56 5         147 $result = $query->( $id );
57 4 50 66     52 die 'Invalid SeeAlso response:' . Dumper($result)
58             if defined $result and !valid($result);
59             } catch {
60 1         30 $env->{'psgi.errors'}->print($_);
61 5         41 };
62 5 100       97 $result = [$id,[],[],[]] unless $result;
63              
64 5   50     31 return $self->psgi( $result || [$id,[],[],[]] );
65             }
66 3         29 }
67              
68             1;
69              
70             __END__