EFA.pm

filter: Statementstmt 41.1% 146 / 355 Branchbran 27.2% 36 / 132 Conditioncond 30.9% 26 / 84 MC/DCmcdc 6.3% 3 / 47 Subroutinesub 61.3% 27 / 44 Podpod 59.0% 13 / 22 totaltotal 36.7% 251 / 684 CC 112 SCAR 53.5
CC
112
Cov
36%
new
632.3
post_with_cache
90.0
post_with_cache_p
72.0
CRAP 211.2
↑ Summary EFA/Departure.pm »
1 package Travel::Status::DE::EFA;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '3.22';
9  
10 2 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Travel::Status::DE::EFA::Departure;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Travel::Status::DE::EFA::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Travel::Status::DE::EFA::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Travel::Status::DE::EFA::Services;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Travel::Status::DE::EFA::Stop;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Travel::Status::DE::EFA::Trip;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub new_p {
Subroutine new_p: not called CC=2 SCAR=17.9
Pod documented
26 0 my ( $class, %opt ) = @_;
27 0 my $promise = $opt{promise}->new;
28  
29 0 my $self;
30  
31 0 eval { $self = $class->new( %opt, async => 1 ); };
32 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
33 0 return $promise->reject($@);
34 }
35  
36 0 $self->{promise} = $opt{promise};
37  
38 $self->post_with_cache_p->then(
39 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
40 0 my ($content) = @_;
41 0 $self->{response} = $self->{json}->decode($content);
42  
43 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
44 0 say $self->{json}->pretty->encode( $self->{response} );
45 }
46  
47 0 $self->check_for_ambiguous;
48 0 $self->check_for_error;
49  
50 0 ▶ if ( $self->{errstr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"errstr"}) 0 0 0
51 0 $promise->reject( $self->{errstr}, $self );
52 0 return;
53 }
54  
55 0 $promise->resolve($self);
56 0 return;
57 }
58 )->catch(
59 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 my ( $err, $self ) = @_;
61 0 $promise->reject($err);
62 0 return;
63 }
64 0 )->wait;
65  
66 0 return $promise;
67 }
68  
69 2 ▶sub new {
Subroutine new: called CC=48 SCAR=64.5
Pod documented
70 2 my ( $class, %opt ) = @_;
71  
72 2 my $service;
73 2 my $encoding = 'UTF-8';
74 2 my $tls_insecure = 0;
75 2 ▶ $opt{timeout} //= 10;
Condition50%1/2
$opt{"timeout"} //= 10
l!l
02
MC/DC0%0/1
$opt{"timeout"} //= 10
$opt{"timeout"}
Truth table50%1/2
A
A= $opt{"timeout"}
Aresult
11
00
76 2 ▶ if ( $opt{timeout} <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"timeout"} <= 0) 0 2 2
77 0 delete $opt{timeout};
78 }
79  
80 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"coord"} or $opt{"name"} or $opt{"stopfinder"} or $opt{"stopseq"} or $opt{"from_json"}) 0 2 2
Condition33%4/12
$opt{"coord"} or $opt{"name"} or $opt{"stopfinder"} or $opt{"stopseq"} or $opt{"from_json"}
l!l&&r!l&&!r
020
$opt{"coord"} or $opt{"name"} or $opt{"stopfinder"} or $opt{"stopseq"}
l!l&&r!l&&!r
002
$opt{"coord"} or $opt{"name"} or $opt{"stopfinder"}
l!l&&r!l&&!r
002
$opt{"coord"} or $opt{"name"}
l!l&&r!l&&!r
002
MC/DC0%0/5
$opt{"coord"} or $opt{"name"} or $opt{"stopfinder"} or $opt{"stopseq"} or $opt{"from_json"}
$opt{"coord"} $opt{"name"} $opt{"stopfinder"} $opt{"stopseq"} $opt{"from_json"}
Truth table16%1/6
A or B or C or D or E
A= $opt{"coord"}B= $opt{"name"}C= $opt{"stopfinder"}D= $opt{"stopseq"}E= $opt{"from_json"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
81 not( $opt{coord}
82 or $opt{name}
83 or $opt{stopfinder}
84 or $opt{stopseq}
85 or $opt{from_json} )
86 )
87 {
88 0 confess('You must specify a name');
89 }
90 2 ▶ if ( $opt{type}
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"type"} and not $opt{"type"} =~ / ^ (?: stop | stopID | address | poi ) $ /x) 0 2 2
Condition33%1/3
$opt{"type"} and not $opt{"type"} =~ / ^ (?: stop | stopID | address | poi ) $ /x
!ll&&!rl&&r
200
MC/DC0%0/2
$opt{"type"} and not $opt{"type"} =~ / ^ (?: stop | stopID | address | poi ) $ /x
$opt{"type"} not $opt{"type"} =~ / ^ (?: stop | stopID | address | poi ) $ /x
Truth table33%1/3
A and not(B)
A= $opt{"type"}B= not $opt{"type"} =~ / ^ (?: stop | stopID | address | poi ) $ /x
ABresult
0X0
100
111
91 and not( $opt{type} =~ m{ ^ (?: stop | stopID | address | poi ) $ }x ) )
92 {
93 0 confess('type must be stop, stopID, address, or poi');
94 }
95  
96 2 ▶ if ( $opt{service} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"service"}) 0 2 2
97 0 ▶ if ( $service
Branch: 1 branch
BranchTrueFalseTotal
if ($service = Travel::Status::DE::EFA::Services::get_service($opt{"service"})) 0 0 0
98 = Travel::Status::DE::EFA::Services::get_service( $opt{service} ) )
99 {
100 0 $opt{efa_url} = $service->{url};
101 0 ▶ if ( $opt{coord} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt{'coord'}) { } 0 0 0
elsif ($opt{'stopfinder'}) { } 0 0 0
elsif ($opt{'stopseq'}) { } 0 0 0
102 0 $opt{efa_url} .= '/XML_COORD_REQUEST';
103 }
104 elsif ( $opt{stopfinder} ) {
105 0 $opt{efa_url} .= '/XML_STOPFINDER_REQUEST';
106 }
107 elsif ( $opt{stopseq} ) {
108 0 $opt{efa_url} .= '/XML_STOPSEQCOORD_REQUEST';
109 }
110 else {
111 0 $opt{efa_url} .= '/XML_DM_REQUEST';
112 }
113 0 ▶ $opt{time_zone} //= $service->{time_zone};
Condition0%0/3
$opt{"time_zone"} //= $service->{"time_zone"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"time_zone"} //= $service->{"time_zone"}
$opt{"time_zone"} $service->{"time_zone"}
Truth table0%0/3
A //= B
A= $opt{"time_zone"}B= $service->{"time_zone"}
ABresult
1X1
011
000
114 0 ▶ if ( not $service->{tls_verify} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service->{"tls_verify"}) 0 0 0
115 0 $tls_insecure = 1;
116 }
117 0 ▶ if ( $service->{encoding} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->{"encoding"}) 0 0 0
118 0 $encoding = $service->{encoding};
119 }
120 }
121 }
122  
123 2 ▶ $opt{time_zone} //= 'Europe/Berlin';
Condition50%1/2
$opt{"time_zone"} //= "Europe/Berlin"
l!l
02
MC/DC0%0/1
$opt{"time_zone"} //= "Europe/Berlin"
$opt{"time_zone"}
Truth table50%1/2
A
A= $opt{"time_zone"}
Aresult
11
00
124  
125 2 ▶ if ( not $opt{efa_url} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"efa_url"}) 0 2 2
126 0 confess('service or efa_url must be specified');
127 }
128 2 ▶ my $dt = $opt{datetime} // DateTime->now( time_zone => $opt{time_zone} );
Condition33%1/3
$opt{"datetime"} // "DateTime"->now("time_zone", $opt{"time_zone"})
l!l&&r!l&&!r
020
MC/DC0%0/2
$opt{"datetime"} // "DateTime"->now("time_zone", $opt{"time_zone"})
$opt{"datetime"} "DateTime"->now("time_zone", $opt{"time_zone"})
Truth table33%1/3
A // B
A= $opt{"datetime"}B= "DateTime"->now("time_zone", $opt{"time_zone"})
ABresult
1X1
011
000
129  
130 ## no critic (RegularExpressions::ProhibitUnusedCapture)
131 ## no critic (Variables::ProhibitPunctuationVars)
132  
133 2 ▶ if ( $opt{time}
Branch: 2 branches
BranchTrueFalseTotal
if ($opt{'time'} and $opt{'time'} =~ / ^ (?<hour> \d\d? ) : (?<minute> \d\d ) $ /x) { } 0 2 2
elsif ($opt{'time'}) { } 0 2 2
Condition33%1/3
$opt{"time"} and $opt{"time"} =~ / ^ (?<hour> \d\d? ) : (?<minute> \d\d ) $ /x
!ll&&!rl&&r
200
MC/DC0%0/2
$opt{"time"} and $opt{"time"} =~ / ^ (?<hour> \d\d? ) : (?<minute> \d\d ) $ /x
$opt{"time"} $opt{"time"} =~ / ^ (?<hour> \d\d? ) : (?<minute> \d\d ) $ /x
Truth table33%1/3
A and B
A= $opt{"time"}B= $opt{"time"} =~ / ^ (?<hour> \d\d? ) : (?<minute> \d\d ) $ /x
ABresult
0X0
100
111
134 and $opt{time} =~ m{ ^ (?<hour> \d\d? ) : (?<minute> \d\d ) $ }x )
135 {
136 $dt->set(
137 hour => $+{hour},
138 minute => $+{minute}
139 0 );
140 }
141 elsif ( $opt{time} ) {
142 0 confess('Invalid time specified');
143 }
144  
145 2 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($opt{'date'} and $opt{'date'} =~ / ^ (?<day> \d\d? ) [.] (?<month> \d\d? ) [.] (?<year> \d{4} )? $ /x) { } 0 2 2
elsif ($opt{'date'}) { } 0 2 2
Condition33%1/3
$opt{"date"} and $opt{"date"} =~ / ^ (?<day> \d\d? ) [.] (?<month> \d\d? ) [.] (?<year> \d{4} )? $ /x
!ll&&!rl&&r
200
MC/DC0%0/2
$opt{"date"} and $opt{"date"} =~ / ^ (?<day> \d\d? ) [.] (?<month> \d\d? ) [.] (?<year> \d{4} )? $ /x
$opt{"date"} $opt{"date"} =~ / ^ (?<day> \d\d? ) [.] (?<month> \d\d? ) [.] (?<year> \d{4} )? $ /x
Truth table33%1/3
A and B
A= $opt{"date"}B= $opt{"date"} =~ / ^ (?<day> \d\d? ) [.] (?<month> \d\d? ) [.] (?<year> \d{4} )? $ /x
ABresult
0X0
100
111
146 $opt{date}
147 and $opt{date} =~ m{ ^ (?<day> \d\d? ) [.] (?<month> \d\d? ) [.]
148 (?<year> \d{4} )? $ }x
149 )
150 {
151 0 ▶ if ( $+{year} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'year'}) { } 0 0 0
152 $dt->set(
153 day => $+{day},
154 month => $+{month},
155 year => $+{year}
156 0 );
157 }
158 else {
159 $dt->set(
160 day => $+{day},
161 month => $+{month}
162 0 );
163 }
164 }
165 elsif ( $opt{date} ) {
166 0 confess('Invalid date specified');
167 }
168  
169 my $self = {
170 cache => $opt{cache},
171 response => $opt{from_json},
172 developer_mode => $opt{developer_mode},
173 efa_url => $opt{efa_url},
174 service => $opt{service},
175 tls_insecure => $tls_insecure,
176 strp_stopseq => DateTime::Format::Strptime->new(
177 pattern => '%Y%m%d %H:%M',
178 time_zone => $opt{time_zone},
179 ),
180 strp_stopseq_s => DateTime::Format::Strptime->new(
181 pattern => '%Y%m%d %H:%M:%S',
182 time_zone => $opt{time_zone},
183 2 ),
184  
185 json => JSON->new->utf8,
186 };
187  
188 2 ▶ if ( $opt{coord} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt{'coord'}) { } 0 2 2
elsif ($opt{'stopfinder'}) { } 0 2 2
elsif ($opt{'stopseq'}) { } 0 2 2
189  
190 # outputFormat => 'JSON' returns invalid JSON
191 $self->{post} = {
192 coord => sprintf( '%.7f:%.7f:%s',
193 0 $opt{coord}{lon}, $opt{coord}{lat}, 'WGS84[DD.ddddd]' ),
194 radius_1 => 1320,
195 type_1 => 'STOP',
196 coordListOutputFormat => 'list',
197 max => 30,
198 inclFilter => 1,
199 outputFormat => 'rapidJson',
200 };
201 }
202 elsif ( $opt{stopfinder} ) {
203  
204 # filter: 2 (stop) | 4 (street) | 8 (address) | 16 (crossing) | 32 (poi) | 64 (postcod)
205 $self->{post} = {
206 locationServerActive => 1,
207 type_sf => 'any',
208 name_sf => $opt{stopfinder}{name},
209 0 anyObjFilter_sf => 2,
210 coordOutputFormat => 'WGS84[DD.DDDDD]',
211 outputFormat => 'JSON',
212 };
213 }
214 elsif ( $opt{stopseq} ) {
215  
216 # outputFormat => 'JSON' also works; leads to different output
217 $self->{post} = {
218 line => $opt{stopseq}{stateless},
219 stop => $opt{stopseq}{stop_id},
220 tripCode => $opt{stopseq}{key},
221 date => $opt{stopseq}{date},
222 time => $opt{stopseq}{time},
223 0 coordOutputFormat => 'WGS84[DD.DDDDD]',
224 outputFormat => 'rapidJson',
225 useRealtime => '1',
226 };
227 }
228 else {
229 $self->{post} = {
230 language => 'de',
231 mode => 'direct',
232 outputFormat => 'JSON',
233 type_dm => $opt{type} // 'stop',
234 useProxFootSearch => $opt{proximity_search} ? '1' : '0',
235 useRealtime => '1',
236 itdDateDay => $dt->day,
237 itdDateMonth => $dt->month,
238 itdDateYear => $dt->year,
239 itdTimeHour => $dt->hour,
240 itdTimeMinute => $dt->minute,
241 limit => $opt{num_results} // 40,
242 name_dm =>
243 2 ▶ uri_escape( encode( $encoding, $opt{name} ), '^A-Za-z0-9-._~ ' ),
Branch: 1 branch
BranchTrueFalseTotal
$opt{'proximity_search'} ? : 0 2 2
Condition50%2/4
$opt{"type"} // "stop"
l!l
02
$opt{"num_results"} // 40
l!l
02
MC/DC0%0/1
$opt{"type"} // "stop"
$opt{"type"}
MC/DC0%0/1
$opt{"num_results"} // 40
$opt{"num_results"}
Truth table50%1/2
A
A= $opt{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $opt{"num_results"}
Aresult
11
00
244 };
245 }
246  
247 2 ▶ if ( $opt{place} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"place"}) 0 2 2
248 0 ▶ if ( $service and $service->{map_place_to_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service and $service->{'map_place_to_name'}) { } 0 0 0
Condition0%0/3
$service and $service->{"map_place_to_name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$service and $service->{"map_place_to_name"}
$service $service->{"map_place_to_name"}
Truth table0%0/3
A and B
A= $serviceB= $service->{"map_place_to_name"}
ABresult
0X0
100
111
249 $self->{post}{name_dm}
250 = uri_escape( encode( $encoding, $opt{place} ),
251 '^A-Za-z0-9-._~ ' )
252 . q{ }
253 0 . $self->{post}{name_dm};
254 }
255 else {
256 0 $self->{post}{placeInfo_dm} = 'invalid';
257 0 $self->{post}{placeState_dm} = 'empty';
258 $self->{post}{place_dm}
259 0 = uri_escape( encode( $encoding, $opt{place} ),
260 '^A-Za-z0-9-._~ ' );
261 }
262 }
263  
264 2 ▶ if ( $opt{full_routes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"full_routes"}) 0 2 2
265 0 $self->{post}->{depType} = 'stopEvents';
266 0 $self->{post}->{includeCompleteStopSeq} = 1;
267 0 $self->{want_full_routes} = 1;
268 }
269  
270 2 bless( $self, $class );
271  
272 2 ▶ if ( $opt{user_agent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'user_agent'}) { } 0 2 2
273 0 $self->{ua} = $opt{user_agent};
274 }
275 else {
276 2 ▶ my %lwp_options = %{ $opt{lwp_options} // { timeout => 10 } };
Condition50%1/2
$opt{"lwp_options"} // {"timeout", 10}
l!l
02
MC/DC0%0/1
$opt{"lwp_options"} // {"timeout", 10}
$opt{"lwp_options"}
Truth table50%1/2
A
A= $opt{"lwp_options"}
Aresult
11
00
277 2 ▶ if ($tls_insecure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tls_insecure) 0 2 2
278 $lwp_options{ssl_opts}{SSL_verify_mode}
279 0 = IO::Socket::SSL::SSL_VERIFY_NONE;
280 0 $lwp_options{ssl_opts}{verify_hostname} = 0;
281 }
282  
283 2 $self->{ua} = LWP::UserAgent->new(%lwp_options);
284 2 $self->{ua}->env_proxy;
285 }
286  
287 2 ▶ if ( $self->{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 0 2 2
288 $self->{cache_key}
289 = $self->{efa_url} . '?'
290 . join( '&',
291 0 map { $_ . '=' . $self->{post}{$_} } sort keys %{ $self->{post} } );
292 }
293  
294 2 ▶ if ( $opt{async} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"async"}) 0 2 2
295 0 return $self;
296 }
297  
298 2 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 2 2
299 0 say 'POST ' . $self->{efa_url};
300 0 ▶ while ( my ( $key, $value ) = each %{ $self->{post} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"post"};}) 0 0 0
301 0 printf( "%30s = %s\n", $key, $value );
302 }
303 }
304  
305 2 ▶ if ( not $self->{response} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"response"}) 0 2 2
306 0 my ( $response, $error ) = $self->post_with_cache;
307  
308 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
309 0 $self->{errstr} = $error;
310 0 return $self;
311 }
312  
313 0 $self->{response} = $self->{json}->decode($response);
314 }
315  
316 2 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 2 2
317 0 say $self->{json}->pretty->encode( $self->{response} );
318 }
319  
320 2 $self->check_for_ambiguous;
321 2 $self->check_for_error;
322  
323 2 return $self;
324 }
325  
326 0 ▶sub post_with_cache {
Subroutine post_with_cache: not called CC=9 SCAR=45.0
Pod undocumented
327 0 my ($self) = @_;
328 0 my $cache = $self->{cache};
329 0 my $url = $self->{efa_url};
330  
331 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
332 0 ▶ say 'POST ' . ( $self->{cache_key} // $url );
Condition0%0/3
$self->{"cache_key"} // $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"cache_key"} // $url
$self->{"cache_key"} $url
Truth table0%0/3
A // B
A= $self->{"cache_key"}B= $url
ABresult
1X1
011
000
333 }
334  
335 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
336 0 my $content = $cache->thaw( $self->{cache_key} );
337 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
338 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
339 0 say ' cache hit';
340 }
341 0 return ( ${$content}, undef );
342 }
343 }
344  
345 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
346 0 say ' cache miss';
347 }
348  
349 0 my $reply = $self->{ua}->post( $url, $self->{post} );
350  
351 0 ▶ if ( $reply->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->is_error) 0 0 0
352 0 return ( undef, $reply->status_line );
353 }
354 0 my $content = $reply->content;
355  
356 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
357 0 $cache->freeze( $self->{cache_key}, \$content );
358 }
359  
360 0 return ( $content, undef );
361 }
362  
363 0 ▶sub post_with_cache_p {
Subroutine post_with_cache_p: not called CC=8 SCAR=42.8
Pod undocumented
364 0 my ($self) = @_;
365 0 my $cache = $self->{cache};
366 0 my $url = $self->{efa_url};
367  
368 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
369 0 ▶ say 'POST ' . ( $self->{cache_key} // $url );
Condition0%0/3
$self->{"cache_key"} // $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"cache_key"} // $url
$self->{"cache_key"} $url
Truth table0%0/3
A // B
A= $self->{"cache_key"}B= $url
ABresult
1X1
011
000
370 }
371  
372 0 my $promise = $self->{promise}->new;
373  
374 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
375 0 my $content = $cache->thaw( $self->{cache_key} );
376 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
377 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
378 0 say ' cache hit';
379 }
380 0 return $promise->resolve( ${$content} );
381 }
382 }
383  
384 0 ▶ if ( $self->{developer_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"developer_mode"}) 0 0 0
385 0 say ' cache miss';
386 }
387  
388 0 ▶ if ( $self->{tls_insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tls_insecure"}) 0 0 0
389 0 $self->{ua}->insecure(1);
390 }
391  
392 $self->{ua}->post_p( $url, form => $self->{post} )->then(
393 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
394 0 my ($tx) = @_;
395 0 ▶ if ( my $err = $tx->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $tx->error) 0 0 0
396 0 $promise->reject(
397 "POST $url returned HTTP $err->{code} $err->{message}");
398 0 return;
399 }
400 0 my $content = $tx->res->body;
401 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
402 0 $cache->freeze( $self->{cache_key}, \$content );
403 }
404 0 $promise->resolve($content);
405 0 return;
406 }
407 )->catch(
408 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
409 0 my ($err) = @_;
410 0 $promise->reject($err);
411 0 return;
412 }
413 0 )->wait;
414  
415 0 return $promise;
416 }
417  
418 2 ▶sub errstr {
Subroutine errstr: called CC=1 SCAR=0.0
Pod documented
419 2 my ($self) = @_;
420  
421 2 return $self->{errstr};
422 }
423  
424 1 ▶sub name_candidates {
Subroutine name_candidates: called CC=2 SCAR=7.4
Pod documented
425 1 my ($self) = @_;
426  
427 1 ▶ if ( $self->{name_candidates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"name_candidates"}) 1 0 1
428 1 return @{ $self->{name_candidates} };
429 }
430 0 return;
431 }
432  
433 1 ▶sub place_candidates {
Subroutine place_candidates: called CC=2 SCAR=8.4
Pod documented
434 1 my ($self) = @_;
435  
436 1 ▶ if ( $self->{place_candidates} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"place_candidates"}) 0 1 1
437 0 return @{ $self->{place_candidates} };
438 }
439 1 return;
440 }
441  
442 2 ▶sub check_for_error {
Subroutine check_for_error: called CC=4 SCAR=13.9
Pod undocumented
443 2 my ($self) = @_;
444  
445 2 my $json = $self->{response};
446  
447 2 my %kv;
448 2 ▶ for my $m ( @{ $json->{dm}{message} // [] } ) {
Condition100%2/2
$json->{"dm"}{"message"} // []
l!l
11
MC/DC100%1/1
$json->{"dm"}{"message"} // []
$json->{"dm"}{"message"}
Truth table100%2/2
A
A= $json->{"dm"}{"message"}
Aresult
11
00
449 4 $kv{ $m->{name} } = $m->{value};
450 }
451  
452 2 ▶ if ( $kv{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv{"error"}) 1 1 2
453 1 $self->{errstr} = "Backend error: $kv{error}";
454 }
455  
456 2 return;
457 }
458  
459 3 ▶sub check_for_ambiguous {
Subroutine check_for_ambiguous: called CC=12 SCAR=30.4
Pod undocumented
460 3 my ($self) = @_;
461  
462 3 my $json = $self->{response};
463  
464 3 ▶ if ( $json->{departureList} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"departureList"}) 1 2 3
465 1 return;
466 }
467  
468 2 ▶ for my $m ( @{ $json->{dm}{message} // [] } ) {
Condition50%1/2
$json->{"dm"}{"message"} // []
l!l
20
MC/DC0%0/1
$json->{"dm"}{"message"} // []
$json->{"dm"}{"message"}
Truth table50%1/2
A
A= $json->{"dm"}{"message"}
Aresult
11
00
469 4 ▶ if ( $m->{name} eq 'error' and $m->{value} eq 'name list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"name"} eq "error" and $m->{"value"} eq "name list") 2 2 4
Condition66%2/3
$m->{"name"} eq "error" and $m->{"value"} eq "name list"
!ll&&!rl&&r
202
MC/DC50%1/2
$m->{"name"} eq "error" and $m->{"value"} eq "name list"
$m->{"name"} eq "error" $m->{"value"} eq "name list"
Truth table66%2/3
A and B
A= $m->{"name"} eq "error"B= $m->{"value"} eq "name list"
ABresult
0X0
100
111
470 2 $self->{errstr} = "ambiguous name parameter";
471 2 $self->{name_candidates} = [];
472 2 ▶ for my $point ( @{ $json->{dm}{points} // [] } ) {
Condition50%1/2
$json->{"dm"}{"points"} // []
l!l
20
MC/DC0%0/1
$json->{"dm"}{"points"} // []
$json->{"dm"}{"points"}
Truth table50%1/2
A
A= $json->{"dm"}{"points"}
Aresult
11
00
473 6 my $place = $point->{ref}{place};
474 push(
475 6 @{ $self->{name_candidates} },
476 Travel::Status::DE::EFA::Stop->new(
477 place => $place,
478 full_name => $point->{name},
479 name => $point->{name} =~ s{\Q$place\E,? ?}{}r,
480 id_num => $point->{ref}{id},
481 )
482 6 );
483 }
484 2 return;
485 }
486 2 ▶ if ( $m->{name} eq 'error' and $m->{value} eq 'place list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"name"} eq "error" and $m->{"value"} eq "place list") 0 2 2
Condition33%1/3
$m->{"name"} eq "error" and $m->{"value"} eq "place list"
!ll&&!rl&&r
200
MC/DC0%0/2
$m->{"name"} eq "error" and $m->{"value"} eq "place list"
$m->{"name"} eq "error" $m->{"value"} eq "place list"
Truth table33%1/3
A and B
A= $m->{"name"} eq "error"B= $m->{"value"} eq "place list"
ABresult
0X0
100
111
487 0 $self->{errstr} = "ambiguous name parameter";
488 0 $self->{place_candidates} = [];
489 0 ▶ for my $point ( @{ $json->{dm}{points} // [] } ) {
Condition0%0/2
$json->{"dm"}{"points"} // []
l!l
00
MC/DC0%0/1
$json->{"dm"}{"points"} // []
$json->{"dm"}{"points"}
Truth table0%0/2
A
A= $json->{"dm"}{"points"}
Aresult
11
00
490 0 my $place = $point->{ref}{place};
491 push(
492 0 @{ $self->{place_candidates} },
493 Travel::Status::DE::EFA::Stop->new(
494 place => $place,
495 full_name => $point->{name},
496 name => $point->{name} =~ s{\Q$place\E,? ?}{}r,
497 id_num => $point->{ref}{id},
498 )
499 0 );
500 }
501 0 return;
502 }
503 }
504  
505 0 return;
506 }
507  
508 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
509 0 my ($self) = @_;
510 0 ▶ if ( $self->{stop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stop"}) 0 0 0
511 0 return $self->{stop};
512 }
513  
514 0 my $point = $self->{response}{dm}{points}{point};
515 0 my $place = $point->{ref}{place};
516  
517 $self->{stop} = Travel::Status::DE::EFA::Stop->new(
518 place => $place,
519 full_name => $point->{name},
520 name => $point->{name} =~ s{\Q$place\E,? ?}{}r,
521 id_num => $point->{ref}{id},
522 id_code => $point->{ref}{gid},
523 0 );
524  
525 0 return $self->{stop};
526 }
527  
528 0 ▶sub stops {
Subroutine stops: not called CC=5 SCAR=34.0
Pod documented
529 0 my ($self) = @_;
530  
531 0 ▶ if ( $self->{stops} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stops"}) 0 0 0
532 0 return @{ $self->{stops} };
533 }
534  
535 0 ▶ my $stops = $self->{response}{dm}{itdOdvAssignedStops} // [];
Condition0%0/2
$self->{"response"}{"dm"}{"itdOdvAssignedStops"} // []
l!l
00
MC/DC0%0/1
$self->{"response"}{"dm"}{"itdOdvAssignedStops"} // []
$self->{"response"}{"dm"}{"itdOdvAssignedStops"}
Truth table0%0/2
A
A= $self->{"response"}{"dm"}{"itdOdvAssignedStops"}
Aresult
11
00
536  
537 0 ▶ if ( ref($stops) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stops eq "HASH") 0 0 0
538 0 $stops = [$stops];
539 }
540  
541 0 my @stops;
542 0 for my $stop ( @{$stops} ) {
543 push(
544 @stops,
545 Travel::Status::DE::EFA::Stop->new(
546 place => $stop->{place},
547 name => $stop->{name},
548 full_name => $stop->{nameWithPlace},
549 id_num => $stop->{stopID},
550 id_code => $stop->{gid},
551 )
552 0 );
553 }
554  
555 0 $self->{stops} = \@stops;
556 0 return @stops;
557 }
558  
559 0 ▶sub infos {
Subroutine infos: not called CC=5 SCAR=34.0
Pod undocumented
560 0 my ($self) = @_;
561  
562 0 ▶ if ( $self->{infos} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"infos"}) 0 0 0
563 0 return @{ $self->{infos} };
564 }
565  
566 0 ▶ for my $info ( @{ $self->{response}{dm}{points}{point}{infos} // [] } ) {
Condition0%0/2
$self->{"response"}{"dm"}{"points"}{"point"}{"infos"} // []
l!l
00
MC/DC0%0/1
$self->{"response"}{"dm"}{"points"}{"point"}{"infos"} // []
$self->{"response"}{"dm"}{"points"}{"point"}{"infos"}
Truth table0%0/2
A
A= $self->{"response"}{"dm"}{"points"}{"point"}{"infos"}
Aresult
11
00
567 push(
568 0 @{ $self->{infos} },
569 Travel::Status::DE::EFA::Info->new( json => $info )
570 );
571 }
572  
573 0 ▶ return @{ $self->{infos} // [] };
Condition0%0/2
$self->{"infos"} // []
l!l
00
MC/DC0%0/1
$self->{"infos"} // []
$self->{"infos"}
Truth table0%0/2
A
A= $self->{"infos"}
Aresult
11
00
574 }
575  
576 1 ▶sub lines {
Subroutine lines: called CC=5 SCAR=19.6
Pod documented
577 1 my ($self) = @_;
578  
579 1 ▶ if ( $self->{lines} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lines"}) 0 1 1
580 0 return @{ $self->{lines} };
581 }
582  
583 1 ▶ for my $line ( @{ $self->{response}{servingLines}{lines} // [] } ) {
Condition50%1/2
$self->{"response"}{"servingLines"}{"lines"} // []
l!l
01
MC/DC0%0/1
$self->{"response"}{"servingLines"}{"lines"} // []
$self->{"response"}{"servingLines"}{"lines"}
Truth table50%1/2
A
A= $self->{"response"}{"servingLines"}{"lines"}
Aresult
11
00
584 0 push( @{ $self->{lines} }, $self->parse_line($line) );
585 }
586  
587 1 ▶ return @{ $self->{lines} // [] };
Condition50%1/2
$self->{"lines"} // []
l!l
01
MC/DC0%0/1
$self->{"lines"} // []
$self->{"lines"}
Truth table50%1/2
A
A= $self->{"lines"}
Aresult
11
00
588 }
589  
590 0 ▶sub parse_line {
Subroutine parse_line: not called CC=2 SCAR=17.9
Pod undocumented
591 0 my ( $self, $line ) = @_;
592  
593 0 ▶ my $mode = $line->{mode} // {};
Condition0%0/2
$line->{"mode"} // {}
l!l
00
MC/DC0%0/1
$line->{"mode"} // {}
$line->{"mode"}
Truth table0%0/2
A
A= $line->{"mode"}
Aresult
11
00
594  
595 return Travel::Status::DE::EFA::Line->new(
596 type => $mode->{product},
597 name => $mode->{name},
598 number => $mode->{number},
599 direction => $mode->{destination},
600 valid => $mode->{timetablePeriod},
601 mot => $mode->{product},
602 operator => $mode->{diva}{operator},
603 identifier => $mode->{diva}{globalId},
604  
605 0 );
606 }
607  
608 2 ▶sub results {
Subroutine results: called CC=4 SCAR=17.9
Pod documented
609 2 my ($self) = @_;
610  
611 2 ▶ if ( $self->{results} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"results"}) 0 2 2
612 0 return @{ $self->{results} };
613 }
614  
615 2 ▶ if ( $self->{post}{coord} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'post'}{'coord'}) { } 0 2 2
elsif ($self->{'post'}{'name_sf'}) { } 0 2 2
616 0 return $self->results_coord;
617 }
618 elsif ( $self->{post}{name_sf} ) {
619 0 return $self->results_stopfinder;
620 }
621 else {
622 2 return $self->results_dm;
623 }
624 }
625  
626 0 ▶sub results_coord {
Subroutine results_coord: not called CC=3 SCAR=24.8
Pod undocumented
627 0 my ($self) = @_;
628 0 my $json = $self->{response};
629  
630 0 my @results;
631 0 ▶ for my $stop ( @{ $json->{locations} // [] } ) {
Condition0%0/2
$json->{"locations"} // []
l!l
00
MC/DC0%0/1
$json->{"locations"} // []
$json->{"locations"}
Truth table0%0/2
A
A= $json->{"locations"}
Aresult
11
00
632 push(
633 @results,
634 Travel::Status::DE::EFA::Stop->new(
635 place => $stop->{parent}{name},
636 full_name => $stop->{properties}{STOP_NAME_WITH_PLACE},
637 distance_m => $stop->{properties}{distance},
638 name => $stop->{name},
639 id_code => $stop->{id},
640 )
641 0 );
642 }
643  
644 0 $self->{results} = \@results;
645  
646 0 return @results;
647 }
648  
649 0 ▶sub results_stopfinder {
Subroutine results_stopfinder: not called CC=5 SCAR=34.0
Pod undocumented
650 0 my ($self) = @_;
651 0 my $json = $self->{response};
652  
653 0 my @results;
654  
655 # Edge case: there is just a single result.
656 # Oh EFA, you so silly.
657 0 ▶ if ( ref( $json->{stopFinder}{points} ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json->{"stopFinder"}{"points"} eq "HASH" and exists $json->{"stopFinder"}{"points"}{"point"}) 0 0 0
Condition0%0/3
ref $json->{"stopFinder"}{"points"} eq "HASH" and exists $json->{"stopFinder"}{"points"}{"point"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $json->{"stopFinder"}{"points"} eq "HASH" and exists $json->{"stopFinder"}{"points"}{"point"}
ref $json->{"stopFinder"}{"points"} eq "HASH" exists $json->{"stopFinder"}{"points"}{"point"}
Truth table0%0/3
A and B
A= ref $json->{"stopFinder"}{"points"} eq "HASH"B= exists $json->{"stopFinder"}{"points"}{"point"}
ABresult
0X0
100
111
658 and exists $json->{stopFinder}{points}{point} )
659 {
660 0 $json->{stopFinder}{points} = [ $json->{stopFinder}{points}{point} ];
661 }
662  
663 0 ▶ for my $stop ( @{ $json->{stopFinder}{points} // [] } ) {
Condition0%0/2
$json->{"stopFinder"}{"points"} // []
l!l
00
MC/DC0%0/1
$json->{"stopFinder"}{"points"} // []
$json->{"stopFinder"}{"points"}
Truth table0%0/2
A
A= $json->{"stopFinder"}{"points"}
Aresult
11
00
664 push(
665 @results,
666 Travel::Status::DE::EFA::Stop->new(
667 place => $stop->{ref}{place},
668 full_name => $stop->{name},
669 name => $stop->{object},
670 id_num => $stop->{ref}{id},
671 id_code => $stop->{ref}{gid},
672 )
673 0 );
674 }
675  
676 0 $self->{results} = \@results;
677  
678 0 return @results;
679 }
680  
681 2 ▶sub results_dm {
Subroutine results_dm: called CC=5 SCAR=16.2
Pod undocumented
682 2 my ($self) = @_;
683 2 my $json = $self->{response};
684  
685 # Oh EFA, you so silly
686 2 ▶ if ( $json->{departureList} and ref( $json->{departureList} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"departureList"} and ref $json->{"departureList"} eq "HASH") 0 2 2
Condition66%2/3
$json->{"departureList"} and ref $json->{"departureList"} eq "HASH"
!ll&&!rl&&r
110
MC/DC0%0/2
$json->{"departureList"} and ref $json->{"departureList"} eq "HASH"
$json->{"departureList"} ref $json->{"departureList"} eq "HASH"
Truth table66%2/3
A and B
A= $json->{"departureList"}B= ref $json->{"departureList"} eq "HASH"
ABresult
0X0
100
111
687 0 $json->{departureList} = [ $json->{departureList}{departure} ];
688 }
689  
690 2 my @results;
691 2 ▶ for my $departure ( @{ $json->{departureList} // [] } ) {
Condition100%2/2
$json->{"departureList"} // []
l!l
11
MC/DC100%1/1
$json->{"departureList"} // []
$json->{"departureList"}
Truth table100%2/2
A
A= $json->{"departureList"}
Aresult
11
00
692 push(
693 @results,
694 Travel::Status::DE::EFA::Departure->new(
695 json => $departure,
696 strp_stopseq => $self->{strp_stopseq},
697 strp_stopseq_s => $self->{strp_stopseq_s}
698 )
699 40 );
700 }
701  
702 40 @results = map { $_->[0] }
703 75 sort { $a->[1] <=> $b->[1] }
704 2 map { [ $_, $_->countdown ] } @results;
705  
706 2 $self->{results} = \@results;
707  
708 2 return @results;
709 }
710  
711 0 ▶sub result {
Subroutine result: not called CC=1 SCAR=6.9
Pod documented
712 0 my ($self) = @_;
713  
714 0 return Travel::Status::DE::EFA::Trip->new( json => $self->{response} );
715 }
716  
717 # static
718 0 ▶sub get_service_ids {
Subroutine get_service_ids: not called CC=1 SCAR=6.9
Pod documented
719 0 return Travel::Status::DE::EFA::Services::get_service_ids(@_);
720 }
721  
722 0 ▶sub get_services {
Subroutine get_services: not called CC=2 SCAR=17.9
Pod documented
723 0 my @services;
724 0 for my $service ( Travel::Status::DE::EFA::Services::get_service_ids() ) {
725 my %desc
726 0 = %{ Travel::Status::DE::EFA::Services::get_service($service) };
727 0 $desc{shortname} = $service;
728 0 push( @services, \%desc );
729 }
730 0 return @services;
731 }
732  
733 # static
734 0 ▶sub get_service {
Subroutine get_service: not called CC=1 SCAR=6.9
Pod documented
735 0 return Travel::Status::DE::EFA::Services::get_service(@_);
736 }
737  
738 2 1;
739  
740 __END__
↑ Summary EFA/Departure.pm »