Commit c56337ce authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

add route_interesting (only useful when full_routes is specified)

parent 6ddf48b7
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -322,6 +322,7 @@ sub parse_route {
				dep_date => sprintf_date( $dates[-1] ),
				dep_time => sprintf_time( $times[-1] ),
				stop     => decode( 'UTF-8', $e->getAttribute('name') ),
				stop_suf => decode( 'UTF-8', $e->getAttribute('nameWO') ),
				platform => $e->getAttribute('platformName'),
			}
		);
+50 −0
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ use strict;
use warnings;
use 5.010;

no if $] >= 5.018, warnings => 'experimental::smartmatch';

use parent 'Class::Accessor';

our $VERSION = '1.09';
@@ -41,6 +43,54 @@ sub route_post {
	return @{ $self->{next_route} };
}

sub route_interesting {
	my ( $self, $max_parts ) = @_;

	my @via = $self->route_post;
	my ( @via_main, @via_show, $last_stop );
	$max_parts //= 3;

	for my $stop (@via) {
		if ( $stop =~ m{ Bf | Hbf | Flughafen | S $ }ox ) {
			push( @via_main, $stop );
		}
	}
	$last_stop = pop(@via);

	if ( @via_main and $via_main[-1] eq $last_stop ) {
		pop(@via_main);
	}
	if ( @via and $via[-1] eq $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 );
		}
	}

	return @via_show;
}

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