line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Plack::unAPI; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
36678
|
use Catmandu::Sane; |
|
2
|
|
|
|
|
145688
|
|
|
2
|
|
|
|
|
11
|
|
4
|
2
|
|
|
2
|
|
1422
|
use Catmandu qw(exporter); |
|
2
|
|
|
|
|
379350
|
|
|
2
|
|
|
|
|
12
|
|
5
|
2
|
|
|
2
|
|
373
|
use Scalar::Util qw(blessed); |
|
2
|
|
|
|
|
17
|
|
|
2
|
|
|
|
|
99
|
|
6
|
2
|
|
|
2
|
|
939
|
use Plack::App::unAPI; |
|
2
|
|
|
|
|
57614
|
|
|
2
|
|
|
|
|
117
|
|
7
|
2
|
|
|
2
|
|
12
|
use Plack::Request; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
33
|
|
8
|
2
|
|
|
2
|
|
10
|
use Moo; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
13
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
579
|
use parent 'Plack::Component'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
8
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has formats => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
default => sub { |
17
|
|
|
|
|
|
|
return { |
18
|
|
|
|
|
|
|
json => { |
19
|
|
|
|
|
|
|
type => 'application/json', |
20
|
|
|
|
|
|
|
exporter => [ 'JSON', pretty => 1 ], |
21
|
|
|
|
|
|
|
docs => 'http://json.org/', |
22
|
|
|
|
|
|
|
}, |
23
|
|
|
|
|
|
|
yaml => { |
24
|
|
|
|
|
|
|
type => 'text/yaml', |
25
|
|
|
|
|
|
|
exporter => [ 'YAML' ], |
26
|
|
|
|
|
|
|
docs => 'http://en.wikipedia.org/wiki/YAML', |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
# TODO: check via pre-instanciation |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has query => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
default => sub { } |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has unapi => ( |
39
|
|
|
|
|
|
|
is => 'ro', |
40
|
|
|
|
|
|
|
lazy => 1, |
41
|
|
|
|
|
|
|
builder => sub { |
42
|
3
|
|
|
|
|
10
|
unAPI( map { |
43
|
2
|
|
|
|
|
17
|
my $format = $_[0]->formats->{$_}; |
44
|
3
|
|
|
|
|
8
|
$_ => [ |
45
|
|
|
|
|
|
|
$_[0]->format_as_app($format), |
46
|
|
|
|
|
|
|
$format->{type}, |
47
|
|
|
|
|
|
|
docs => $format->{docs}, |
48
|
|
|
|
|
|
|
] |
49
|
2
|
|
|
2
|
|
1043
|
} keys %{$_[0]->formats}) |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub format_as_app { |
54
|
3
|
|
|
3
|
0
|
4
|
my ($self, $format) = @_; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub { |
57
|
5
|
|
|
5
|
|
1237
|
my ($env) = @_; |
58
|
5
|
|
|
|
|
15
|
my $req = Plack::Request->new($env); |
59
|
5
|
|
|
|
|
36
|
my $id = $req->param('id'); |
60
|
|
|
|
|
|
|
|
61
|
5
|
|
|
|
|
57
|
my $record = $self->query->($id); |
62
|
5
|
100
|
|
|
|
42
|
if (ref $record) { |
|
|
100
|
|
|
|
|
|
63
|
3
|
|
|
|
|
5
|
my $out; |
64
|
3
|
|
|
|
|
4
|
my $exporter = exporter( @{ $format->{exporter} }, file => \$out ); |
|
3
|
|
|
|
|
21
|
|
65
|
3
|
|
|
|
|
233641
|
$exporter->add($record); |
66
|
3
|
|
|
|
|
11857
|
$exporter->commit; |
67
|
3
|
|
|
|
|
68
|
[ 200, [ 'Content-Type' => $format->{type} ] , [ $out ] ]; |
68
|
|
|
|
|
|
|
} elsif(defined $record) { |
69
|
1
|
|
|
|
|
6
|
[ 400, [ 'Content-Type' => 'text/plain' ], [ $record ] ]; |
70
|
|
|
|
|
|
|
} else { |
71
|
1
|
|
|
|
|
7
|
[ 404, [ 'Content-Type' => 'text/plain' ], [ 'Not Found' ] ]; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
3
|
|
|
|
|
27
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub call { |
77
|
8
|
|
|
8
|
1
|
53093
|
my ($self, $env) = @_; |
78
|
8
|
|
|
|
|
145
|
$self->unapi->($env); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
__END__ |