From d0f1eccd06746b602fcc468a6d14465c0195004d Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 13 Jul 2011 13:33:17 +0200 Subject: DeutscheBahn/Result: Add route_interesting accessor --- lib/Travel/Status/DE/DeutscheBahn/Result.pm | 57 +++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'lib/Travel') diff --git a/lib/Travel/Status/DE/DeutscheBahn/Result.pm b/lib/Travel/Status/DE/DeutscheBahn/Result.pm index 660abc5..5c2e9e1 100644 --- a/lib/Travel/Status/DE/DeutscheBahn/Result.pm +++ b/lib/Travel/Status/DE/DeutscheBahn/Result.pm @@ -50,6 +50,52 @@ sub route { return @{ $self->{route} }; } +sub route_interesting { + my ( $self, $max_parts ) = @_; + + my @via = $self->route; + my ( @via_main, @via_show, $last_stop ); + $max_parts //= 3; + + for my $stop (@via) { + if ( $stop =~ m{ ?Hbf}o ) { + push( @via_main, $stop ); + } + } + $last_stop = pop(@via); + + if ( @via_main and @via and $via[0] eq $via_main[0] ) { + shift(@via_main); + } + + if ( @via < $max_parts ) { + @via_show = @via; + } + else { + if ( @via_main >= $max_parts ) { + @via_show = ( $via[0] ); + } + else { + @via_show = splice( @via, 0, $max_parts - @via_main ); + } + + while ( @via_show < $max_parts and @via_main ) { + my $stop = shift(@via_main); + if ( $stop ~~ \@via_show or $stop eq $last_stop ) { + next; + } + push( @via_show, $stop ); + } + } + + for (@via_show) { + s{ ?Hbf}{}; + } + + return @via_show; + +} + 1; __END__ @@ -131,6 +177,17 @@ arrive. Returns a list of station names the train will pass between the selected station and its origin/destination. +=item $result->route_interesting([I]) + +Returns a list of (at most I) interesting stations the train will pass on +its journey. This is somewhat tricky (and therefore experimental). + +The first element of the list is always the train's next stop. The following +elements contain as many main stations as possible, but there may also be +smaller stations if not enough main stations are available. + +Note that all main stations will be stripped of their "Hbf" suffix. + =item $result->route_raw Returns the raw string used to create the route array. -- cgit v1.2.3