Zapp/Trigger.pm

filter: Statementstmt 37.5% 15 / 40 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 40.0% 4 / 10 Podpod 33.3% 2 / 6 totaltotal 25.6% 21 / 82 CC 11 SCAR 25.3
CC
11
Cov
26%
create
30.0
set
12.0
config_field
12.0
CRAP 12.6
« Zapp/Controller/Run.pm ↑ Summary Zapp/Task/GetOAuth2Token.pm »
1 package Zapp::Trigger;
2 # ABSTRACT: Trigger a plan from an event
3  
4 #pod =head1 SYNOPSIS
5 #pod
6 #pod package My::Trigger;
7 #pod use Mojo::Base 'Zapp::Trigger', -signatures;
8 #pod
9 #pod sub install( $self, $app, $config={} ) {
10 #pod $self->SUPER::install( $app, $config );
11 #pod # Set up trigger to call $self->enqueue when needed
12 #pod }
13 #pod
14 #pod __DATA__
15 #pod @@ config.html.ep
16 #pod %# Form to configure trigger
17 #pod
18 #pod =head1 DESCRIPTION
19 #pod
20 #pod This is the base class for Zapp triggers. Triggers handle events and run
21 #pod configured plans. Triggers can accept configuration and plan input.
22 #pod
23 #pod =head1 SEE ALSO
24 #pod
25 #pod L<Zapp>
26 #pod
27 #pod =cut
28  
29 2 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Mojo::JSON qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use Mojo::Loader qw( data_section );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 #pod =attr app
35 #pod
36 #pod The application object.
37 #pod
38 #pod =cut
39  
40 2 has app => ;
41  
42 #pod =attr moniker
43 #pod
44 #pod The name of the trigger. Multiple configurations for a trigger class
45 #pod should have different names.
46 #pod
47 #pod =cut
48  
49 2 has moniker => ;
50  
51 #pod =method install
52 #pod
53 #pod Called automatically when adding the trigger. Should be overridden to set up
54 #pod any routes, timers, connections, or other kind of listeners to fire the
55 #pod configured triggers.
56 #pod
57 #pod =cut
58  
59 0 ▶sub install( $self, $app, $config={} ) {
Subroutine install: not called CC=2 SCAR=17.9
Pod documented
60 0 $self->app( $app );
61 }
62  
63 ▶sub create( $self, $data ) {
Subroutine create: not called CC=5 SCAR=34.0
Pod undocumented
64 # Created a new trigger
65 0 ▶ $data->{class} //= blessed $self;
Condition0%0/3
$data->{"class"} //= builtin::blessed($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"class"} //= builtin::blessed($self)
$data->{"class"} builtin::blessed($self)
Truth table0%0/3
A //= B
A= $data->{"class"}B= builtin::blessed($self)
ABresult
1X1
011
000
66 0 ▶ $data->{state} //= "inactive";
Condition0%0/2
$data->{"state"} //= "inactive"
l!l
00
MC/DC0%0/1
$data->{"state"} //= "inactive"
$data->{"state"}
Truth table0%0/2
A
A= $data->{"state"}
Aresult
11
00
67 # XXX: Auto-encode JSON in Yancy
68 0 ▶ $data->{config} = encode_json( $data->{config} // {} );
Condition0%0/2
$data->{"config"} // {}
l!l
00
MC/DC0%0/1
$data->{"config"} // {}
$data->{"config"}
Truth table0%0/2
A
A= $data->{"config"}
Aresult
11
00
69 0 ▶ $data->{input} = encode_json( $data->{input} // {} );
Condition0%0/2
$data->{"input"} // {}
l!l
00
MC/DC0%0/1
$data->{"input"} // {}
$data->{"input"}
Truth table0%0/2
A
A= $data->{"input"}
Aresult
11
00
70  
71 0 return $self->app->yancy->create( zapp_triggers => $data );
72 }
73  
74 ▶sub delete( $self, $trigger_id ) {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
75 0 return $self->app->yancy->delete( zapp_triggers => $trigger_id );
76 }
77  
78 ▶sub set( $self, $trigger_id, $data ) {
Subroutine set: not called CC=3 SCAR=24.8
Pod undocumented
79 # XXX: Auto-encode JSON in Yancy
80 0 ▶ $data->{config} &&= encode_json( $data->{config} );
Condition0%0/3
$data->{"config"} &&= encode_json($data->{"config"})
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"config"} &&= encode_json($data->{"config"})
$data->{"config"} encode_json($data->{"config"})
Truth table0%0/3
A &&= B
A= $data->{"config"}B= encode_json($data->{"config"})
ABresult
0X0
100
111
81 0 ▶ $data->{input} &&= encode_json( $data->{input} );
Condition0%0/3
$data->{"input"} &&= encode_json($data->{"input"})
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"input"} &&= encode_json($data->{"input"})
$data->{"input"} encode_json($data->{"input"})
Truth table0%0/3
A &&= B
A= $data->{"input"}B= encode_json($data->{"input"})
ABresult
0X0
100
111
82 0 return $self->app->yancy->set( zapp_triggers => $trigger_id, $data );
83 }
84  
85 #pod =method enqueue
86 #pod
87 #pod Enqueue the plan for the given trigger ID. The plan input will be processed via
88 #pod L<Zapp::Formula/resolve>, passing in the given C<context> hash reference of data.
89 #pod Returns the run enqueued (from L<Zapp/enqueue_plan>).
90 #pod
91 #pod =cut
92  
93 ▶sub enqueue( $self, $trigger_id, $context ) {
Subroutine enqueue: not called CC=2 SCAR=17.9
Pod documented
94 # Called by the trigger to enqueue a job. Creates a row in
95 # zapp_trigger_runs automatically.
96 0 my $trigger = $self->app->yancy->get( zapp_triggers => $trigger_id );
97  
98 # Should modify $input from the trigger input to the plan input, if
99 # needed.
100 0 my $raw_input = decode_json( $trigger->{input} );
101 0 my %input;
102 0 for my $field ( keys %$raw_input ) {
103 0 my $raw_value = $raw_input->{ $field }{ value };
104 0 $input{ $field } = $self->app->formula->resolve( $raw_value, $context );
105 }
106  
107 0 my $run = $self->app->enqueue_plan( $trigger->{plan_id}, \%input );
108 $self->app->yancy->create(
109 zapp_trigger_runs => {
110 trigger_id => $trigger_id,
111 run_id => $run->{ run_id },
112 0 context => encode_json( $context ),
113 },
114 );
115  
116 0 return $run;
117 }
118  
119 0 ▶sub config_field( $self, $c, $config_value=undef ) {
Subroutine config_field: not called CC=3 SCAR=24.8
Pod undocumented
120 0 my $class = blessed $self;
121 0 my $tmpl = data_section( $class, 'config.html.ep' );
122 0 ▶ return '' if !$tmpl;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmpl 0 0 0
123 # XXX: Use Mojo::Template directly to get better names than 'inline
124 # template XXXXXXXXXXXX'?
125 0 return $c->render_to_string(
126 inline => $tmpl,
127 self => $self,
128 config => $config_value,
129 );
130 }
131  
132 2 1;
133  
134 =pod
135  
136 =head1 NAME
137  
138 Zapp::Trigger - Trigger a plan from an event
139  
140 =head1 VERSION
141  
142 version 0.004
143  
144 =head1 SYNOPSIS
145  
146 package My::Trigger;
147 use Mojo::Base 'Zapp::Trigger', -signatures;
148  
149 sub install( $self, $app, $config={} ) {
150 $self->SUPER::install( $app, $config );
151 # Set up trigger to call $self->enqueue when needed
152 }
153  
154 __DATA__
155 @@ config.html.ep
156 %# Form to configure trigger
157  
158 =head1 DESCRIPTION
159  
160 This is the base class for Zapp triggers. Triggers handle events and run
161 configured plans. Triggers can accept configuration and plan input.
162  
163 =head1 ATTRIBUTES
164  
165 =head2 app
166  
167 The application object.
168  
169 =head2 moniker
170  
171 The name of the trigger. Multiple configurations for a trigger class
172 should have different names.
173  
174 =head1 METHODS
175  
176 =head2 install
177  
178 Called automatically when adding the trigger. Should be overridden to set up
179 any routes, timers, connections, or other kind of listeners to fire the
180 configured triggers.
181  
182 =head2 enqueue
183  
184 Enqueue the plan for the given trigger ID. The plan input will be processed via
185 L<Zapp::Formula/resolve>, passing in the given C<context> hash reference of data.
186 Returns the run enqueued (from L<Zapp/enqueue_plan>).
187  
188 =head1 SEE ALSO
189  
190 L<Zapp>
191  
192 =head1 AUTHOR
193  
194 Doug Bell <preaction@cpan.org>
195  
196 =head1 COPYRIGHT AND LICENSE
197  
198 This software is copyright (c) 2021 by Doug Bell.
199  
200 This is free software; you can redistribute it and/or modify it under
201 the same terms as the Perl 5 programming language system itself.
202  
203 =cut
204  
205 __DATA__
« Zapp/Controller/Run.pm ↑ Summary Zapp/Task/GetOAuth2Token.pm »