OpenSky.pm

filter: Statementstmt 93.7% 211 / 225 Branchbran 59.0% 78 / 132 Conditioncond 48.8% 21 / 43 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 96.5% 28 / 29 Podpod 100% 6 / 6 totaltotal 75.6% 347 / 459 CC 83 SCAR 21.8
CC
83
Cov
78%
_get_response
23.4
__ANON__
12.1
_url
11.9
CRAP 8.9
« OpenSky/Core/StateVector.pm ↑ Summary OpenSky/Response.pm »
1 package WebService::OpenSky;
2  
3 # ABSTRACT: Perl interface to the OpenSky Network API
4  
5 # see also https://github.com/openskynetwork/opensky-api
6 8 ▶use v5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use WebService::OpenSky::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use WebService::OpenSky::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
9 ArrayRef
10 Bool
11 Dict
12 HashRef
13 InstanceOf
14 Int
15 Latitude
16 Longitude
17 NonEmptyStr
18 Num
19 Optional
20 Undef
21 8 );
22 8 ▶use WebService::OpenSky::Response::States;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use WebService::OpenSky::Response::Flights;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use WebService::OpenSky::Response::FlightTrack;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use PerlX::Maybe;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Config::INI::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 8 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 ▶use Mojo::JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 ▶use Type::Params -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 8 our $VERSION = '0.5';
34  
35 7 ▶param config => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
36 is => 'ro',
37 isa => NonEmptyStr,
38 7 ▶ default => method() { $ENV{HOME} . '/.openskyrc' },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
39 );
40  
41 8 param [qw/debug raw testing/] => (
42 isa => Bool,
43 default => 0,
44 );
45  
46 9 ▶field _config_data => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 9 9
47 isa => HashRef,
48 9 ▶ default => method() {
Subroutine __ANON__: called CC=10 SCAR=24.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 9 9
49 9 ▶ my $file = $self->config // '';
Condition50%1/2
$self->config // ""
l!l
90
MC/DC0%0/1
$self->config // ""
$self->config
Truth table50%1/2
A
A= $self->config
Aresult
11
00
50 9 ▶ if ( !-e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 9 0 9
51 9 ▶ if ($self->testing # if we're testing
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testing or $self->_has_username and $self->_has_password or $ENV{"OPENSKY_USERNAME"} and $ENV{"OPENSKY_PASSWORD"}) 8 1 9
Condition66%8/12
$self->testing or $self->_has_username and $self->_has_password or $ENV{"OPENSKY_USERNAME"} and $ENV{"OPENSKY_PASSWORD"}
l!l&&r!l&&!r
801
$self->testing or $self->_has_username and $self->_has_password
l!l&&r!l&&!r
711
$self->_has_username and $self->_has_password
!ll&&!rl&&r
101
$ENV{"OPENSKY_USERNAME"} and $ENV{"OPENSKY_PASSWORD"}
!ll&&!rl&&r
100
MC/DC40%2/5
$self->testing or $self->_has_username and $self->_has_password or $ENV{"OPENSKY_USERNAME"} and $ENV{"OPENSKY_PASSWORD"}
$self->testing $self->_has_username $self->_has_password $ENV{"OPENSKY_USERNAME"} $ENV{"OPENSKY_PASSWORD"}
Truth table37%3/8
A or B and C or D and E
A= $self->testingB= $self->_has_usernameC= $self->_has_passwordD= $ENV{"OPENSKY_USERNAME"}E= $ENV{"OPENSKY_PASSWORD"}
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
52 || ( $self->_has_username && $self->_has_password ) # or we have a username and password
53 || ( $ENV{OPENSKY_USERNAME} && $ENV{OPENSKY_PASSWORD} )
54 ) # even if they're from %ENV
55 {
56 8 return {}; # then we don't need a config file
57 }
58 1 croak("Config file '$file' does not exist");
59 }
60 0 Config::INI::Reader->read_file( $self->config );
61 8 },
62 );
63  
64 7 ▶field _last_request_time => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
65 isa => HashRef [Int],
66 7 ▶ default => method() {
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
67 return {
68 7 '/states/all' => 0,
69 '/states/own' => 0,
70 };
71 8 }
72 );
73  
74 0 ▶field _ua => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 0 0
75 isa => InstanceOf ['Mojo::UserAgent'],
76 0 ▶ default => method() { Mojo::UserAgent->new },
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 0 0
77 );
78  
79 7 ▶param _username => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
80 isa => NonEmptyStr,
81 lazy => 1,
82 init_arg => 'username',
83 predicate => '_has_username',
84 7 ▶ default => method() { $ENV{OPENSKY_USERNAME} // $self->_config_data->{opensky}{username} },
Subroutine __ANON__: called CC=4 SCAR=14.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
Condition33%1/3
$ENV{"OPENSKY_USERNAME"} // $self->_config_data->{"opensky"}{"username"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$ENV{"OPENSKY_USERNAME"} // $self->_config_data->{"opensky"}{"username"}
$ENV{"OPENSKY_USERNAME"} $self->_config_data->{"opensky"}{"username"}
Truth table33%1/3
A // B
A= $ENV{"OPENSKY_USERNAME"}B= $self->_config_data->{"opensky"}{"username"}
ABresult
1X1
011
000
85 );
86  
87 7 ▶param _password => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
88 isa => NonEmptyStr,
89 lazy => 1,
90 init_arg => 'password',
91 predicate => '_has_password',
92 7 ▶ default => method() { $ENV{OPENSKY_PASSWORD} // $self->_config_data->{opensky}{password} },
Subroutine __ANON__: called CC=4 SCAR=14.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
Condition33%1/3
$ENV{"OPENSKY_PASSWORD"} // $self->_config_data->{"opensky"}{"password"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$ENV{"OPENSKY_PASSWORD"} // $self->_config_data->{"opensky"}{"password"}
$ENV{"OPENSKY_PASSWORD"} $self->_config_data->{"opensky"}{"password"}
Truth table33%1/3
A // B
A= $ENV{"OPENSKY_PASSWORD"}B= $self->_config_data->{"opensky"}{"password"}
ABresult
1X1
011
000
93 );
94  
95 7 ▶param _base_url => (
Branch: 1 branch
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
96 init_arg => 'base_url',
97 isa => NonEmptyStr,
98 lazy => 1,
99 7 ▶ default => method() {
Subroutine __ANON__: called CC=4 SCAR=14.5
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and Function::Parameters::_croak('Too many arguments for method (anon) (expected 1, got ' . @_ . ')') 0 7 7
100 7 ▶ $self->_config_data->{_}{base_url} // 'https://opensky-network.org/api';
Condition50%1/2
$self->_config_data->{"_"}{"base_url"} // "https://opensky-network.org/api"
l!l
07
MC/DC0%0/1
$self->_config_data->{"_"}{"base_url"} // "https://opensky-network.org/api"
$self->_config_data->{"_"}{"base_url"}
Truth table50%1/2
A
A= $self->_config_data->{"_"}{"base_url"}
Aresult
11
00
101 8 },
102 );
103  
104 8 field limit_remaining => (
105 isa => Int | Undef,
106 writer => '_set_limit_remaining',
107 default => undef,
108 );
109  
110 8 signature_for get_states => (
111 method => 1,
112 named => [
113 time => Optional [Num], { default => 0 },
114 icao24 => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ],
115 bbox => Optional [
116 Dict [
117 lamin => Latitude,
118 lomin => Longitude,
119 lamax => Latitude,
120 lomax => Longitude,
121 ],
122 ],
123 { default => {} },
124 extended => Optional [Bool],
125 ],
126 named_to_list => 1,
127 );
128  
129 4 ▶method get_states( $seconds, $icao24, $bbox, $extended ) {
Subroutine get_states: called CC=5 SCAR=16.5
Branch: 2 branches
BranchTrueFalseTotal
@_ < 5 and Function::Parameters::_croak('Too few arguments for method get_states (expected 5, got ' . @_ . ')') 0 4 4
@_ > 5 and Function::Parameters::_croak('Too many arguments for method get_states (expected 5, got ' . @_ . ')') 0 4 4
130 4 my %params = (
131 maybe time => $seconds,
132 maybe icao24 => $icao24,
133 maybe extended => $extended,
134 );
135 4 ▶ if ( keys $bbox->%* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$bbox) 4 0 4
136 4 $params{$_} = $bbox->{$_} for qw( lamin lomin lamax lomax );
137 }
138  
139 4 return $self->_get_response(
140 route => '/states/all',
141 params => \%params,
142 class => 'WebService::OpenSky::Response::States',
143  
144 # rate_limit_noauth => 10,
145 # rate_limit_auth => 5,
146 );
147 8 }
148  
149 8 signature_for get_my_states => (
150 method => 1,
151 named => [
152 time => Optional [Num], { default => 0 },
153 icao24 => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ],
154 serials => Optional [ NonEmptyStr | ArrayRef [NonEmptyStr] ],
155 ],
156 named_to_list => 1,
157 );
158  
159 method get_my_states( $seconds, $icao24, $serials ) {
160 my %params = (
161 extended => 1,
162 maybe time => $seconds,
163 maybe icao24 => $icao24,
164 maybe serials => $serials,
165 );
166 return $self->_get_response(
167 route => '/states/own',
168 params => \%params,
169 class => 'WebService::OpenSky::Response::States',
170  
171 # rate_limit_noauth => undef,
172 # rate_limit_auth => 1,
173 );
174 8 }
175  
176 4 ▶method get_flights_from_interval( $begin, $end ) {
Subroutine get_flights_from_interval: called CC=5 SCAR=16.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method get_flights_from_interval (expected 3, got ' . @_ . ')') 0 4 4
@_ > 3 and Function::Parameters::_croak('Too many arguments for method get_flights_from_interval (expected 3, got ' . @_ . ')') 0 4 4
177 4 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 1 3 4
178 1 croak 'The end time must be greater than or equal to the start time.';
179 }
180 3 ▶ if ( ( $end - $begin ) > 7200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 7200) 1 2 3
181 1 croak 'The time interval must be smaller than two hours.';
182 }
183  
184 2 my %params = ( begin => $begin, end => $end );
185 2 return $self->_get_response(
186 route => '/flights/all',
187 params => \%params,
188 class => 'WebService::OpenSky::Response::Flights',
189 );
190 8 }
191  
192 4 ▶method get_flights_by_aircraft( $icao24, $begin, $end ) {
Subroutine get_flights_by_aircraft: called CC=5 SCAR=16.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for method get_flights_by_aircraft (expected 4, got ' . @_ . ')') 0 4 4
@_ > 4 and Function::Parameters::_croak('Too many arguments for method get_flights_by_aircraft (expected 4, got ' . @_ . ')') 0 4 4
193 4 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 1 3 4
194 1 croak 'The end time must be greater than or equal to the start time.';
195 }
196 3 ▶ if ( ( $end - $begin ) > 2592 * 1e3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 2592000) 1 2 3
197 1 croak 'The time interval must be smaller than 30 days.';
198 }
199  
200 2 my %params = ( icao24 => $icao24, begin => $begin, end => $end );
201 2 return $self->_get_response(
202 route => '/flights/aircraft',
203 params => \%params,
204 class => 'WebService::OpenSky::Response::Flights',
205 );
206 8 }
207  
208 4 ▶method get_arrivals_by_airport( $airport, $begin, $end ) {
Subroutine get_arrivals_by_airport: called CC=5 SCAR=16.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for method get_arrivals_by_airport (expected 4, got ' . @_ . ')') 0 4 4
@_ > 4 and Function::Parameters::_croak('Too many arguments for method get_arrivals_by_airport (expected 4, got ' . @_ . ')') 0 4 4
209 4 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 1 3 4
210 1 croak 'The end time must be greater than or equal to the start time.';
211 }
212 3 ▶ if ( ( $end - $begin ) > 604800 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 604800) 1 2 3
213 1 croak 'The time interval must be smaller than 7 days.';
214 }
215  
216 2 my %params = ( airport => $airport, begin => $begin, end => $end );
217 2 return $self->_get_response(
218 route => '/flights/arrival',
219 params => \%params,
220 class => 'WebService::OpenSky::Response::Flights',
221 );
222 8 }
223  
224 4 ▶method get_departures_by_airport( $airport, $begin, $end ) {
Subroutine get_departures_by_airport: called CC=5 SCAR=16.2
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 4 and Function::Parameters::_croak('Too few arguments for method get_departures_by_airport (expected 4, got ' . @_ . ')') 0 4 4
@_ > 4 and Function::Parameters::_croak('Too many arguments for method get_departures_by_airport (expected 4, got ' . @_ . ')') 0 4 4
225 4 ▶ if ( $begin >= $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin >= $end) 1 3 4
226 1 croak 'The end time must be greater than or equal to the start time.';
227 }
228 3 ▶ if ( ( $end - $begin ) > 604800 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $begin > 604800) 1 2 3
229 1 croak 'The time interval must be smaller than 7 days.';
230 }
231  
232 2 my %params = ( airport => $airport, begin => $begin, end => $end );
233 2 return $self->_get_response(
234 route => '/flights/departure',
235 params => \%params,
236 class => 'WebService::OpenSky::Response::Flights',
237 );
238 8 }
239  
240 2 ▶method get_track_by_aircraft( $icao24, $time ) {
Subroutine get_track_by_aircraft: called CC=5 SCAR=17.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method get_track_by_aircraft (expected 3, got ' . @_ . ')') 0 2 2
@_ > 3 and Function::Parameters::_croak('Too many arguments for method get_track_by_aircraft (expected 3, got ' . @_ . ')') 0 2 2
241 2 ▶ if ( $time != 0 && ( time - $time ) > 2592 * 1e3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time != 0 and time - $time > 2592000) 0 2 2
Condition33%1/3
$time != 0 and time - $time > 2592000
!ll&&!rl&&r
020
MC/DC0%0/2
$time != 0 and time - $time > 2592000
$time != 0 time - $time > 2592000
Truth table33%1/3
A and B
A= $time != 0B= time - $time > 2592000
ABresult
0X0
100
111
242 0 croak 'It is not possible to access flight tracks from more than 30 days in the past.';
243 }
244  
245 2 my %params = ( icao24 => $icao24, time => $time );
246 2 return $self->_get_response(
247 route => '/tracks/all',
248 params => \%params,
249 class => 'WebService::OpenSky::Response::FlightTrack',
250 );
251 8 }
252  
253 8 signature_for _get_response => (
254 method => 1,
255 named => [
256 route => NonEmptyStr,
257 params => Optional [HashRef],
258 credits => Optional [Bool],
259 class => Optional [NonEmptyStr],
260 no_auth_required => Optional [Bool],
261 ],
262 named_to_list => 1,
263 );
264  
265 14 ▶method _get_response( $route, $params, $credits, $response_class, $no_auth_required ) {
Subroutine _get_response: called CC=17 SCAR=31.5
Branch: 2 branches
BranchTrueFalseTotal
@_ < 6 and Function::Parameters::_croak('Too few arguments for method _get_response (expected 6, got ' . @_ . ')') 0 14 14
@_ > 6 and Function::Parameters::_croak('Too many arguments for method _get_response (expected 6, got ' . @_ . ')') 0 14 14
266 14 my $url = $self->_url( $route, $params, $no_auth_required );
267  
268 14 ▶ if ( !$self->testing ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->testing) 2 12 14
269  
270 # XXX Ugh. I'd like to use attributes to attach metadata to the
271 # methods, but with the attribute order switch, I can't. So I'm
272 # going to leave this ugly hack here.
273 2 ▶ my $method
Branch: 2 branches
BranchTrueFalseTotal
$route eq '/states/all' ? : 2 0 2
$route eq '/states/own' ? : 0 0 0
274 = $route eq '/states/all' ? 'get_states'
275 : $route eq '/states/own' ? 'get_my_states'
276 : undef;
277 2 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) 2 0 2
278 2 ▶ if ( my $delay_remaining = $self->delay_remaining($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delay_remaining = $self->delay_remaining($method)) 1 1 2
279 1 carp("You have to wait $delay_remaining seconds before you can call $method again.");
280 1 return;
281 }
282 }
283  
284 1 my $limit_remaining = $self->limit_remaining;
285  
286 # if it's not defined, we haven't made an API call yet
287 1 ▶ if ( defined $limit_remaining && !$limit_remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $limit_remaining and not $limit_remaining) 0 1 1
Condition33%1/3
defined $limit_remaining and not $limit_remaining
!ll&&!rl&&r
100
MC/DC0%0/2
defined $limit_remaining and not $limit_remaining
defined $limit_remaining not $limit_remaining
Truth table33%1/3
A and not(B)
A= defined $limit_remainingB= not $limit_remaining
ABresult
0X0
100
111
288 0 carp("You have no API credits left for $route. See https://openskynetwork.github.io/opensky-api/rest.html#limitations");
289 0 return;
290 }
291 }
292  
293 13 my $response = $self->_GET($url);
294 13 my $remaining = $response->headers->header('X-Rate-Limit-Remaining');
295  
296 13 $self->_debug( $response->headers->to_string . "\n" );
297  
298 # not all requests cost credits, so we only want to set the limit if
299 # $remaining is defined
300 13 ▶ $self->_set_limit_remaining($remaining) if !$credits && defined $remaining;
Branch: 1 branch
BranchTrueFalseTotal
if not $credits and defined $remaining 2 11 13
Condition66%2/3
not $credits and defined $remaining
!ll&&!rl&&r
0112
MC/DC50%1/2
not $credits and defined $remaining
not $credits defined $remaining
Truth table66%2/3
not(A) and B
A= not $creditsB= defined $remaining
ABresult
0X0
100
111
301  
302 # this is annoying. If the didn't match any criteria, the service should return a 200
303 # and an empty response. Instead, we get a 404.
304 13 ▶ if ( !$response->is_success && $response->code != 404 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success and $response->code != 404) 0 13 13
Condition66%2/3
not $response->is_success and $response->code != 404
!ll&&!rl&&r
760
MC/DC0%0/2
not $response->is_success and $response->code != 404
not $response->is_success $response->code != 404
Truth table66%2/3
not(A) and B
A= not $response->is_successB= $response->code != 404
ABresult
0X0
100
111
305 0 croak $response->to_string;
306 }
307 13 $self->_last_request_time->{$route} = time;
308  
309 13 ▶ return $remaining if $credits;
Branch: 1 branch
BranchTrueFalseTotal
if $credits 0 13 13
310 13 my $response_body = $response->body;
311 13 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 13 13
312 0 $self->_debug($response_body);
313 }
314 13 ▶ my $raw_response = $response_body ? decode_json($response_body) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response_body ? : 7 6 13
315 13 return $response_class->new(
316 route => $route,
317 query => $params,
318 maybe raw_response => $raw_response,
319 );
320 8 }
321  
322 14 ▶method delay_remaining($method) {
Subroutine delay_remaining: called CC=9 SCAR=22.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method delay_remaining (expected 2, got ' . @_ . ')') 0 14 14
@_ > 2 and Function::Parameters::_croak('Too many arguments for method delay_remaining (expected 2, got ' . @_ . ')') 0 14 14
323 14 state $rate_limits = {
324 'get_states' => {
325 route => '/states/all',
326 noauth => 10,
327 auth => 5,
328 },
329 'get_my_states' => {
330 route => '/states/own',
331 noauth => undef,
332 auth => 1,
333 },
334 };
335 14 ▶ my $delay = $rate_limits->{$method} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $delay = $rate_limits->{$method} or return 0 6 8 14
336 6 my $limit = $self->limit_remaining;
337  
338 # XXX this is a bit of a hack. If we've not made any requests yet, we don't
339 # know what the limit is, so we assume they haven't made a request yet.
340 # Probably need to revisit this. I would love an API endpoint that lets me
341 # fetch the limit
342 6 ▶ return 0 if !defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit 3 3 6
343  
344 3 ▶ return 0 if $limit <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $limit <= 0 0 3 3
345 3 my $seconds_since_last_request = time - $self->_last_request_time->{ $delay->{route} };
346  
347 3 my $delay_remaining;
348 3 ▶ if ( !$self->_password && $delay->{noauth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_password and $delay->{'noauth'}) { } 0 3 3
Condition33%1/3
not $self->_password and $delay->{"noauth"}
!ll&&!rl&&r
300
MC/DC0%0/2
not $self->_password and $delay->{"noauth"}
not $self->_password $delay->{"noauth"}
Truth table33%1/3
not(A) and B
A= not $self->_passwordB= $delay->{"noauth"}
ABresult
0X0
100
111
349 0 $delay = $delay->{noauth} - $seconds_since_last_request;
350 }
351 else {
352 3 $delay_remaining = $delay->{auth} - $seconds_since_last_request;
353 }
354 3 ▶ return $delay_remaining > 0 ? $delay_remaining : 0;
Branch: 1 branch
BranchTrueFalseTotal
$delay_remaining > 0 ? : 2 1 3
355 8 }
356  
357 # an easy target to override for testing
358 method _GET($url) {
359 $self->_debug("GET $url\n");
360 return $self->_ua->get($url)->res;
361 8 }
362  
363 13 ▶method _debug($msg) {
Subroutine _debug: called CC=4 SCAR=15.0
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method _debug (expected 2, got ' . @_ . ')') 0 13 13
@_ > 2 and Function::Parameters::_croak('Too many arguments for method _debug (expected 2, got ' . @_ . ')') 0 13 13
364 13 ▶ return if !$self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 13 0 13
365 0 say STDERR $msg;
366 8 }
367  
368 14 ▶method _url( $url, $params = {}, $no_auth_required = 0 ) {
Subroutine _url: called CC=8 SCAR=24.7
Branch: 4 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for method _url (expected 2, got ' . @_ . ')') 0 14 14
@_ > 4 and Function::Parameters::_croak('Too many arguments for method _url (expected 4, got ' . @_ . ')') 0 14 14
@_ < 3 and do { @_ < 2 and $params = {}; $no_auth_required = 0 } 0 14 14
@_ < 2 and $params = {} 0 0 0
369 14 my $username = $self->_username;
370 14 my $password = $self->_password;
371 14 ▶ if ( ( !$username || !$password ) && $no_auth_required ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $username && $password and $no_auth_required) 0 14 14
Condition33%2/6
not $username && $password and $no_auth_required
!ll&&!rl&&r
1400
$username && $password
!ll&&!rl&&r
0014
MC/DC0%0/3
not $username && $password and $no_auth_required
$username $password $no_auth_required
Truth table20%1/5
not(A && B) and C
A= $usernameB= $passwordC= $no_auth_required
ABCresult
11X0
0X00
1000
0X11
1011
372 0 return Mojo::URL->new( $self->_base_url . $url )->query($params);
373 }
374 14 $url = Mojo::URL->new( $self->_base_url . $url )->userinfo( $self->_username . ':' . $self->_password );
375 14 $url->query($params);
376 14 return $url;
377 }
378  
379 __END__
« OpenSky/Core/StateVector.pm ↑ Summary OpenSky/Response.pm »