File Coverage

blib/lib/Travel/Status/DE/HAFAS/Product.pm
Criterion Covered Total %
statement 36 42 85.7
branch 8 14 57.1
condition 15 30 50.0
subroutine 5 6 83.3
pod 1 2 50.0
total 65 94 69.1


line stmt bran cond sub pod time code
1             package Travel::Status::DE::HAFAS::Product;
2              
3             # vim:foldmethod=marker
4              
5 5     5   30 use strict;
  5         9  
  5         158  
6 5     5   21 use warnings;
  5         11  
  5         224  
7 5     5   70 use 5.014;
  5         15  
8              
9 5     5   80 use parent 'Class::Accessor';
  5         26  
  5         72  
10              
11             our $VERSION = '6.25';
12              
13             Travel::Status::DE::HAFAS::Product->mk_ro_accessors(
14             qw(class line_id line_no name number type type_long operator));
15              
16             # {{{ Constructor
17              
18             sub new {
19 45     45 1 131 my ( $obj, %opt ) = @_;
20              
21 45         82 my $product = $opt{product};
22 45         85 my $common = $opt{common};
23 45         84 my $opL = $common->{opL};
24              
25             # DB:
26             # catIn / catOutS eq "IXr" => "ICE X Regio"? regional tickets are generally accepted
27             # <= does not hold
28              
29 45         92 my $class = $product->{cls};
30 45   33     230 my $name = $product->{addName} // $product->{name};
31 45         108 my $line_no = $product->{prodCtx}{line};
32 45         94 my $train_no = $product->{prodCtx}{num};
33 45         128 my $cat = $product->{prodCtx}{catOut};
34 45         93 my $catlong = $product->{prodCtx}{catOutL};
35              
36             # ÖBB, you so silly
37 45 0 33     182 if ( $name and $name =~ m{Zug-Nr} and $product->{nameS} ) {
      33        
38 0         0 $name = $product->{nameS};
39             }
40              
41 45 0 33     244 if ( $name and $cat and $name eq $cat and $product->{nameS} ) {
      33        
      33        
42 0         0 $name .= ' ' . $product->{nameS};
43             }
44              
45 45 50 66     257 if ( defined $train_no and not $train_no ) {
46 0         0 $train_no = undef;
47             }
48              
49 45 100 66     161 if (
      66        
      100        
50             not defined $line_no
51             and defined $product->{prodCtx}{matchId}
52             and
53             ( not defined $train_no or $product->{prodCtx}{matchId} ne $train_no )
54             )
55             {
56 3         9 $line_no = $product->{prodCtx}{matchId};
57             }
58              
59 45         78 my $line_id;
60 45 100       112 if ( $product->{prodCtx}{lineId} ) {
61 40         209 $line_id = lc( $product->{prodCtx}{lineId} =~ s{_+}{-}gr );
62             }
63              
64 45         103 my $operator;
65 45 100       127 if ( defined $product->{oprX} ) {
66 33 50       105 if ( my $opref = $opL->[ $product->{oprX} ] ) {
67 33         73 $operator = $opref->{name};
68             }
69             }
70              
71 45         290 my $ref = {
72             name => $name,
73             number => $train_no,
74             line_id => $line_id,
75             line_no => $line_no,
76             type => $cat,
77             type_long => $catlong,
78             class => $class,
79             operator => $operator,
80             };
81              
82 45         96 bless( $ref, $obj );
83              
84 45         190 return $ref;
85             }
86              
87             # }}}
88              
89             sub TO_JSON {
90 0     0 0   my ($self) = @_;
91              
92 0           return { %{$self} };
  0            
93             }
94              
95             1;
96              
97             __END__
98              
99             =head1 NAME
100              
101             Travel::Status::DE::HAFAS::Product - Information about a HAFAS product
102             associated with a journey.
103              
104             =head1 SYNOPSIS
105              
106             =head1 VERSION
107              
108             version 6.25
109              
110             =head1 DESCRIPTION
111              
112             Travel::Status::DE::HAFAS::Product describes a product (e.g. train or bus)
113             associated with a Travel::Status::DE::HAFAS::Journey(3pm) or one of its
114             stops.
115              
116             =head1 METHODS
117              
118             =head2 ACCESSORS
119              
120             =over
121              
122             =item $product->class
123              
124             An integer identifying the the mode of transport class. Semantics depend on
125             backend See Travel::Status::DE::HAFAS(3pm)'s C<< $hafas->get_active_service >>
126             method.
127              
128             =item $product->line_id
129              
130             Line identifier, or undef if it is unknown.
131             This is a backend-specific identifier, e.g. "7-vrr010-17" for VRR U17.
132             The format is compatible with L<https://github.com/Traewelling/line-colors>.
133              
134             =item $product->line_no
135              
136             Line number, or undef if it is unknown.
137             The line identifier may be a single number such as "11" (underground train
138             line U 11), a single word (e.g. "AIR") or a combination (e.g. "SB16").
139             May also provide line numbers of IC/ICE services.
140              
141             =item $product->name
142              
143             Trip or line name, either in a format like "Bus SB16" (Bus line
144             SB16), "RE 42" (RegionalExpress train 42) or "IC 2901" (InterCity train 2901,
145             no line information). May contain extraneous whitespace characters. Note that
146             this accessor does not return line information for DB IC/ICE/EC services, even
147             if it is available. Use B<line_no> for those.
148              
149             =item $product->number
150              
151             Trip number (e.g. train number), or undef if it is unknown.
152              
153             =item $product->type
154              
155             Type of this product, e.g. "S" for S-Bahn, "RE" for Regional Express
156             or "STR" for tram / StraE<szlig>enbahn.
157              
158             =item $product->type_long
159              
160             Long type of this product, e.g. "S-Bahn" or "Regional-Express".
161              
162             =item $product->operator
163              
164             The operator responsible for this product. Returns undef
165             if the backend does not provide an operator.
166              
167             =back
168              
169             =head1 DIAGNOSTICS
170              
171             None.
172              
173             =head1 DEPENDENCIES
174              
175             =over
176              
177             =item Class::Accessor(3pm)
178              
179             =back
180              
181             =head1 BUGS AND LIMITATIONS
182              
183             None known.
184              
185             =head1 SEE ALSO
186              
187             Travel::Status::DE::HAFAS(3pm).
188              
189             =head1 AUTHOR
190              
191             Copyright (C) 2024 by Birte Kristina Friesel E<lt>derf@finalrewind.orgE<gt>
192              
193             =head1 LICENSE
194              
195             This module is licensed under the same terms as Perl itself.