blib/lib/VisualCrossing/API.pm

filter: Statementstmt 81.2% 91 / 112 Branchbran 56.4% 35 / 62 Conditioncond 55.5% 10 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 2 totaltotal 69.4% 152 / 219 CC 38 SCAR 40.5
CC
38
Cov
71%
_getUrl
103.0
BUILD
8.2
__ANON__
2.1
CRAP 57.4
↑ Summary
1 # ABSTRACT: provides perl API to VisualCrossing
2 package VisualCrossing::API;
3  
4 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '1.0.0';
11  
12 4 my $DEBUG = 0;
13  
14 4 my $api = "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline";
15 4 my $docs = "https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/";
16 4 my %unitGroups = (
17 us => 1,
18 base => 1,
19 metric => 1,
20 uk => 1,
21 );
22 4 my %includes = (
23 days => 1,
24 hours => 1,
25 current => 1,
26 events => 1,
27 obs => 1,
28 remote => 1,
29 fcst => 1,
30 stats => 1,
31 statsfcst => 1,
32 );
33  
34 has url => (
35 is => 'lazy',
36 2 ▶ 'default' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 2 my $self = shift;
38 2 return $self->_getUrl();
39 },
40 4 );
41 # key must be specified
42 has key => (
43 is => 'ro',
44 8 ▶ 'isa' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
45 8 ▶ die "Invalid key specified: see $docs\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 8 8
46 unless defined($_[0]);
47 },
48 4 required => 1,
49 );
50  
51 # location or latitude/longitude must be specified
52 4 has location => (is => 'ro');
53 4 has latitude => (is => 'ro');
54 4 has longitude => (is => 'ro');
55  
56 # date is optional
57 4 has date => (is => 'ro');
58 4 has date2 => (is => 'ro');
59  
60 # uncommon options
61 has include => (
62 is => 'ro',
63 4 ▶ 'isa' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
64 die "Invalid include specified: see $docs\n"
65 4 ▶ unless exists($includes{ $_[0] });
Branch: 1 branch
BranchTrueFalseTotal
unless exists $includes{$_[0]} 1 3 4
66 },
67 4 );
68 has unitGroup => (
69 is => 'ro',
70 1 ▶ 'isa' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
71 die "Invalid unitGroup specified: see $docs\n"
72 1 ▶ unless exists($unitGroups{ $_[0] });
Branch: 1 branch
BranchTrueFalseTotal
unless exists $unitGroups{$_[0]} 1 0 1
73 },
74 4 );
75 4 has lang => (is => 'ro');
76 4 has options => (is => 'ro');
77 4 has nonulls => (is => 'ro');
78 4 has noheaders => (is => 'ro');
79 4 has contentType => (is => 'ro');
80 4 has timezone => (is => 'ro');
81 4 has maxDistance => (is => 'ro');
82 4 has maxStations => (is => 'ro');
83 4 has elevationDifference => (is => 'ro');
84 4 has locationNames => (is => 'ro');
85 4 has forecastBasisDate => (is => 'ro');
86 4 has forecastBasisDay => (is => 'ro');
87 4 has degreeDayTempBase => (is => 'ro');
88 4 has degreeDayTempMaxThreshold => (is => 'ro');
89  
90 1 ▶sub getWeather {
Subroutine getWeather: called CC=2 SCAR=7.0
91 1 my $self = shift;
92 1 my $http = HTTP::Tiny->new;
93 1 my $url = $self->url;
94 1 my $response = $http->get($url);
95  
96 die "Request to '$url' failed: $response->{status} $response->{reason}\n"
97 1 ▶ unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 1 1
98  
99 1 my $coder = JSON->new->utf8;
100 1 my $result = $coder->decode($response->{content});
101 1 return $result;
102 }
103  
104 7 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=21.0
Pod undocumented
105 7 my ($self, $args) = @_;
106 # validate the resulting object
107  
108 7 ▶ if (defined($self->{location})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'location'}) { } 4 3 7
elsif (defined $self->{'latitude'} and defined $self->{'longitude'}) { } 0 3 3
109 # ok
110 ▶ } elsif (defined($self->{latitude}) && defined($self->{longitude})) {
Condition66%2/3
defined $self->{"latitude"} and defined $self->{"longitude"}
!ll&&!rl&&r
210
MC/DC0%0/2
defined $self->{"latitude"} and defined $self->{"longitude"}
defined $self->{"latitude"} defined $self->{"longitude"}
Truth table66%2/3
A and B
A= defined $self->{"latitude"}B= defined $self->{"longitude"}
ABresult
0X0
100
111
111 # ok
112 } else {
113 3 die "Invalid request either location or latitude/longitude must be specified: see $docs\n";
114 }
115  
116 4 ▶ if (defined($self->{date}) && defined($self->{date2})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'date'} and defined $self->{'date2'}) { } 2 2 4
elsif (not defined $self->{'date'} and defined $self->{'date2'}) { } 1 2 3
Condition100%3/3
defined $self->{"date"} and defined $self->{"date2"}
!ll&&!rl&&r
112
MC/DC100%2/2
defined $self->{"date"} and defined $self->{"date2"}
defined $self->{"date"} defined $self->{"date2"}
Truth table100%3/3
A and B
A= defined $self->{"date"}B= defined $self->{"date2"}
ABresult
0X0
100
111
117 # ok
118 ▶ } elsif (!defined($self->{date}) && defined($self->{date2})) {
Condition66%2/3
not defined $self->{"date"} and defined $self->{"date2"}
!ll&&!rl&&r
101
MC/DC50%1/2
not defined $self->{"date"} and defined $self->{"date2"}
not defined $self->{"date"} defined $self->{"date2"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"date"}B= defined $self->{"date2"}
ABresult
0X0
100
111
119 1 die "Invalid request date must exist if date2 is specified: see $docs\n";
120 }
121 }
122  
123 2 ▶sub _getUrl {
Subroutine _getUrl: called CC=27 SCAR=46.3
124 2 my ($self) = @_;
125 2 my $url = $api;
126  
127 2 ▶ if (defined($self->{location})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'location'}) { } 2 0 2
elsif (defined $self->{'latitude'} and defined $self->{'longitude'}) { } 0 0 0
128 2 $url = $url . '/' . $self->{location};
129 ▶ } elsif (defined($self->{latitude}) && defined($self->{longitude})) {
Condition0%0/3
defined $self->{"latitude"} and defined $self->{"longitude"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"latitude"} and defined $self->{"longitude"}
defined $self->{"latitude"} defined $self->{"longitude"}
Truth table0%0/3
A and B
A= defined $self->{"latitude"}B= defined $self->{"longitude"}
ABresult
0X0
100
111
130 0 $url = $url . '/' . $self->{latitude} . ',' . $self->{longitude};
131 } else {
132 0 die "Invalid request either location or latitude/longitude must be specified: see $docs\n";
133 }
134  
135 2 ▶ if (defined($self->{date}) && defined($self->{date2})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $self->{'date'} and defined $self->{'date2'}) { } 1 1 2
elsif (not defined $self->{'date'} and defined $self->{'date2'}) { } 0 1 1
elsif (defined $self->{'date'}) { } 1 0 1
Condition66%2/3
defined $self->{"date"} and defined $self->{"date2"}
!ll&&!rl&&r
011
MC/DC50%1/2
defined $self->{"date"} and defined $self->{"date2"}
defined $self->{"date"} defined $self->{"date2"}
Truth table66%2/3
A and B
A= defined $self->{"date"}B= defined $self->{"date2"}
ABresult
0X0
100
111
136 1 $url = $url . '/' . $self->{date} . '/' . $self->{date2};
137 ▶ } elsif (!defined($self->{date}) && defined($self->{date2})) {
Condition33%1/3
not defined $self->{"date"} and defined $self->{"date2"}
!ll&&!rl&&r
100
MC/DC0%0/2
not defined $self->{"date"} and defined $self->{"date2"}
not defined $self->{"date"} defined $self->{"date2"}
Truth table33%1/3
not(A) and B
A= not defined $self->{"date"}B= defined $self->{"date2"}
ABresult
0X0
100
111
138 0 die "Invalid request date must exist if date2 is specified: see $docs\n";
139 } elsif (defined($self->{date})) {
140 1 $url = $url . '/' . $self->{date} ;
141 }
142  
143 2 ▶ if (!defined($self->{key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"key"}) 0 2 2
144 0 die "Invalid request key must be specified: see $docs\n";
145 }
146 2 $url = $url . "?key=" . $self->{key};
147  
148 2 ▶ if (defined($self->{include})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"include"}) 2 0 2
149 2 $url = $url . '&include=' . $self->{include};
150 }
151 2 ▶ if (defined($self->{unitGroup})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"unitGroup"}) 0 2 2
152 0 $url = $url . '&unitGroup=' . $self->{unitGroup};
153 }
154 2 ▶ if (defined($self->{lang})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"lang"}) 0 2 2
155 0 $url = $url . '&lang=' . $self->{lang};
156 }
157 2 ▶ if (defined($self->{options})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}) 0 2 2
158 0 $url = $url . '&options=' . $self->{options};
159 }
160 2 ▶ if (defined($self->{nonulls})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"nonulls"}) 0 2 2
161 0 $url = $url . '&nonulls=' . $self->{nonulls};
162 }
163 2 ▶ if (defined($self->{noheaders})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"noheaders"}) 0 2 2
164 0 $url = $url . '&noheaders=' . $self->{noheaders};
165 }
166 2 ▶ if (defined($self->{contentType})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"contentType"}) 0 2 2
167 0 $url = $url . '&contentType=' . $self->{contentType};
168 }
169 2 ▶ if (defined($self->{timezone})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"timezone"}) 0 2 2
170 0 $url = $url . '&timezone=' . $self->{timezone};
171 }
172 2 ▶ if (defined($self->{maxDistance})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"maxDistance"}) 0 2 2
173 0 $url = $url . '&maxDistance=' . $self->{maxDistance};
174 }
175 2 ▶ if (defined($self->{maxStations})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"maxStations"}) 0 2 2
176 0 $url = $url . '&maxStations=' . $self->{maxStations};
177 }
178 2 ▶ if (defined($self->{elevationDifference})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"elevationDifference"}) 0 2 2
179 0 $url = $url . '&elevationDifference=' . $self->{elevationDifference};
180 }
181 2 ▶ if (defined($self->{locationNames})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"locationNames"}) 0 2 2
182 0 $url = $url . '&locationNames=' . $self->{locationNames};
183 }
184 2 ▶ if (defined($self->{forecastBasisDate})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"forecastBasisDate"}) 0 2 2
185 0 $url = $url . '&forecastBasisDate=' . $self->{forecastBasisDate};
186 }
187 2 ▶ if (defined($self->{forecastBasisDay})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"forecastBasisDay"}) 0 2 2
188 0 $url = $url . '&forecastBasisDay=' . $self->{forecastBasisDay};
189 }
190 2 ▶ if (defined($self->{degreeDayTempBase})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"degreeDayTempBase"}) 0 2 2
191 0 $url = $url . '&degreeDayTempBase=' . $self->{degreeDayTempBase};
192 }
193 2 ▶ if (defined($self->{degreeDayTempMaxThreshold})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"degreeDayTempMaxThreshold"}) 0 2 2
194 0 $url = $url . '&degreeDayTempMaxThreshold=' . $self->{degreeDayTempMaxThreshold};
195 }
196  
197 2 ▶ $DEBUG && print "DEBUG: ddURL=" . $url . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and print 'DEBUG: ddURL=' . $url . "\n" 0 2 2
198 2 return $url;
199 }
200  
201 0 ▶sub TO_JSON {return { %{shift()} };}
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
202  
203 4 1;
204  
205  
206 =pod
207  
208 =encoding utf-8
209  
210 =head1 NAME
211  
212 VisualCrossing::API - Provides Perl API to VisualCrossing
213  
214 =head1 SYNOPSIS
215  
216 use VisualCrossing::API;
217 use JSON::XS;
218 use feature 'say';
219  
220 my $location = "AU419";
221 my $date = "2023-05-25"; # example time (optional)
222 my $key = "ABCDEFGABCDEFGABCDEFGABCD"; # example VisualCrossing API key
223  
224 ## Current Data (limit to current, saves on API cost)
225 my $weatherApi = VisualCrossing::API->new(
226 key => $key,
227 location => $location,
228 include => "current",
229 );
230 my $current = $weatherApi->getWeather;
231  
232 say "current temperature: " . $current->{currentConditions}->{temp};
233 say "current conditions: " . $current->{currentConditions}->{conditions};
234  
235 ## Historical Data (limit to single day, saves on API cost)
236 my $weatherApi = VisualCrossing::API->new(
237 key => $key,
238 location => $location,
239 date => $date
240 date2 => $date
241 include => "days",
242 );
243 my $history = $weatherApi->getWeather;
244  
245 say "$date temperature: " . $history->{days}[0]->{temp};
246 say "$date conditions: " . $history->{days}[0]->{conditions};
247  
248 =head1 DESCRIPTION
249  
250 This module is a wrapper around the VisualCrossing API.
251  
252 =head1 REFERENCES
253  
254 Git repository: L<https://github.com/duanemay/VisualCrossing-API>
255  
256 VisualCrossing API docs: L<https://www.visualcrossing.com/resources/documentation/weather-api/timeline-weather-api/>
257  
258 Based on DarkSky-API: L<https://github.com/mlbright/DarkSky-API>
259  
260 =head1 COPYRIGHT
261  
262 Copyright (c) 2023 L<Duane May>
263  
264 =head1 LICENSE
265  
266 This library is free software and may be distributed under the APACHE LICENSE, VERSION 2.0 L<https://www.apache.org/licenses/LICENSE-2.0>.
267  
268 =cut
↑ Summary