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