blib/lib/Weather/Astro7Timer.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 20 / 20 Conditioncond 100% 16 / 16 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 100% 121 / 121 SCAR 28.9
CC
18
Cov
100%
get_response
8.0
get
5.0
new
4.0
CRAP 18.0
↑ Summary
1 package Weather::Astro7Timer;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Weather::Astro7Timer - Simple client for the 7Timer.info Weather Forecast service
12  
13 =head1 VERSION
14  
15 Version 0.01
16  
17 =cut
18  
19 3 our $VERSION = '0.1';
20  
21 =head1 SYNOPSIS
22  
23 my $w7t = Weather::Astro7Timer->new();
24  
25 # Get ASTRO weather for the Stonehenge area
26  
27 my %report = $w7t->get(
28 product => 'astro', # Forecast type (astro, civil, civillight, meteo, two)
29 lat => 51.2, # Latitude
30 lon => -1.8, # Longitude
31 );
32  
33 # Dumping the result would give you:
34 %report = {
35 'product' => 'astro',
36 'init' => '2023032606',
37 'dataseries' => [{
38 'temp2m' => 6,
39 'wind10m' => {
40 'speed' => 2,
41 'direction' => 'NE'
42 },
43 'rh2m' => 13,
44 'seeing' => 3,
45 'timepoint' => 3,
46 'lifted_index' => 2,
47 'prec_type' => 'none',
48 'cloudcover' => 9,
49 'transparency' => 6
50 },
51 {...},
52 ...
53 ]
54 };
55  
56 =head1 DESCRIPTION
57  
58 Weather::Astro7Timer provides basic access to the L<7Timer.info|https://7Timer.info>
59 Weather Forecast API.
60 7Timer is a service based on NOAA's GFS and provides various types of forecast products.
61 It is mostly known for its ASTRO product intended for astronomers and stargazers, as
62 it provides an astronomical seeing and transparency forecast.
63  
64 Pease see the L<official API documentation|https://www.7timer.info/doc.php> and
65 L<GitHub Wiki|https://github.com/Yeqzids/7timer-issues/wiki/Wiki> for details.
66  
67 The module was made to serve the apps L<Xasteria|https://astro.ecuadors.net/xasteria/> and
68 L<Polar Scope Align|https://astro.ecuadors.net/polar-scope-align/>, but if your service
69 requires some extra functionality, feel free to contact the author about it.
70  
71 =head1 CONSTRUCTOR
72  
73 =head2 C<new>
74  
75 my $w7t = Weather::Astro7Timer->new(
76 scheme => $http_scheme?,
77 timeout => $timeout_sec?,
78 agent => $user_agent_string?,
79 ua => $lwp_ua?,
80 );
81
82 Optional parameters:
83  
84 =over 4
85  
86 =item * C<scheme> : You can specify C<http>. Default: C<https>.
87  
88 =item * C<timeout> : Timeout for requests in secs. Default: C<30>.
89  
90 =item * C<agent> : Customize the user agent string.
91  
92 =item * C<ua> : Pass your own L<LWP::UserAgent> to customise further.
93  
94 =back
95  
96 =head1 METHODS
97  
98 =head2 C<get>
99  
100 my $report = $w7t->get(
101 product => $product, # Forecast type (astro, civil, civillight, meteo, two)
102 lat => $lat, # Latitude
103 lon => $lon, # Longitude
104 output => $format?, # Output format (default json)
105 unit => $unit?, # Units (default metric)
106 lang => $language?, # Language (default en)
107 tzshift => $tz_shift?, # Timezone shift from UTC (hours, default 0)
108 ac => $alt_cor?, # Altitude correction (default 0)
109 );
110  
111 my %report = $w7t->get( ... );
112  
113  
114 Fetches a forecast report for the requested for the requested location.
115 Returns a string containing the JSON or XML data, except in array context, in which case,
116 as a convenience, it will use L<JSON> or L<XML::Simple> to decode it directly to a Perl hash.
117 For an explanation to the returned data, refer to the L<official API documentation|http://www.7timer.info/doc.php>.
118  
119 If the request is not successful, it will C<die> throwing the C<< HTTP::Response->status_line >>.
120  
121 Required parameters:
122  
123 =over 4
124
125 =item * C<lat> : Latitude (-90 to 90). South is negative.
126  
127 =item * C<lon> : Longitude (-180 to 180). West is negative.
128  
129 =item * C<product> : Choose from the available forecast products:
130  
131 =over 4
132  
133 =item * C<astro> : ASTRO 3-day forecast for astronomy/stargazing with 3h step (includes astronomical seeing, transparency).
134  
135 =item * C<civil> : CIVIL 8-day forecast that provides a weather type enum (see docs for equivalent icons) with 3h step.
136  
137 =item * C<civillight> : CIVIL Light simplified per-day forecast for next week.
138  
139 =item * C<meteo> : A detailed meteorogical forecast including relative humidity and wind profile from 950hPa to 200hPa.
140  
141 =item * C<two> : A two week overview forecast.
142  
143 =back
144  
145 =back
146  
147 Optional parameters (see the API documentation for further details):
148  
149 =over 4
150  
151 =item * C<lang> : Supports C<zh-CN> or C<zh-TW>, otherwise default is C<en>.
152  
153 =item * C<unit> : C<metric> (default) or C<british> units.
154  
155 =item * C<output> : Output format, supports C<json> (default) or C<xml>, although
156 C<internal> will also work, returning png image.
157  
158 =item * C<tzshift> : Timezone offset in hours ( -23 to 23).
159  
160 =item * C<ac> : Altitude correction (e.g. temp) for high peaks. Default C<0>, accepts
161 C<2> or C<7> (in km). Only for C<astro> product.
162  
163 =back
164  
165 =head2 C<get_response>
166  
167 my $response = $w7t->get_response(
168 lat => $lat,
169 lon => $lon,
170 product => $product
171 %args?
172 );
173  
174 Same as C<get> except it returns the full L<HTTP::Response> from the API (so you
175 can handle bad requests yourself).
176  
177  
178 =head1 CONVENIENCE FUNCTIONS
179  
180 =head2 C<products>
181  
182 my @products = Weather::Astro7Timer::products();
183  
184 Returns the supported forecast products.
185  
186 =cut
187  
188 sub new {
189 8 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
190  
191 8 my $self = {};
192 8 bless($self, $class);
193  
194 8 my %args = @_;
195  
196 8 $self->{ua} = $args{ua};
197 8 ▶ $self->{scheme} = $args{scheme} || 'https';
Condition100%2/2
$args{'scheme'} || 'https'
l!l
26
MC/DC100%1/1
$args{'scheme'} || 'https'
$args{'scheme'}
Truth table100%2/2
A
A= $args{'scheme'}
Aresult
11
00
198 8 ▶ $self->{timeout} = $args{timeout} || 30;
Condition100%2/2
$args{'timeout'} || 30
l!l
17
MC/DC100%1/1
$args{'timeout'} || 30
$args{'timeout'}
Truth table100%2/2
A
A= $args{'timeout'}
Aresult
11
00
199 8 ▶ $self->{agent} = $args{agent} || "libwww-perl Weather::Astro7Timer/$VERSION";
Condition100%2/2
$args{'agent'} || "libwww-perl Weather::Astro7Timer/$Weather::Astro7Timer::VERSION"
l!l
17
MC/DC100%1/1
$args{'agent'} || "libwww-perl Weather::Astro7Timer/$Weather::Astro7Timer::VERSION"
$args{'agent'}
Truth table100%2/2
A
A= $args{'agent'}
Aresult
11
00
200  
201 8 return $self;
202 }
203  
204 sub get {
205 13 ▶ my $self = shift;
Subroutine get: called CC=5 SCAR=16.1
Pod documented
206 13 my %args = @_;
207 13 ▶ $args{lang} ||= 'en';
Condition100%2/2
$args{"lang"} ||= "en"
l!l
67
MC/DC100%1/1
$args{"lang"} ||= "en"
$args{"lang"}
Truth table100%2/2
A
A= $args{"lang"}
Aresult
11
00
208 13 ▶ $args{output} ||= 'json';
Condition100%2/2
$args{"output"} ||= "json"
l!l
211
MC/DC100%1/1
$args{"output"} ||= "json"
$args{"output"}
Truth table100%2/2
A
A= $args{"output"}
Aresult
11
00
209  
210 13 my $resp = $self->get_response(%args);
211  
212 7 ▶ if ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 6 1 7
213 6 ▶ return _output($resp->decoded_content, wantarray ? $args{output} : '');
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
214 }
215 else {
216 1 die $resp->status_line;
217 }
218 }
219  
220 sub get_response {
221 13 ▶ my $self = shift;
Subroutine get_response: called CC=8 SCAR=20.8
Pod documented
222 13 my %args = @_;
223  
224 croak("product was not defined")
225 13 ▶ unless $args{product};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"product"} 1 12 13
226  
227 croak("product not supported")
228 12 ▶ unless grep { /^$args{product}$/ } products();
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^$args{"product"}$/;} products() 1 11 12
229  
230 croak("lat between -90 and 90 expected")
231 11 ▶ unless defined $args{lat} && abs($args{lat}) <= 90;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"lat"} and abs $args{"lat"} <= 90 2 9 11
Condition100%3/3
defined $args{"lat"} and abs $args{"lat"} <= 90
!ll&&!rl&&r
119
MC/DC100%2/2
defined $args{"lat"} and abs $args{"lat"} <= 90
defined $args{"lat"} abs $args{"lat"} <= 90
Truth table100%3/3
A and B
A= defined $args{"lat"}B= abs $args{"lat"} <= 90
ABresult
0X0
100
111
232  
233 croak("lon between -180 and 180 expected")
234 9 ▶ unless defined $args{lon} && abs($args{lon}) <= 180;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"lon"} and abs $args{"lon"} <= 180 2 7 9
Condition100%3/3
defined $args{"lon"} and abs $args{"lon"} <= 180
!ll&&!rl&&r
117
MC/DC100%2/2
defined $args{"lon"} and abs $args{"lon"} <= 180
defined $args{"lon"} abs $args{"lon"} <= 180
Truth table100%3/3
A and B
A= defined $args{"lon"}B= abs $args{"lon"} <= 180
ABresult
0X0
100
111
235  
236 7 my $url = $self->_weather_url(%args);
237  
238 7 ▶ unless ($self->{ua}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ua"}) 4 3 7
239 4 require LWP::UserAgent;
240 4 $self->{ua} = LWP::UserAgent->new();
241 }
242  
243 7 $self->{ua}->agent($self->{agent});
244 7 $self->{ua}->timeout($self->{timeout});
245  
246 7 return $self->{ua}->get($url);
247 }
248  
249 sub _weather_url {
250 9 ▶ my $self = shift;
Subroutine _weather_url: called CC=1 SCAR=0.0
251 9 my %args = @_;
252 9 my $prod = delete $args{product};
253 my $url =
254 $self->{scheme}
255 . "://www.7timer.info/bin/$prod.php?"
256 9 . join("&", map {"$_=$args{$_}"} keys %args);
257  
258 9 return $url;
259 }
260  
261 sub _output {
262 6 ▶ my $str = shift;
Subroutine _output: called CC=4 SCAR=13.9
263 6 my $format = shift;
264  
265 6 ▶ return $str unless $format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 3 3 6
266  
267 3 ▶ if ($format eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'json') { } 1 2 3
elsif ($format eq 'xml') { } 1 1 2
268 1 require JSON;
269 1 return %{JSON::decode_json($str)};
270 } elsif ($format eq 'xml') {
271 1 require XML::Simple;
272 1 return %{XML::Simple::XMLin($str)};
273 }
274 1 return (data => $str);
275 }
276  
277 sub products {
278 12 ▶ return qw/astro two civil civillight meteo/;
Subroutine products: called CC=1 SCAR=0.0
Pod documented
279 }
280  
281 =head1 AUTHOR
282  
283 Dimitrios Kechagias, C<< <dkechag at cpan.org> >>
284  
285 =head1 BUGS
286  
287 Please report any bugs or feature requests either on L<GitHub|https://github.com/dkechag/Weather-Astro7Timer> (preferred), or on RT (via the email
288 C<bug-weather-astro7timer at rt.cpan.org> or L<web interface|https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Weather-Astro7TImer>).
289  
290 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
291  
292 =head1 GIT
293  
294 L<https://github.com/dkechag/Weather-Astro7Timer>
295  
296 =head1 LICENSE AND COPYRIGHT
297  
298 This software is copyright (c) 2023 by Dimitrios Kechagias.
299  
300 This is free software; you can redistribute it and/or modify it under
301 the same terms as the Perl 5 programming language system itself.
302  
303 =cut
304  
305 3 1;
↑ Summary