blib/lib/SignalWire/Agents/DataMap.pm

filter: Statementstmt 89.1% 123 / 138 Branchbran 70.8% 34 / 48 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 14 totaltotal 79.3% 188 / 237 CC 29 SCAR 13.6
CC
29
Cov
84%
to_swaig_function
8.0
webhook_expressions
6.0
params
6.0
CRAP 3.9
« blib/lib/SignalWire/Agents/Relay/Action.pm ↑ Summary blib/lib/SignalWire/Agents/Skills/Builtin/McpGateway.pm »
1 package SignalWire::Agents::DataMap;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 has 'function_name' => (
8 is => 'ro',
9 required => 1,
10 );
11  
12 has '_purpose' => (
13 is => 'rw',
14 14 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 );
16  
17 has '_parameters' => (
18 is => 'rw',
19 14 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 );
21  
22 has '_required_params' => (
23 is => 'rw',
24 14 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 );
26  
27 has '_expressions' => (
28 is => 'rw',
29 14 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 );
31  
32 has '_webhooks' => (
33 is => 'rw',
34 14 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 );
36  
37 has '_output' => (
38 is => 'rw',
39 14 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 );
41  
42 has '_error_keys' => (
43 is => 'rw',
44 14 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 );
46  
47 # Constructor shortcut: DataMap->new("name") or DataMap->new(function_name => "name")
48 14 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
49 14 my ($orig, $class, @args) = @_;
50 14 ▶ if (@args == 1 && !ref $args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and not ref $args[0]) 13 1 14
Condition66%2/3
@args == 1 and not ref $args[0]
!ll&&!rl&&r
1013
MC/DC50%1/2
@args == 1 and not ref $args[0]
@args == 1 not ref $args[0]
Truth table66%2/3
A and not(B)
A= @args == 1B= not ref $args[0]
ABresult
0X0
100
111
51 13 return $class->$orig(function_name => $args[0]);
52 }
53 1 return $class->$orig(@args);
54 1 };
55  
56 11 ▶sub purpose {
Subroutine purpose: called CC=1 SCAR=0.0
Pod undocumented
57 11 my ($self, $desc) = @_;
58 11 $self->_purpose($desc);
59 11 return $self;
60 }
61  
62 1 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod undocumented
63 1 my ($self, $desc) = @_;
64 1 return $self->purpose($desc);
65 }
66  
67 7 ▶sub parameter {
Subroutine parameter: called CC=5 SCAR=16.6
Pod undocumented
68 7 my ($self, $name, $type, $description, %opts) = @_;
69 7 ▶ my $required = $opts{required} // 0;
Condition50%1/2
$opts{"required"} // 0
l!l
70
MC/DC0%0/1
$opts{"required"} // 0
$opts{"required"}
Truth table50%1/2
A
A= $opts{"required"}
Aresult
11
00
70 7 my $enum = $opts{enum};
71  
72 7 my %param_def = (
73 type => $type,
74 description => $description,
75 );
76 7 ▶ $param_def{enum} = $enum if $enum;
Branch: 1 branch
BranchTrueFalseTotal
if $enum 1 6 7
77  
78 7 $self->_parameters->{$name} = \%param_def;
79  
80 7 ▶ if ($required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required) 7 0 7
81 7 my $req = $self->_required_params;
82 0 ▶ push @$req, $name unless grep { $_ eq $name } @$req;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @$req 7 0 7
83 }
84  
85 7 return $self;
86 }
87  
88 2 ▶sub expression {
Subroutine expression: called CC=3 SCAR=12.0
Pod undocumented
89 2 my ($self, $test_value, $pattern, $output, %opts) = @_;
90 2 my $nomatch_output = $opts{nomatch_output};
91  
92 # If pattern is a compiled regex, extract the string
93 2 ▶ if (ref $pattern eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "Regexp") 0 2 2
94 0 $pattern = "$pattern";
95 # Strip Perl regex delimiters: (?^:...) or (?^u:...)
96 0 $pattern =~ s/^\(\?[\^a-z]*://;
97 0 $pattern =~ s/\)$//;
98 }
99  
100 2 my %expr = (
101 string => $test_value,
102 pattern => $pattern,
103 output => $output->to_hash,
104 );
105 2 ▶ $expr{'nomatch-output'} = $nomatch_output->to_hash if $nomatch_output;
Branch: 1 branch
BranchTrueFalseTotal
if $nomatch_output 0 2 2
106  
107 2 push @{ $self->_expressions }, \%expr;
108 2 return $self;
109 }
110  
111 8 ▶sub webhook {
Subroutine webhook: called CC=5 SCAR=16.2
Pod undocumented
112 8 my ($self, $method, $url, %opts) = @_;
113 8 my $headers = $opts{headers};
114 8 my $form_param = $opts{form_param};
115 8 my $input_args_as_params = $opts{input_args_as_params};
116 8 my $require_args = $opts{require_args};
117  
118 8 my %wh = (
119 url => $url,
120 method => uc($method),
121 );
122 8 ▶ $wh{headers} = $headers if $headers;
Branch: 1 branch
BranchTrueFalseTotal
if $headers 1 7 8
123 8 ▶ $wh{form_param} = $form_param if $form_param;
Branch: 1 branch
BranchTrueFalseTotal
if $form_param 0 8 8
124 8 ▶ $wh{input_args_as_params} = JSON::true if $input_args_as_params;
Branch: 1 branch
BranchTrueFalseTotal
if $input_args_as_params 0 8 8
125 8 ▶ $wh{require_args} = $require_args if $require_args;
Branch: 1 branch
BranchTrueFalseTotal
if $require_args 0 8 8
126  
127 8 push @{ $self->_webhooks }, \%wh;
128 8 return $self;
129 }
130  
131 0 ▶sub webhook_expressions {
Subroutine webhook_expressions: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my ($self, $expressions) = @_;
133 die "Must add webhook before setting webhook expressions"
134 0 ▶ unless @{ $self->_webhooks };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_webhooks;} 0 0 0
135 0 $self->_webhooks->[-1]{expressions} = $expressions;
136 0 return $self;
137 }
138  
139 3 ▶sub body {
Subroutine body: called CC=2 SCAR=6.9
Pod undocumented
140 3 my ($self, $data) = @_;
141 die "Must add webhook before setting body"
142 3 ▶ unless @{ $self->_webhooks };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_webhooks;} 1 2 3
143 2 $self->_webhooks->[-1]{body} = $data;
144 2 return $self;
145 }
146  
147 0 ▶sub params {
Subroutine params: not called CC=2 SCAR=17.9
Pod undocumented
148 0 my ($self, $data) = @_;
149 die "Must add webhook before setting params"
150 0 ▶ unless @{ $self->_webhooks };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_webhooks;} 0 0 0
151 0 $self->_webhooks->[-1]{params} = $data;
152 0 return $self;
153 }
154  
155 3 ▶sub foreach {
Subroutine foreach: called CC=5 SCAR=16.1
Pod undocumented
156 3 my ($self, $config) = @_;
157 die "Must add webhook before setting foreach"
158 3 ▶ unless @{ $self->_webhooks };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_webhooks;} 1 2 3
159 2 ▶ die "foreach_config must be a hashref" unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 0 2 2
160  
161 2 for my $key (qw(input_key output_key append)) {
162 die "foreach config missing required key: $key"
163 5 ▶ unless exists $config->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $config->{$key} 1 4 5
164 }
165  
166 1 $self->_webhooks->[-1]{foreach} = $config;
167 1 return $self;
168 }
169  
170 8 ▶sub output {
Subroutine output: called CC=2 SCAR=6.9
Pod undocumented
171 8 my ($self, $result) = @_;
172 die "Must add webhook before setting output"
173 8 ▶ unless @{ $self->_webhooks };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_webhooks;} 1 7 8
174 7 $self->_webhooks->[-1]{output} = $result->to_hash;
175 7 return $self;
176 }
177  
178 1 ▶sub fallback_output {
Subroutine fallback_output: called CC=1 SCAR=0.0
Pod undocumented
179 1 my ($self, $result) = @_;
180 1 $self->_output($result->to_hash);
181 1 return $self;
182 }
183  
184 1 ▶sub error_keys {
Subroutine error_keys: called CC=2 SCAR=7.2
Pod undocumented
185 1 my ($self, $keys) = @_;
186 1 ▶ if (@{ $self->_webhooks }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_webhooks;}) { } 1 0 1
187 1 $self->_webhooks->[-1]{error_keys} = $keys;
188 } else {
189 0 $self->_error_keys($keys);
190 }
191 1 return $self;
192 }
193  
194 1 ▶sub global_error_keys {
Subroutine global_error_keys: called CC=1 SCAR=0.0
Pod undocumented
195 1 my ($self, $keys) = @_;
196 1 $self->_error_keys($keys);
197 1 return $self;
198 }
199  
200 10 ▶sub to_swaig_function {
Subroutine to_swaig_function: called CC=8 SCAR=20.8
Pod undocumented
201 10 my ($self) = @_;
202  
203 # Build parameter schema
204 10 my %param_schema;
205 10 ▶ if (keys %{ $self->_parameters }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$self->_parameters;}) { } 6 4 10
206 6 $param_schema{type} = 'object';
207 6 $param_schema{properties} = { %{ $self->_parameters } };
208 6 ▶ if (@{ $self->_required_params }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_required_params;}) 6 0 6
209 6 $param_schema{required} = [ @{ $self->_required_params } ];
210 }
211 } else {
212 4 %param_schema = (type => 'object', properties => {});
213 }
214  
215 # Build data_map
216 10 my %data_map;
217 10 ▶ if (@{ $self->_expressions }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_expressions;}) 1 9 10
218 1 $data_map{expressions} = $self->_expressions;
219 }
220 10 ▶ if (@{ $self->_webhooks }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_webhooks;}) 5 5 10
221 5 $data_map{webhooks} = $self->_webhooks;
222 }
223 10 ▶ if (defined $self->_output) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_output) 1 9 10
224 1 $data_map{output} = $self->_output;
225 }
226 10 ▶ if (@{ $self->_error_keys }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_error_keys;}) 1 9 10
227 1 $data_map{error_keys} = $self->_error_keys;
228 }
229  
230 return {
231 10 ▶ function => $self->function_name,
Condition100%2/2
$self->_purpose || 'Execute ' . $self->function_name
l!l
91
MC/DC100%1/1
$self->_purpose || 'Execute ' . $self->function_name
$self->_purpose
Truth table100%2/2
A
A= $self->_purpose
Aresult
11
00
232 description => $self->_purpose || "Execute " . $self->function_name,
233 parameters => \%param_schema,
234 data_map => \%data_map,
235 };
236 }
237  
238 1 1;
« blib/lib/SignalWire/Agents/Relay/Action.pm ↑ Summary blib/lib/SignalWire/Agents/Skills/Builtin/McpGateway.pm »