line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Plack::unAPI; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
37239
|
use Catmandu::Sane; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Catmandu; |
5
|
|
|
|
|
|
|
use Catmandu::Util qw(is_array_ref); |
6
|
|
|
|
|
|
|
use Plack::App::unAPI; |
7
|
|
|
|
|
|
|
use Plack::Request; |
8
|
|
|
|
|
|
|
use Moo; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use parent 'Plack::Component'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
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 store => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
coerce => sub { |
36
|
|
|
|
|
|
|
if (is_array_ref($_[0])) { |
37
|
|
|
|
|
|
|
Catmandu->store(@$_[0]) |
38
|
|
|
|
|
|
|
} elsif(!ref $_[0]) { |
39
|
|
|
|
|
|
|
Catmandu->store($_[0]) |
40
|
|
|
|
|
|
|
} else { |
41
|
|
|
|
|
|
|
$_[0]; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has query => ( |
47
|
|
|
|
|
|
|
is => 'ro', |
48
|
|
|
|
|
|
|
lazy => 1, |
49
|
|
|
|
|
|
|
builder => sub { |
50
|
|
|
|
|
|
|
if (my $store = $_[0]->store) { |
51
|
|
|
|
|
|
|
sub { $store->bag->get($_[0]) } |
52
|
|
|
|
|
|
|
} else { |
53
|
|
|
|
|
|
|
sub { } |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
has unapi => ( |
59
|
|
|
|
|
|
|
is => 'ro', |
60
|
|
|
|
|
|
|
lazy => 1, |
61
|
|
|
|
|
|
|
builder => sub { |
62
|
|
|
|
|
|
|
unAPI( map { |
63
|
|
|
|
|
|
|
my $format = $_[0]->formats->{$_}; |
64
|
|
|
|
|
|
|
$_ => [ |
65
|
|
|
|
|
|
|
$_[0]->format_as_app($format), |
66
|
|
|
|
|
|
|
$format->{type}, |
67
|
|
|
|
|
|
|
docs => $format->{docs}, |
68
|
|
|
|
|
|
|
] |
69
|
|
|
|
|
|
|
} keys %{$_[0]->formats}) |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub format_as_app { |
74
|
|
|
|
|
|
|
my ($self, $format) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub { |
77
|
|
|
|
|
|
|
my ($env) = @_; |
78
|
|
|
|
|
|
|
my $req = Plack::Request->new($env); |
79
|
|
|
|
|
|
|
my $id = $req->param('id'); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $record = $self->query->($id); |
82
|
|
|
|
|
|
|
if (ref $record) { |
83
|
|
|
|
|
|
|
my $out; |
84
|
|
|
|
|
|
|
my $exporter = is_array_ref($format->{exporter}) |
85
|
|
|
|
|
|
|
? $format->{exporter} : [$format->{exporter}]; |
86
|
|
|
|
|
|
|
$exporter = Catmandu->exporter( @$exporter, file => \$out ); |
87
|
|
|
|
|
|
|
$exporter->add($record); |
88
|
|
|
|
|
|
|
$exporter->commit; |
89
|
|
|
|
|
|
|
[ 200, [ 'Content-Type' => $format->{type} ] , [ $out ] ]; |
90
|
|
|
|
|
|
|
} elsif(defined $record) { |
91
|
|
|
|
|
|
|
[ 400, [ 'Content-Type' => 'text/plain' ], [ $record ] ]; |
92
|
|
|
|
|
|
|
} else { |
93
|
|
|
|
|
|
|
[ 404, [ 'Content-Type' => 'text/plain' ], [ 'Not Found' ] ]; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub call { |
99
|
|
|
|
|
|
|
my ($self, $env) = @_; |
100
|
|
|
|
|
|
|
$self->unapi->($env); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
__END__ |