File Coverage

blib/lib/Articulate/Service/SimplePreviews.pm
Criterion Covered Total %
statement 18 33 54.5
branch 0 8 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 49 51.0


line stmt bran cond sub pod time code
1             package Articulate::Service::SimplePreviews;
2              
3 5     5   2440 use strict;
  5         11  
  5         149  
4 5     5   19 use warnings;
  5         9  
  5         94  
5              
6 5     5   20 use Articulate::Syntax;
  5         5  
  5         231  
7              
8 5     5   6328 use Moo;
  5         10  
  5         24  
9             with 'Articulate::Role::Service';
10              
11 5     5   1511 use Try::Tiny;
  5         7  
  5         245  
12 5     5   20 use Scalar::Util qw(blessed);
  5         160  
  5         1082  
13              
14             =head1 NAME
15              
16             Articulate::Service::SimplePreviews - provide preview
17              
18             =cut
19              
20             =head1 METHODS
21              
22             =head3 handle_preview
23              
24             preview => {
25             meta => {}
26             content => '...',
27             location => '...'
28             }
29              
30             This is in almost all respects identical to the C verb in L with the exception that nothing is written.
31              
32             Throws an error if the content already exists or if the user has no write permission on that location.
33              
34             =cut
35              
36             sub handle_preview {
37 0     0 1   my $self = shift;
38 0           my $request = shift;
39              
40 0           my $item =
41             blessed $request->data
42             ? $request->data
43             : $self->construction->construct(
44 0 0         { ( %{ $request->data } ? %{ $request->data } : () ), } );
  0 0          
45              
46 0           my $location = $item->location;
47              
48 0           my $user = $request->user_id;
49 0           my $permission = $self->authorisation->permitted( $user, write => $location );
50              
51 0 0         if ($permission)
52             { # no point offering this service to people who can't write there
53              
54 0 0         $self->validation->validate($item)
55             or throw_error BadRequest => 'The content did not validate'; # or throw
56 0           $self->enrichment->enrich( $item, $request ); # this will throw if it fails
57              
58             # skip the storage interaction
59 0           my $item_class = $item->location->[-2];
60              
61 0           $self->augmentation->augment($item_class); # this will throw if it fails
62              
63 0           return response $item_class, {
64             $item_class => {
65             schema => $item->meta->{schema},
66             content => $item->content,
67             location => $item->location, # as string or arrayref?
68             },
69             };
70             }
71             else {
72 0           throw_error Forbidden => $permission->reason;
73             }
74             }
75              
76             1;