DBRIS/Connection.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 203 CC 17 SCAR 48.7
CC
17
Cov
0%
new
240.0
TO_JSON
12.0
segments
2.0
CRAP 130.6
1 package Travel::Routing::DE::DBRIS::Connection;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6 use utf8;
7  
8 use parent 'Class::Accessor';
9  
10 use DateTime::Duration;
11 use Travel::Routing::DE::DBRIS::Connection::Segment;
12  
13 our $VERSION = '0.18';
14  
15 Travel::Routing::DE::DBRIS::Connection->mk_ro_accessors(
16 qw(changes feasibility is_cancelled is_unscheduled is_unlikely
17 duration sched_duration rt_duration
18 sched_dep rt_dep dep
19 sched_arr rt_arr arr
20 occupancy occupancy_first occupancy_second
21 price price_unit recon
22 )
23 );
24  
25 sub new {
26 my ( $obj, %opt ) = @_;
27  
28 my $json = $opt{json};
29 my $strpdate = $opt{strpdate_obj};
30 my $strptime = $opt{strptime_obj};
31  
32 my $ref = {
33 changes => $json->{umstiegsAnzahl},
34 feasibility => $json->{ereignisZusammenfassung}{anschlussBewertungCode}
35 // -1,
36 is_unscheduled => $json->{isAlternativeVerbindung},
37 id => $json->{tripId},
38 recon => $json->{ctxRecon},
39 price => $json->{angebotsPreis}{betrag},
40 price_unit => $json->{angebotsPreis}{waehrung},
41 strptime_obj => $strptime,
42 };
43  
44 if ( $ref->{price_unit} and $ref->{price_unit} eq 'EUR' ) {
45 $ref->{price_unit} = '€';
46 }
47  
48 if ( my $d = $json->{verbindungsDauerInSeconds} ) {
49 $ref->{sched_duration} = DateTime::Duration->new(
50 hours => int( $d / 3600 ),
51 minutes => int( ( $d % 3600 ) / 60 ),
52 seconds => $d % 60,
53 );
54 }
55 if ( my $d = $json->{ezVerbindungsDauerInSeconds} ) {
56 $ref->{rt_duration} = DateTime::Duration->new(
57 hours => int( $d / 3600 ),
58 minutes => int( ( $d % 3600 ) / 60 ),
59 seconds => $d % 60,
60 );
61 }
62 $ref->{duration} = $ref->{rt_duration} // $ref->{sched_duration};
63  
64 for my $occupancy ( @{ $json->{auslastungsmeldungen} // [] } ) {
65 if ( $occupancy->{klasse} eq 'KLASSE_1' ) {
66 $ref->{occupancy_first} = $occupancy->{stufe};
67 }
68 if ( $occupancy->{klasse} eq 'KLASSE_2' ) {
69 $ref->{occupancy_second} = $occupancy->{stufe};
70 }
71 }
72  
73 if ( $ref->{occupancy_first} and $ref->{occupancy_second} ) {
74 $ref->{occupancy}
75 = ( $ref->{occupancy_first} + $ref->{occupancy_second} ) / 2;
76 }
77 elsif ( $ref->{occupancy_first} ) {
78 $ref->{occupancy} = $ref->{occupancy_first};
79 }
80 elsif ( $ref->{occupancy_second} ) {
81 $ref->{occupancy} = $ref->{occupancy_second};
82 }
83  
84 for my $segment ( @{ $json->{verbindungsAbschnitte} // [] } ) {
85 push(
86 @{ $ref->{segments} },
87 Travel::Routing::DE::DBRIS::Connection::Segment->new(
88 json => $segment,
89 strptime_obj => $strptime
90 )
91 );
92 }
93  
94 for my $i ( 0 .. $#{ $ref->{segments} // [] } - 1 ) {
95 if ( $ref->{segments}[$i]{train_short} ) {
96 my $arr = $ref->{segments}[$i]->arr;
97 for my $j ( $i + 1 .. $#{ $ref->{segments} // [] } ) {
98 if ( $ref->{segments}[$j]{train_short} ) {
99 my $dep = $ref->{segments}[$j]->dep;
100 if ( $arr and $dep ) {
101 $ref->{segments}[$j]{transfer_duration} = $dep - $arr;
102 if ( $dep < $arr ) {
103 $ref->{segments}[$i]{is_unlikely} = 1;
104 }
105 }
106 last;
107 }
108 }
109 }
110 }
111  
112 for my $key (qw(sched_dep rt_dep dep)) {
113 $ref->{$key} = $ref->{segments}[0]{$key};
114 }
115 for my $key (qw(sched_arr rt_arr arr)) {
116 $ref->{$key} = $ref->{segments}[-1]{$key};
117 }
118  
119 for my $note ( @{ $json->{risNotizen} // [] } ) {
120 push( @{ $ref->{notes} }, $note );
121 if ( $note->{key} eq 'text.realtime.connection.cancelled' ) {
122 $ref->{is_cancelled} = 1;
123 }
124 elsif ( $note->{key} eq 'text.realtime.connection.brokentrip' ) {
125 $ref->{is_unlikely} = 1;
126 }
127 }
128 for my $message ( @{ $json->{messages} // [] } ) {
129 push( @{ $ref->{messages} }, $message );
130 }
131  
132 bless( $ref, $obj );
133  
134 return $ref;
135 }
136  
137 sub segments {
138 my ($self) = @_;
139  
140 return @{ $self->{segments} // [] };
141 }
142  
143 sub notes {
144 my ($self) = @_;
145  
146 return @{ $self->{notes} // [] };
147 }
148  
149 sub messages {
150 my ($self) = @_;
151  
152 return @{ $self->{messages} // [] };
153 }
154  
155 sub TO_JSON {
156 my ($self) = @_;
157  
158 my $ret = { %{$self} };
159  
160 delete $ret->{strptime_obj};
161  
162 for my $k (qw(sched_dep rt_dep dep sched_arr rt_arr arr)) {
163 if ( $ret->{$k} ) {
164 $ret->{$k} = $ret->{$k}->epoch;
165 }
166 }
167  
168 for my $k (qw(sched_duration rt_duration duration)) {
169 if ( $ret->{$k} ) {
170 $ret->{$k} = $ret->{$k}->in_units('minutes');
171 }
172 }
173  
174 return $ret;
175 }
176  
177 1;