HAFAS/Connection.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 184 CC 12 SCAR 33.9
CC
12
Cov
0%
new
72.0
TO_JSON
12.0
messages
6.0
CRAP 29.5
1 package Travel::Routing::DE::HAFAS::Connection;
2  
3 # vim:foldmethod=marker
4  
5 use strict;
6 use warnings;
7 use 5.014;
8  
9 use parent 'Class::Accessor';
10 use DateTime::Duration;
11 use Travel::Routing::DE::HAFAS::Utils;
12 use Travel::Routing::DE::HAFAS::Connection::Section;
13  
14 our $VERSION = '0.11';
15  
16 Travel::Routing::DE::HAFAS::Connection->mk_ro_accessors(
17 qw(changes duration sched_dep rt_dep sched_arr rt_arr dep arr dep_platform arr_platform dep_loc arr_loc dep_cancelled arr_cancelled is_cancelled load)
18 );
19  
20 # {{{ Constructor
21  
22 sub new {
23 my ( $obj, %opt ) = @_;
24  
25 my $hafas = $opt{hafas};
26 my $connection = $opt{connection};
27 my $locL = $opt{locL};
28 my $prodL = $opt{prodL};
29  
30 # himL may only be present in departure monitor mode
31 my @remL = @{ $opt{common}{remL} // [] };
32 my @himL = @{ $opt{common}{himL} // [] };
33  
34 my @msgL = @{ $connection->{msgL} // [] };
35 my @secL = @{ $connection->{secL} // [] };
36  
37 my $date = $connection->{date};
38 my $duration = $connection->{dur};
39  
40 $duration = DateTime::Duration->new(
41 hours => substr( $duration, 0, 2 ),
42 minutes => substr( $duration, 2, 2 ),
43 seconds => substr( $duration, 4, 2 ),
44 );
45  
46 my @messages;
47 for my $msg (@msgL) {
48 if ( $msg->{type} eq 'REM' and defined $msg->{remX} ) {
49 push( @messages, $hafas->add_message( $remL[ $msg->{remX} ] ) );
50 }
51 elsif ( $msg->{type} eq 'HIM' and defined $msg->{himX} ) {
52 push( @messages, $hafas->add_message( $himL[ $msg->{himX} ], 1 ) );
53 }
54 else {
55 say "Unknown message type $msg->{type}";
56 }
57 }
58  
59 my $strptime = DateTime::Format::Strptime->new(
60 pattern => '%Y%m%dT%H%M%S',
61 time_zone => 'Europe/Berlin'
62 );
63  
64 # dProgType/aProgType: CORRECTED oder PROGNOSED
65 my $sched_dep = handle_day_change(
66 date => $date,
67 time => $connection->{dep}{dTimeS},
68 offset => $connection->{dep}{dTZOffset},
69 strp_obj => $strptime,
70 );
71 my $rt_dep = handle_day_change(
72 date => $date,
73 time => $connection->{dep}{dTimeR},
74 offset => $connection->{dep}{dTZOffset},
75 strp_obj => $strptime,
76 );
77 my $sched_arr = handle_day_change(
78 date => $date,
79 time => $connection->{arr}{aTimeS},
80 offset => $connection->{arr}{aTZOffset},
81 strp_obj => $strptime,
82 );
83 my $rt_arr = handle_day_change(
84 date => $date,
85 time => $connection->{arr}{aTimeR},
86 offset => $connection->{arr}{aTZOffset},
87 strp_obj => $strptime,
88 );
89  
90 my @sections;
91 for my $sec (@secL) {
92 if ( $sec->{type} eq 'WALK' and not $sec->{gis}{durS} ) {
93 next;
94 }
95 push(
96 @sections,
97 Travel::Routing::DE::HAFAS::Connection::Section->new(
98 common => $opt{common},
99 date => $date,
100 locL => $locL,
101 prodL => $prodL,
102 sec => $sec,
103 hafas => $hafas,
104 )
105 );
106 }
107  
108 my $prev;
109 for my $sec (@sections) {
110 if ( $sec->type eq 'JNY' ) {
111 if ($prev) {
112 $sec->set_transfer_from_previous_section($prev);
113 }
114 $prev = $sec;
115 }
116 }
117  
118 my $tco = {};
119 for my $tco_id ( @{ $connection->{dTrnCmpSX}{tcocX} // [] } ) {
120 my $tco_kv = $opt{common}{tcocL}[$tco_id];
121 $tco->{ $tco_kv->{c} } = $tco_kv->{r};
122 }
123  
124 my $dep_cancelled = $connection->{dep}{dCncl} ? 1 : 0;
125 my $arr_cancelled = $connection->{arr}{aCncl} ? 1 : 0;
126 my $is_cancelled = $dep_cancelled || $arr_cancelled;
127  
128 my $ref = {
129 duration => $duration,
130 changes => $connection->{chg},
131 sched_dep => $sched_dep,
132 rt_dep => $rt_dep,
133 sched_arr => $sched_arr,
134 rt_arr => $rt_arr,
135 dep_cancelled => $dep_cancelled,
136 arr_cancelled => $arr_cancelled,
137 is_cancelled => $is_cancelled,
138 dep => $rt_dep // $sched_dep,
139 arr => $rt_arr // $sched_arr,
140 dep_platform => $connection->{dep}{dPlatfR}
141 // $connection->{dep}{dPlatfS},
142 arr_platform => $connection->{arr}{aPlatfR}
143 // $connection->{arr}{aPlatfS},
144 dep_loc => $locL->[ $connection->{dep}{locX} ],
145 arr_loc => $locL->[ $connection->{arr}{locX} ],
146 load => $tco,
147 messages => \@messages,
148 sections => \@sections,
149 };
150  
151 bless( $ref, $obj );
152  
153 return $ref;
154 }
155  
156 # }}}
157  
158 # {{{ Accessors
159  
160 sub messages {
161 my ($self) = @_;
162  
163 if ( $self->{messages} ) {
164 return @{ $self->{messages} };
165 }
166 return;
167 }
168  
169 sub sections {
170 my ($self) = @_;
171  
172 if ( $self->{sections} ) {
173 return @{ $self->{sections} };
174 }
175 return;
176 }
177  
178 sub TO_JSON {
179 my ($self) = @_;
180  
181 my $ret = { %{$self} };
182  
183 for my $k ( keys %{$ret} ) {
184 if ( ref( $ret->{$k} ) eq 'DateTime' ) {
185 $ret->{$k} = $ret->{$k}->epoch;
186 }
187 if ( ref( $ret->{$k} ) eq 'DateTime::Duration' ) {
188 $ret->{$k} = [ $ret->{$k}->in_units( 'days', 'hours', 'minutes' ) ];
189 }
190 }
191  
192 return $ret;
193 }
194  
195 # }}}
196  
197 1;
198  
199 __END__