DBWagenreihung.pm untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 292 CC 17 SCAR 30.5
CC
17
Cov
0%
get_with_cache
72.0
get_wagonorder
30.0
new
20.0
CRAP 21.1
1 package Travel::Status::DE::DBWagenreihung;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6 use utf8;
7  
8 use parent 'Class::Accessor';
9  
10 use Carp qw(cluck confess);
11 use JSON;
12 use List::Util qw(uniq);
13 use LWP::UserAgent;
14 use Travel::Status::DE::DBWagenreihung::Group;
15 use Travel::Status::DE::DBWagenreihung::Sector;
16 use Travel::Status::DE::DBWagenreihung::Carriage;
17  
18 our $VERSION = '0.18';
19  
20 Travel::Status::DE::DBWagenreihung->mk_ro_accessors(
21 qw(direction platform train_type));
22  
23 # {{{ Constructors
24  
25 sub new {
26 my ( $class, %opt ) = @_;
27  
28 if ( not $opt{train_number} and not $opt{from_json} ) {
29 confess('train_number option must be set');
30 }
31  
32 if ( not $opt{departure} and not $opt{from_json} ) {
33 confess('departure option must be set');
34 }
35  
36 my $self = {
37 api_base =>
38 'https://www.bahn.de/web/api/reisebegleitung/wagenreihung/vehicle-sequence',
39 developer_mode => $opt{developer_mode},
40 cache => $opt{cache},
41 departure => $opt{departure},
42 eva => $opt{eva},
43 from_json => $opt{from_json},
44 json => JSON->new,
45 train_type => $opt{train_type},
46 train_number => $opt{train_number},
47 user_agent => $opt{user_agent},
48 };
49  
50 bless( $self, $class );
51  
52 if ( not $self->{user_agent} ) {
53 my %lwp_options = %{ $opt{lwp_options} // { timeout => 10 } };
54 $self->{user_agent} = LWP::UserAgent->new(%lwp_options);
55 $self->{user_agent}->env_proxy;
56 }
57  
58 $self->get_wagonorder;
59  
60 return $self;
61 }
62  
63 sub get_wagonorder {
64 my ($self) = @_;
65  
66 my $api_base = $self->{api_base};
67 my $cache = $self->{cache};
68 my $eva = $self->{eva};
69 my $train_type = $self->{train_type};
70 my $train_number = $self->{train_number};
71  
72 my $json = $self->{from_json};
73  
74 if ( not $json ) {
75 my $datetime = $self->{departure}->clone->set_time_zone('UTC');
76 my $date = $datetime->strftime('%Y-%m-%d');
77 my $time = $datetime->rfc3339 =~ s{(?=Z)}{.000}r;
78 $self->{param} = {
79 administrationId => 80,
80 category => $train_type,
81 date => $date,
82 evaNumber => $eva,
83 number => $train_number,
84 time => $time
85 };
86 my $url
87 = $api_base . '?'
88 . join( '&',
89 map { $_ . '=' . $self->{param}{$_} } keys %{ $self->{param} } );
90 my ( $content, $err ) = $self->get_with_cache( $cache, $url );
91  
92 if ($err) {
93 $self->{errstr} = "GET $url: $err";
94 return;
95 }
96 $json = $self->{from_json} = $self->{json}->utf8->decode($content);
97 }
98  
99 if ( exists $json->{error} ) {
100 $self->{errstr} = 'Backend error: ' . $json->{error}{msg};
101 return;
102 }
103  
104 if ( not $json->{departureID} ) {
105 $self->{errstr} = 'No carriage formation available';
106 return;
107 }
108  
109 return $self->parse_wagonorder;
110 }
111  
112 # }}}
113 # {{{ Internal Helpers
114  
115 sub get_with_cache {
116 my ( $self, $cache, $url ) = @_;
117  
118 if ( $self->{developer_mode} ) {
119 say "GET $url";
120 }
121  
122 if ($cache) {
123 my $content = $cache->thaw($url);
124 if ($content) {
125 if ( $self->{developer_mode} ) {
126 say ' cache hit';
127 }
128 return ( ${$content}, undef );
129 }
130 }
131  
132 if ( $self->{developer_mode} ) {
133 say ' cache miss';
134 }
135  
136 my $ua = $self->{user_agent};
137 my $res = $ua->get($url);
138  
139 if ( $res->is_error ) {
140 return ( undef, $res->status_line );
141 }
142 my $content = $res->decoded_content;
143  
144 if ($cache) {
145 $cache->freeze( $url, \$content );
146 }
147  
148 return ( $content, undef );
149 }
150  
151 sub parse_wagonorder {
152 my ($self) = @_;
153  
154 $self->{platform} = $self->{from_json}{departurePlatform};
155 $self->{platform_sched} = $self->{from_json}{departurePlatformSchedule};
156  
157 $self->parse_carriages;
158 $self->{destinations} = $self->merge_group_attr('destination');
159 $self->{train_numbers} = $self->merge_group_attr('train_no');
160 $self->{trains} = $self->merge_group_attr('train');
161 }
162  
163 sub merge_group_attr {
164 my ( $self, $attr ) = @_;
165  
166 my @attrs;
167 my %attr_to_group;
168 my %attr_to_sectors;
169  
170 for my $group ( $self->groups ) {
171 push( @attrs, $group->{$attr} );
172 push( @{ $attr_to_group{ $group->{$attr} } }, $group );
173 push( @{ $attr_to_sectors{ $group->{$attr} } }, $group->sectors );
174 }
175  
176 @attrs = uniq @attrs;
177  
178 return [
179 map {
180 {
181 name => $_,
182 groups => $attr_to_group{$_},
183 sectors => $attr_to_sectors{$_}
184 }
185 } @attrs
186 ];
187 }
188  
189 sub parse_carriages {
190 my ($self) = @_;
191  
192 my $platform_length
193 = $self->{from_json}{platform}{end} - $self->{from_json}{platform}{start};
194  
195 for my $sector ( @{ $self->{from_json}{platform}{sectors} } ) {
196 push(
197 @{ $self->{sectors} },
198 Travel::Status::DE::DBWagenreihung::Sector->new(
199 json => $sector,
200 platform => {
201 start => $self->{from_json}{platform}{start},
202 end => $self->{from_json}{platform}{end},
203 }
204 )
205 );
206 }
207  
208 my @groups;
209 my @numbers;
210  
211 for my $group ( @{ $self->{from_json}{groups} // [] } ) {
212 my @group_carriages;
213 for my $carriage ( @{ $group->{vehicles} // [] } ) {
214 my $carriage_object
215 = Travel::Status::DE::DBWagenreihung::Carriage->new(
216 json => $carriage,
217 platform => {
218 start => $self->{from_json}{platform}{start},
219 end => $self->{from_json}{platform}{end},
220 }
221 );
222 push( @group_carriages, $carriage_object );
223 push( @{ $self->{carriages} }, $carriage_object );
224 }
225 @group_carriages
226 = sort { $a->start_percent <=> $b->start_percent } @group_carriages;
227 my $group_obj = Travel::Status::DE::DBWagenreihung::Group->new(
228 json => $group,
229 carriages => \@group_carriages,
230 );
231 push( @groups, $group_obj );
232 push( @numbers, $group_obj->train_no );
233 }
234  
235 @groups = sort { $a->start_percent <=> $b->start_percent } @groups;
236  
237 @numbers = uniq @numbers;
238 $self->{train_numbers} = \@numbers;
239  
240 if ( @{ $self->{carriages} // [] } > 1 ) {
241 if ( $self->{carriages}[0]->{start_percent}
242 > $self->{carriages}[-1]->{start_percent} )
243 {
244 $self->{direction} = 100;
245 }
246 else {
247 $self->{direction} = 0;
248 }
249 }
250  
251 $self->{groups} = [@groups];
252 }
253  
254 # }}}
255 # {{{ Public Functions
256  
257 sub errstr {
258 my ($self) = @_;
259  
260 return $self->{errstr};
261 }
262  
263 sub destinations {
264 my ($self) = @_;
265  
266 return @{ $self->{destinations} // [] };
267 }
268  
269 sub train_numbers {
270 my ($self) = @_;
271  
272 return @{ $self->{train_numbers} // [] };
273 }
274  
275 sub trains {
276 my ($self) = @_;
277  
278 return @{ $self->{trains} // [] };
279 }
280  
281 sub sectors {
282 my ($self) = @_;
283  
284 return @{ $self->{sectors} // [] };
285 }
286  
287 sub groups {
288 my ($self) = @_;
289 return @{ $self->{groups} // [] };
290 }
291  
292 sub carriages {
293 my ($self) = @_;
294 return @{ $self->{carriages} // [] };
295 }
296  
297 sub TO_JSON {
298 my ($self) = @_;
299  
300 my %copy = %{$self};
301  
302 delete $copy{from_json};
303  
304 return {%copy};
305 }
306  
307 # }}}
308  
309 1;
310  
311 __END__