Skip to content

Commit

Permalink
Journey: Add operators accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
derf committed Apr 3, 2024
1 parent 99ea553 commit 7e92994
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
14 changes: 3 additions & 11 deletions bin/hafas-m
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,10 @@ elsif ( $opt{journey} ) {
$delay_fmt = $delay_len + 3;
}

if ( @prods == 1 ) {
if ( $prev_prod->operator ) {
printf( "Betrieb: %s\n", $prev_prod->operator );
}
}
else {
printf(
"Betrieb: %s\n",
join( q{, },
uniq map { $_->operator } grep { $_->operator } @prods )
);
if ( $result->operators ) {
printf( "Betrieb: %s\n", join( q{, }, $result->operators ) );
}

$prev_prod = 0;

my $desc = Travel::Status::DE::HAFAS::get_service($service) // {};
Expand Down
27 changes: 25 additions & 2 deletions lib/Travel/Status/DE/HAFAS/Journey.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use 5.014;

use parent 'Class::Accessor';
use DateTime::Format::Strptime;
use List::Util qw(any);
use List::Util qw(any uniq);
use Travel::Status::DE::HAFAS::Stop;

our $VERSION = '6.00';
Expand Down Expand Up @@ -270,6 +270,23 @@ sub messages {
return;
}

sub operators {
my ($self) = @_;

if ( $self->{operators} ) {
return @{ $self->{operators} };
}

$self->{operators} = [
uniq map { ( $_->prod_arr // $_->prod_dep )->operator } grep {
( $_->prod_arr or $_->prod_dep )
and ( $_->prod_arr // $_->prod_dep )->operator
} $self->route
];

return @{ $self->{operators} };
}

sub polyline {
my ($self) = @_;

Expand Down Expand Up @@ -549,7 +566,13 @@ detailed delay reasons (e.g. "switch damage between X and Y, expect delays").
=item $journey->operator
The operator responsible for this journey. Returns undef
if the backend does not provide an operator.
if the backend does not provide an operator. Note that the operator may
change along the journey -- in this case, the returned operator depends on
the backend and appears to be the first one in most cases.
=item $journey->operators
List of all operators observed along the journey.
=item $journey->station (station only)
Expand Down

0 comments on commit 7e92994

Please sign in to comment.