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 4     4   2969 use strict;
  4         11  
  4         169  
4 4     4   26 use warnings;
  4         6  
  4         118  
5              
6 4     4   22 use Articulate::Syntax;
  4         6  
  4         30  
7              
8 4     4   6872 use Moo;
  4         11  
  4         27  
9             with 'Articulate::Role::Service';
10              
11 4     4   1415 use Try::Tiny;
  4         10  
  4         285  
12 4     4   24 use Scalar::Util qw(blessed);
  4         11  
  4         1081  
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 = blessed $request->data ? $request->data : $self->construction->construct( {
41 0 0         (%{$request->data} ? %{$request->data} : ()),
  0 0          
42             } );
43              
44 0           my $location = $item->location;
45              
46 0           my $user = $request->user_id;
47 0           my $permission = $self->authorisation->permitted ( $user, write => $location );
48              
49 0 0         if ( $permission ) { # no point offering this service to people who can't write there
50              
51 0 0         $self->validation->validate ($item) or throw_error BadRequest => 'The content did not validate'; # or throw
52 0           $self->enrichment->enrich ($item, $request); # this will throw if it fails
53              
54             # skip the storage interaction
55 0           my $item_class = $item->location->[-2];
56              
57 0           $self->augmentation->augment ($item_class); # this will throw if it fails
58              
59 0           return response $item_class, {
60             $item_class => {
61             schema => $item->meta->{schema},
62             content => $item->content,
63             location => $item->location, # as string or arrayref?
64             },
65             };
66             }
67             else {
68 0           throw_error Forbidden => $permission->reason;
69             }
70             }
71              
72             1;