File Coverage

blib/lib/Plack/App/Catmandu/Bag.pm
Criterion Covered Total %
statement 18 60 30.0
branch 0 12 0.0
condition 0 10 0.0
subroutine 6 18 33.3
pod 1 10 10.0
total 25 110 22.7


line stmt bran cond sub pod time code
1             package Plack::App::Catmandu::Bag;
2              
3 1     1   303797 use Catmandu::Sane;
  1         3  
  1         8  
4              
5             our $VERSION = '0.01';
6              
7 1     1   220 use parent 'Plack::Component';
  1         2  
  1         9  
8 1     1   3347 use Catmandu;
  1         2  
  1         9  
9 1     1   676 use Router::Simple;
  1         3691  
  1         29  
10 1     1   6 use JSON qw(encode_json);
  1         2  
  1         9  
11 1     1   107 use namespace::clean;
  1         3  
  1         9  
12              
13             sub bag {
14 0     0 0   my ($self) = @_;
15 0   0       $self->{_bag} ||= $self->_build_bag;
16             }
17              
18             sub router {
19 0     0 0   my ($self) = @_;
20 0   0       $self->{_router} ||= $self->_build_router;
21             }
22              
23             sub _build_bag {
24 0     0     my ($self) = @_;
25 0           Catmandu->store($self->{store})->bag($self->{bag});
26             }
27              
28             sub _build_router {
29 0     0     my ($self) = @_;
30 0           my $router = Router::Simple->new;
31 0 0         if ($self->bag->does('Catmandu::Plugin::Versioning')) {
32 0           $router->connect(
33             '/{id}/versions',
34             {action => 'version_list'},
35             {method => 'GET'}
36             );
37 0           $router->connect(
38             '/{id}/versions/{version}',
39             {action => 'version_show'},
40             {method => 'GET'}
41             );
42             }
43 0           $router->connect('/', {action => 'list'}, {method => 'GET'});
44 0           $router->connect('/{id}', {action => 'show'}, {method => 'GET'});
45 0           $router;
46             }
47              
48             sub list {
49 0     0 0   my ($self, $params) = @_;
50 0   0       my $start = $params->{start} // 0;
51 0   0       my $limit = $params->{limit} // 10;
52 0           $self->ok($self->bag->slice($start, $limit)->to_array);
53             }
54              
55             sub show {
56 0     0 0   my ($self, $params) = @_;
57 0 0         if (my $data = $self->bag->get($params->{id})) {
58 0           $self->ok($data);
59             }
60             else {
61 0           $self->not_found;
62             }
63             }
64              
65             sub version_list {
66 0     0 0   my ($self, $params) = @_;
67 0 0         if (my $data = $self->bag->get_history($params->{id})) {
68 0           $self->ok($data);
69             }
70             else {
71 0           $self->not_found;
72             }
73             }
74              
75             sub version_show {
76 0     0 0   my ($self, $params) = @_;
77 0 0         if (my $data = $self->bag->get_version($params->{id}, $params->{version}))
78             {
79 0           $self->ok($data);
80             }
81             else {
82 0           $self->not_found;
83             }
84             }
85              
86             sub ok {
87 0     0 0   my ($self, $data) = @_;
88 0           my $res = {data => $data};
89             [
90 0           '200', ['Content-Type' => 'application/vnd.api+json'],
91             [encode_json($res)],
92             ];
93             }
94              
95             sub method_not_allowed {
96 0     0 0   ['405', ['Content-Type' => 'text/plain'], ['Method Not Allowed']];
97             }
98              
99             sub not_found {
100 0     0 0   ['404', ['Content-Type' => 'text/plain'], ['Not Found']];
101             }
102              
103             sub call {
104 0     0 1   my ($self, $env) = @_;
105 0           my $router = $self->router;
106              
107 0 0         if (my $params = $router->match($env)) {
    0          
108 0           my $action = $params->{action};
109 0           $self->$action($params);
110             }
111             elsif ($router->method_not_allowed) {
112 0           $self->method_not_allowed;
113             }
114             else {
115 0           $self->not_found;
116             }
117             }
118              
119             1;
120             __END__
121              
122             =encoding utf-8
123              
124             =head1 NAME
125              
126             Plack::App::Catmandu::Bag - Plack application that wraps a REST API around a Catmandu::Bag
127              
128             =head1 SYNOPSIS
129              
130             use Catmandu;
131             use Plack::Builder;
132             use Plack::App::Catmandu::Bag;
133              
134             Catmandu->define_store('library',
135             MongoDB => (bags => {books => {plugins => ['Versioning']}}));
136              
137             builder {
138             mount '/api/books' => Plack::App::Catmandu::Bag->new(
139             store => 'library',
140             bag => 'books',
141             );
142             };
143              
144             =head1 DESCRIPTION
145              
146             This is an early minimal release, look at the tests for usage.
147              
148             =head1 AUTHOR
149              
150             Nicolas Steenlant E<lt>nicolas.steenlant@ugent.beE<gt>
151              
152             =head1 COPYRIGHT
153              
154             Copyright 2017- Nicolas Steenlant
155              
156             =head1 LICENSE
157              
158             This library is free software; you can redistribute it and/or modify
159             it under the same terms as Perl itself.
160              
161             =head1 SEE ALSO
162              
163             =cut