blib/lib/SignalWire/Skills/Builtin/WeatherApi.pm

filter: Statementstmt 94.7% 36 / 38 Branchbran 87.5% 7 / 8 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 3 totaltotal 91.1% 62 / 68 CC 8 SCAR 14.7
CC
8
Cov
94%
register_tools
8.1
BEGIN
1.0
BEGIN
1.0
CRAP 4.3
« examples/swmlservice_swaig_standalone.pl ↑ Summary blib/lib/SignalWire/DataMap.pm »
1 package SignalWire::Skills::Builtin::WeatherApi;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 extends 'SignalWire::Skills::SkillBase';
6  
7 4 ▶use SignalWire::Skills::SkillRegistry;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 SignalWire::Skills::SkillRegistry->register_skill('weather_api', __PACKAGE__);
9  
10 7 ▶has '+skill_name' => (default => sub { 'weather_api' });
Subroutine __ANON__: called CC=1 SCAR=0.0
11 7 ▶has '+skill_description' => (default => sub { 'Get current weather information from WeatherAPI.com' });
Subroutine __ANON__: called CC=1 SCAR=0.0
12 7 ▶has '+supports_multiple_instances' => (default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 6 ▶sub setup { 1 }
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
15  
16 5 ▶sub register_tools {
Subroutine register_tools: called CC=8 SCAR=20.9
Pod undocumented
17 5 my ($self) = @_;
18 5 ▶ my $tool_name = $self->params->{tool_name} // 'get_weather';
Condition100%2/2
$self->params->{"tool_name"} // "get_weather"
l!l
23
MC/DC100%1/1
$self->params->{"tool_name"} // "get_weather"
$self->params->{"tool_name"}
Truth table100%2/2
A
A= $self->params->{"tool_name"}
Aresult
11
00
19 5 ▶ my $api_key = $self->params->{api_key} // '';
Condition100%2/2
$self->params->{"api_key"} // ""
l!l
41
MC/DC100%1/1
$self->params->{"api_key"} // ""
$self->params->{"api_key"}
Truth table100%2/2
A
A= $self->params->{"api_key"}
Aresult
11
00
20 5 ▶ my $unit = $self->params->{temperature_unit} // 'fahrenheit';
Condition100%2/2
$self->params->{"temperature_unit"} // "fahrenheit"
l!l
14
MC/DC100%1/1
$self->params->{"temperature_unit"} // "fahrenheit"
$self->params->{"temperature_unit"}
Truth table100%2/2
A
A= $self->params->{"temperature_unit"}
Aresult
11
00
21  
22 5 ▶ my $temp_field = $unit eq 'celsius' ? 'temp_c' : 'temp_f';
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'celsius' ? : 1 4 5
23 5 ▶ my $feels_field = $unit eq 'celsius' ? 'feelslike_c' : 'feelslike_f';
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'celsius' ? : 1 4 5
24 5 ▶ my $unit_label = $unit eq 'celsius' ? 'C' : 'F';
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'celsius' ? : 1 4 5
25  
26 # Honor WEATHER_API_BASE_URL env var so the audit fixture
27 # (audit_skills_dispatch.py) can redirect us at a local HTTP server.
28 # When unset we use the canonical
29 # https://api.weatherapi.com/v1/current.json URL. Either way the
30 # `current.json` path component is preserved (the audit's
31 # expected_path_substring is `current.json`).
32 5 my $base = $ENV{WEATHER_API_BASE_URL};
33 5 my $url;
34 5 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) { } 0 5 5
35 0 $base =~ s{/+$}{};
36 0 $url = "$base/v1/current.json?key=${api_key}&q=\${lc:enc:args.location}&aqi=no";
37 } else {
38 5 $url = "https://api.weatherapi.com/v1/current.json?key=${api_key}&q=\${lc:enc:args.location}&aqi=no";
39 }
40  
41 5 $self->agent->register_swaig_function({
42 function => $tool_name,
43 description => 'Get current weather information for any location',
44 parameters => {
45 type => 'object',
46 properties => {
47 location => { type => 'string', description => 'Location to get weather for' },
48 },
49 required => ['location'],
50 },
51 data_map => {
52 webhooks => [{
53 method => 'GET',
54 url => $url,
55 output => {
56 response => "Temperature: \${current.${temp_field}}${unit_label}, "
57 . "Feels like: \${current.${feels_field}}${unit_label}, "
58 . "Condition: \${current.condition.text}, "
59 . "Humidity: \${current.humidity}%, "
60 . "Wind: \${current.wind_mph} mph",
61 },
62 }],
63 },
64 });
65 }
66  
67 1 ▶sub get_parameter_schema {
Subroutine get_parameter_schema: called CC=1 SCAR=0.0
Pod undocumented
68 return {
69 1 %{ SignalWire::Skills::SkillBase->get_parameter_schema },
70 api_key => { type => 'string', required => 1, hidden => 1 },
71 tool_name => { type => 'string', default => 'get_weather' },
72 temperature_unit => { type => 'string', enum => ['fahrenheit', 'celsius'], default => 'fahrenheit' },
73 };
74 }
75  
76 4 1;
« examples/swmlservice_swaig_standalone.pl ↑ Summary blib/lib/SignalWire/DataMap.pm »