File Coverage

blib/lib/Interact/Later.pm
Criterion Covered Total %
statement 47 55 85.4
branch 3 4 75.0
condition n/a
subroutine 14 17 82.3
pod 4 6 66.6
total 68 82 82.9


line stmt bran cond sub pod time code
1             package Interact::Later;
2              
3 1     1   581607 use 5.020;
  1         4  
4 1     1   8 use strict;
  1         9  
  1         21  
5 1     1   8 use warnings;
  1         3  
  1         54  
6              
7              
8 1     1   6 use Storable qw( store retrieve );
  1         2  
  1         62  
9 1     1   530 use Path::Class qw/dir/;
  1         10091  
  1         59  
10 1     1   627 use Moose;
  1         471120  
  1         8  
11 1     1   8821 use Data::UUID;
  1         1088  
  1         67  
12 1     1   516 use File::Find::Rule;
  1         8800  
  1         7  
13 1     1   65 use Data::Printer;
  1         3  
  1         10  
14              
15 1     1   775 use experimental 'signatures';
  1         3697  
  1         6  
16 1     1   185 no if ( $] >= 5.018 ), 'warnings' => 'experimental';
  1         2  
  1         9  
17              
18             has 'cache_path' => (
19             is => 'ro',
20             isa => 'Str',
21             trigger => sub {
22              
23             # https://stackoverflow.com/a/1415884/954777
24             # $_[0] is the new value
25             # $_[1] is the old value
26             $_[ 0 ]->{ cache_path } = dir( $_[ 1 ] )->absolute->stringify() . '/';
27             }
28             );
29              
30             has 'file_extension' => (
31             is => 'ro',
32             isa => 'Str'
33             );
34              
35 0     0 0 0 sub get_oldest_file_in_cache($self) {
  0         0  
  0         0  
36 0         0 my @files = File::Find::Rule
37             ->file
38             ->name( '*' . $self->file_extension );
39              
40             # https://stackoverflow.com/a/7585306/954777
41             }
42              
43 0     0 0 0 sub release_cache($id) { }
  0         0  
44              
45 1     1 1 13329 sub clean_cache($self) {
  1         2  
  1         2  
46 1         41 while ( glob $self->cache_path . '*' . $self->file_extension ) {
47 1000 50       80413 unlink $_ or warn("Can't remove $_: $!");
48             }
49              
50             }
51              
52             sub generate_uuid {
53 1000     1000 1 619324 my $uuid = Data::UUID->new->create_str();
54 1000         775570 return $uuid;
55             }
56              
57 1000     1000 1 5906919 sub write_data_to_disk ( $self, $data ) {
  1000         2468  
  1000         2037  
  1000         1464  
58 1000 100       44231 if ( not -d $self->cache_path ) {
59 1         48 mkdir $self->cache_path;
60 1         81 say 'created cache directory';
61             }
62 1000         36119 store \$data, $self->cache_path . generate_uuid() . $self->file_extension;
63             }
64              
65 0     0 1   sub retrieve_data_from_disk($id) {
  0            
66              
67             }
68              
69              
70             =encoding UTF-8
71              
72             =head1 NAME
73              
74             Interact::Later - Delay some tasks for later.
75              
76             =head1 VERSION
77              
78             Version 0.03
79              
80             =cut
81              
82             our $VERSION = '0.03';
83              
84              
85             =head1 SYNOPSIS
86              
87             Can be used typically when you receive lots of POST requests that you don't want
88             to proceed right now. This module will fastly store the data content on disk
89             without the need to use a database.
90              
91             use Interact::Later;
92              
93             my $delayer = Interact::Later->new(
94             cache_path => './cache/',
95             file_extension => '.dmp'
96             );
97              
98             $delayer->write_data_to_disk($data);
99              
100             # Later...
101             $delayer->clean_cache;
102              
103             =head1 EXPORT
104              
105             A list of functions that can be exported. You can delete this section
106             if you don't export anything, such as for a purely object-oriented module.
107              
108             =head1 SUBROUTINES/METHODS
109              
110             =head2 get_oldest_file_in_the_cache
111              
112             Retrieve the oldest file in the cache
113              
114             =head2 clean_cache
115              
116             =head2 generate_uuid
117              
118             =head2 write_data_to_disk
119              
120             Writes the cache files to disk using C<Storable>. It also checks that the cache
121             path exists and if not, it creates it.
122              
123             =head2 retrieve_data_from_disk
124              
125              
126             =head1 AUTHOR
127              
128             Sébastien Feugère, C<< <smonff at riseup.net> >>
129              
130             =head1 BUGS
131              
132             Please report any bugs or feature requests to C<bug-interact-later at rt.cpan.org>, or through
133             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Interact-Later>. I will be notified, and then you'll
134             automatically be notified of progress on your bug as I make changes.
135              
136              
137              
138              
139             =head1 SUPPORT
140              
141             You can find documentation for this module with the perldoc command.
142              
143             perldoc Interact::Later
144              
145              
146             You can also look for information at:
147              
148             =over 4
149              
150             =item * Gitlab: Gitlab issues tracker (report bugs here)
151              
152             L<http://gitlab.com/smonff/Interact-Later>
153              
154             =item * AnnoCPAN: Annotated CPAN documentation
155              
156             L<http://annocpan.org/dist/Interact-Later>
157              
158             =item * CPAN Ratings
159              
160             L<http://cpanratings.perl.org/d/Interact-Later>
161              
162             =item * Search CPAN
163              
164             L<http://search.cpan.org/dist/Interact-Later/>
165              
166             =back
167              
168              
169             =head1 ACKNOWLEDGEMENTS
170              
171              
172             =head1 LICENSE AND COPYRIGHT
173              
174             Copyright 2019 Sébastien Feugère.
175              
176             This program is free software; you can redistribute it and/or modify it
177             under the terms of the the Artistic License (2.0). You may obtain a
178             copy of the full license at:
179              
180             L<http://www.perlfoundation.org/artistic_license_2_0>
181              
182             Any use, modification, and distribution of the Standard or Modified
183             Versions is governed by this Artistic License. By using, modifying or
184             distributing the Package, you accept this license. Do not use, modify,
185             or distribute the Package, if you do not accept this license.
186              
187             If your Modified Version has been derived from a Modified Version made
188             by someone other than you, you are nevertheless required to ensure that
189             your Modified Version complies with the requirements of this license.
190              
191             This license does not grant you the right to use any trademark, service
192             mark, tradename, or logo of the Copyright Holder.
193              
194             This license includes the non-exclusive, worldwide, free-of-charge
195             patent license to make, have made, use, offer to sell, sell, import and
196             otherwise transfer the Package with respect to any patent claims
197             licensable by the Copyright Holder that are necessarily infringed by the
198             Package. If you institute patent litigation (including a cross-claim or
199             counterclaim) against any party alleging that the Package constitutes
200             direct or contributory patent infringement, then this Artistic License
201             to you shall terminate on the date that such litigation is filed.
202              
203             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
204             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
205             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
206             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
207             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
208             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
209             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
210             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
211              
212              
213             =cut
214              
215             1; # End of Interact::Later