DBWagenreihung/Carriage.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 201 CC 22 SCAR 43.3
CC
22
Cov
0%
parse_type
132.0
new
110.0
is_first_class
6.0
CRAP 76.3
1 package Travel::Status::DE::DBWagenreihung::Carriage;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6 use utf8;
7  
8 use parent 'Class::Accessor';
9 use Carp qw(cluck);
10  
11 our $VERSION = '0.18';
12 Travel::Status::DE::DBWagenreihung::Carriage->mk_ro_accessors(
13 qw(class_type is_closed is_dosto is_locomotive is_powercar
14 number model section uic_id type
15 start_meters end_meters length_meters start_percent end_percent length_percent
16 has_priority_seats has_ac has_quiet_zone has_bahn_comfort has_wheelchair_space
17 has_wheelchair_toilet has_family_zone has_infant_cabin has_info has_bistro
18 has_first_class has_second_class
19 )
20 );
21  
22 my %type_map = (
23 SEATS_SEVERELY_DISABLE => 'priority_seats',
24 AIR_CONDITION => 'ac',
25 ZONE_QUIET => 'quiet_zone',
26 SEATS_BAHN_COMFORT => 'bahn_comfort',
27 INFO => 'info',
28 TOILET_WHEELCHAIR => 'wheelchair_toilet',
29 WHEELCHAIR_SPACE => 'wheelchair_space',
30 ZONE_FAMILY => 'family_zone',
31 CABIN_INFANT => 'infant_cabin',
32 );
33  
34 sub new {
35 my ( $obj, %opt ) = @_;
36 my $ref = {};
37  
38 my %json = %{ $opt{json} };
39 my $platform = $opt{platform};
40  
41 $ref->{class_type} = 0;
42 $ref->{has_bistro} = 0;
43 $ref->{is_locomotive} = 0;
44 $ref->{is_powercar} = 0;
45 $ref->{is_closed} = 0;
46 $ref->{number} = $json{wagonIdentificationNumber};
47 $ref->{model} = $json{vehicleID};
48 $ref->{uic_id} = $json{vehicleID};
49 $ref->{section} = $json{platformPosition}{sector};
50 $ref->{type} = $json{type}{constructionType};
51  
52 $ref->{model} =~ s{^.....(...)....(?:-.)?$}{$1} or $ref->{model} = undef;
53  
54 my $self = bless( $ref, $obj );
55  
56 $self->parse_type;
57  
58 for my $amenity ( @{ $json{amenities} // [] } ) {
59 my $type = $amenity->{type};
60 if ( $type_map{$type} ) {
61 my $key = 'has_' . $type_map{$type};
62 $self->{$key} = 1;
63 }
64 }
65  
66 if ( $json{status} and $json{status} eq 'CLOSED' ) {
67 $ref->{is_closed} = 1;
68 }
69  
70 if ( $json{type}{category} =~ m{DININGCAR} ) {
71 $ref->{has_bistro} = 1;
72 }
73 elsif ( $json{type}{category} eq 'LOCOMOTIVE' ) {
74 $ref->{is_locomotive} = 1;
75 }
76 elsif ( $json{type}{category} eq 'POWERCAR' ) {
77 $ref->{is_powercar} = 1;
78 }
79  
80 $ref->{has_first_class} = $json{type}{hasFirstClass};
81 $ref->{has_second_class} = $json{type}{hasEconomyClass};
82  
83 if ( $ref->{type} =~ m{AB} ) {
84 $ref->{class_type} = 12;
85 }
86 elsif ( $ref->{type} =~ m{A} ) {
87 $ref->{class_type} = 1;
88 }
89 elsif ( $ref->{type} =~ m{B|WR} ) {
90 $ref->{class_type} = 2;
91 }
92  
93 my $pos = $json{platformPosition};
94 my $platform_length = $platform->{end} - $platform->{start};
95  
96 $ref->{start_meters} = $pos->{start};
97 $ref->{end_meters} = $pos->{end};
98 $ref->{start_percent}
99 = ( $pos->{start} - $platform->{start} ) * 100 / $platform_length,
100 $ref->{end_percent}
101 = ( $pos->{end} - $platform->{start} ) * 100 / $platform_length,
102 $ref->{length_meters} = $pos->{start} - $pos->{end};
103 $ref->{length_percent} = $ref->{end_percent} - $ref->{start_percent};
104  
105 if ( $pos->{start} eq ''
106 or $pos->{end} eq '' )
107 {
108 $ref->{position}{valid} = 0;
109 }
110 else {
111 $ref->{position}{valid} = 1;
112 }
113  
114 return $self;
115 }
116  
117 sub attributes {
118 my ($self) = @_;
119  
120 return @{ $self->{attributes} // [] };
121 }
122  
123 # See also:
124 # https://de.wikipedia.org/wiki/UIC-Bauart-Bezeichnungssystem_f%C3%BCr_Reisezugwagen#Kennbuchstaben
125 # https://www.deutsche-reisezugwagen.de/lexikon/erklarung-der-gattungszeichen/
126 sub parse_type {
127 my ($self) = @_;
128  
129 my $type = $self->{type};
130 my @desc;
131  
132 if ( $type =~ m{^D} ) {
133 $self->{is_dosto} = 1;
134 push( @desc, 'Doppelstock' );
135 }
136  
137 if ( $type =~ m{b} ) {
138 $self->{has_accessibility} = 1;
139 push( @desc, 'Behindertengerechte Ausstattung' );
140 }
141  
142 if ( $type =~ m{d} ) {
143 $self->{multipurpose} = 1;
144 push( @desc, 'Mehrzweck' );
145 }
146  
147 if ( $type =~ m{f} ) {
148 push( @desc, 'Steuerabteil' );
149 }
150  
151 if ( $type =~ m{i} ) {
152 push( @desc, 'Interregio' );
153 }
154  
155 if ( $type =~ m{mm} ) {
156 push( @desc, 'modernisiert' );
157 }
158  
159 if ( $type =~ m{p} ) {
160 $self->{has_ac} = 1;
161 push( @desc, 'Großraum' );
162 }
163  
164 if ( $type =~ m{s} ) {
165 push( @desc, 'Sonderabteil' );
166 }
167  
168 if ( $type =~ m{v} ) {
169 $self->{has_ac} = 1;
170 $self->{has_compartments} = 1;
171 push( @desc, 'Abteil' );
172 }
173  
174 if ( $type =~ m{w} ) {
175 $self->{has_ac} = 1;
176 $self->{has_compartments} = 1;
177 push( @desc, 'Abteil' );
178 }
179  
180 $self->{attributes} = \@desc;
181 }
182  
183 sub is_first_class {
184 my ($self) = @_;
185  
186 if ( $self->{type} =~ m{^D?A} ) {
187 return 1;
188 }
189 return 0;
190 }
191  
192 sub is_second_class {
193 my ($self) = @_;
194  
195 if ( $self->{type} =~ m{^D?A?B} ) {
196 return 1;
197 }
198 return 0;
199 }
200  
201 sub sections {
202 my ($self) = @_;
203  
204 return @{ $self->{sections} };
205 }
206  
207 sub TO_JSON {
208 my ($self) = @_;
209  
210 my %copy = %{$self};
211  
212 return {%copy};
213 }
214  
215 1;