File Coverage

blib/lib/App/Notifier/Service.pm
Criterion Covered Total %
statement 26 37 70.2
branch 0 6 0.0
condition 0 5 0.0
subroutine 9 11 81.8
pod n/a
total 35 59 59.3


line stmt bran cond sub pod time code
1             package App::Notifier::Service;
2             $App::Notifier::Service::VERSION = '0.0900';
3 2     2   1327 use 5.014;
  2         7  
4              
5 2     2   945 use Mojolicious::Lite -signatures;
  2         1148367  
  2         16  
6 2     2   62984 use Plack::Builder;
  2         19178  
  2         170  
7              
8 2     2   18 use File::Spec ();
  2         5  
  2         44  
9 2     2   10 use JSON::MaybeXS qw( decode_json );
  2         6  
  2         127  
10 2     2   1006 use YAML::XS qw( LoadFile );
  2         6112  
  2         129  
11 2     2   1276 use List::MoreUtils qw();
  2         28163  
  2         76  
12              
13 2     2   21 use POSIX ":sys_wait_h";
  2         5  
  2         22  
14 2     2   417 use Errno;
  2         5  
  2         2486  
15              
16             sub _REAPER
17             {
18 0     0     local $!; # don't let waitpid() overwrite current error
19 0   0       while ( ( my $pid = waitpid( -1, WNOHANG ) ) > 0 && WIFEXITED($?) )
20             {
21             }
22 0           $SIG{CHLD} = \&_REAPER; # loathe SysV
23             }
24              
25             # $SIG{CHLD} = \&_REAPER;
26              
27             my $config_fn = ( $ENV{'NOTIFIER_CONFIG'}
28             || File::Spec->catfile( $ENV{HOME}, '.app_notifier.yml' ) );
29              
30             my $config;
31              
32             sub _process_cmd_line_arg
33             {
34 0     0     my ( $arg, $text_params ) = @_;
35              
36 0 0         if ( ref($arg) eq '' )
    0          
37             {
38 0           return $arg;
39             }
40             elsif ( ref($arg) eq 'HASH' )
41             {
42 0 0         if ( $arg->{type} eq 'text_param' )
43             {
44 0   0       return ( $text_params->{ $arg->{param_name} } // '' );
45             }
46             else
47             {
48 0           die +{ msg => "Unknown special argument type $arg->{type}", };
49             }
50             }
51             else
52             {
53 0           die +{ msg =>
54             "Unhandled perl type for argument in command line template (should be string or hash.",
55             };
56             }
57              
58 0           return;
59             }
60              
61             get '/notify' => sub {
62             my $c = shift;
63              
64             $config ||= LoadFile($config_fn);
65              
66             my $cmd_id = ( $c->req->param('cmd_id') || 'default' );
67             my $text_params = {};
68             if ( defined( my $text_params_as_json = $c->req->param('text_params') ) )
69             {
70             $text_params = decode_json($text_params_as_json);
71             if ( ref($text_params) ne 'HASH' )
72             {
73             return "Invalid text_params - should be a JSON hash.\n";
74             }
75             elsif ( List::MoreUtils::any { ref($_) ne '' } values(%$text_params) )
76             {
77             return "Invalid text_params - all values must be strings.\n";
78             }
79             }
80             my $cmd = $config->{commands}->{$cmd_id};
81              
82             if ( defined($cmd) )
83             {
84             my @before_cmd_line = ( ( ref($cmd) eq 'ARRAY' ) ? @$cmd : $cmd );
85              
86             my @cmd_line = eval {
87             map { _process_cmd_line_arg( $_, $text_params ); } @before_cmd_line;
88             };
89              
90             if ( my $Err = $@ )
91             {
92             if ( ref($Err) eq 'HASH' and ( exists( $Err->{msg} ) ) )
93             {
94             return ( $Err->{msg} . "\n" );
95             }
96             else
97             {
98             die $Err;
99             }
100             }
101              
102             my $pid;
103             if ( !defined( $pid = fork() ) )
104             {
105             die "Cannot fork: $!";
106             }
107             elsif ( !$pid )
108             {
109             # I'm the child.
110             if ( fork() eq 0 )
111             {
112             # I'm the grandchild.
113             system { $cmd_line[0] } @cmd_line;
114             }
115             POSIX::_exit(0);
116             }
117             else
118             {
119             waitpid( $pid, 0 );
120             }
121             $c->render( text => "Success.\n" );
122             }
123             else
124             {
125             $c->log("Unknown Command ID '$cmd_id'.");
126             return "Unknown Command ID.\n";
127             }
128             };
129              
130             get '/' => sub {
131             my $c = shift;
132             $c->render( text => "OK\n" );
133             };
134              
135             builder
136             {
137             # enable 'Deflater';
138             app->start;
139             };
140              
141             # "true";
142              
143             # start;
144              
145             __END__
146              
147             =pod
148              
149             =encoding utf-8
150              
151             =head1 NAME
152              
153             App::Notifier::Service - an HTTP service for the notifier application for
154             notifying that an event (such as the finish of a task) occured.
155              
156             =head1 VERSION
157              
158             version 0.0900
159              
160             =head1 SYNOPSIS
161              
162             # Prepare a YAML file in ~/.app_notifier.yml with content like this:
163              
164             $ cat <<EOF > ~/.app_notifier.yml
165             commands:
166             # These entries contain command lines that get invoked after the
167             # notification is received. They can be either strings or arrays.
168             default:
169             - /home/shlomif/bin/desktop-finish-cue
170             shine:
171             - /home/shlomif/bin/desktop-finish-cue
172             - "--song"
173             - "/home/music/Music/dosd-mp3s/Carmen and Camille - Shine 4U"
174             - "--message"
175             - type: "text_param"
176             param_name: "msg"
177             EOF
178              
179             # Run the Dancer application from the distribution's root directory.
180             plackup ./bin/app.psgi
181              
182             # Alternatively run the following Perl code:
183             use Dancer2 0.300003;
184             use App::Notifier::Service;
185             start;
186              
187             # When you want to notify that an event occured:
188             $ curl 'http://127.0.0.1:3000/notify'
189             $ curl 'http://127.0.0.1:3000/notify?cmd_id=shine'
190              
191             =head1 AUTHOR
192              
193             Shlomi Fish, L<http://www.shlomifish.org/> .
194              
195             =head1 ACKNOWLEDGEMENTS
196              
197             =head1 COPYRIGHT & LICENSE
198              
199             Copyright 2012 Shlomi Fish.
200              
201             This program is distributed under the MIT (X11) License:
202             L<http://www.opensource.org/licenses/mit-license.php>
203              
204             Permission is hereby granted, free of charge, to any person
205             obtaining a copy of this software and associated documentation
206             files (the "Software"), to deal in the Software without
207             restriction, including without limitation the rights to use,
208             copy, modify, merge, publish, distribute, sublicense, and/or sell
209             copies of the Software, and to permit persons to whom the
210             Software is furnished to do so, subject to the following
211             conditions:
212              
213             The above copyright notice and this permission notice shall be
214             included in all copies or substantial portions of the Software.
215              
216             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
217             EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
218             OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
219             NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
220             HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
221             WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
222             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
223             OTHER DEALINGS IN THE SOFTWARE.
224              
225             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
226              
227             =head1 SUPPORT
228              
229             =head2 Websites
230              
231             The following websites have more information about this module, and may be of help to you. As always,
232             in addition to those websites please use your favorite search engine to discover more resources.
233              
234             =over 4
235              
236             =item *
237              
238             MetaCPAN
239              
240             A modern, open-source CPAN search engine, useful to view POD in HTML format.
241              
242             L<https://metacpan.org/release/App-Notifier-Service>
243              
244             =item *
245              
246             RT: CPAN's Bug Tracker
247              
248             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
249              
250             L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-Notifier-Service>
251              
252             =item *
253              
254             CPANTS
255              
256             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
257              
258             L<http://cpants.cpanauthors.org/dist/App-Notifier-Service>
259              
260             =item *
261              
262             CPAN Testers
263              
264             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
265              
266             L<http://www.cpantesters.org/distro/A/App-Notifier-Service>
267              
268             =item *
269              
270             CPAN Testers Matrix
271              
272             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
273              
274             L<http://matrix.cpantesters.org/?dist=App-Notifier-Service>
275              
276             =item *
277              
278             CPAN Testers Dependencies
279              
280             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
281              
282             L<http://deps.cpantesters.org/?module=App::Notifier::Service>
283              
284             =back
285              
286             =head2 Bugs / Feature Requests
287              
288             Please report any bugs or feature requests by email to C<bug-app-notifier-service at rt.cpan.org>, or through
289             the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=App-Notifier-Service>. You will be automatically notified of any
290             progress on the request by the system.
291              
292             =head2 Source Code
293              
294             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
295             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
296             from your repository :)
297              
298             L<https://github.com/shlomif/app-notifier>
299              
300             git clone git://github.com/shlomif/app-notifier.git
301              
302             =head1 AUTHOR
303              
304             Shlomi Fish <shlomif@cpan.org>
305              
306             =head1 BUGS
307              
308             Please report any bugs or feature requests on the bugtracker website
309             L<https://github.com/shlomif/app-notifier/issues>
310              
311             When submitting a bug or request, please include a test-file or a
312             patch to an existing test-file that illustrates the bug or desired
313             feature.
314              
315             =head1 COPYRIGHT AND LICENSE
316              
317             This software is Copyright (c) 2021 by Shlomi Fish.
318              
319             This is free software, licensed under:
320              
321             The MIT (X11) License
322              
323             =cut