summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-07-13 13:33:17 +0200
committerDaniel Friesel <derf@finalrewind.org>2011-07-13 13:33:17 +0200
commitd0f1eccd06746b602fcc468a6d14465c0195004d (patch)
treee41632f726a52b3c7c4504b91f21d7dca7b27ed5
parent10687196f1f39d01c17a415898758b3a65608396 (diff)
DeutscheBahn/Result: Add route_interesting accessor
-rw-r--r--Changelog1
-rwxr-xr-xbin/db-ris42
-rw-r--r--lib/Travel/Status/DE/DeutscheBahn/Result.pm57
3 files changed, 60 insertions, 40 deletions
diff --git a/Changelog b/Changelog
index ff7b584..f390f9f 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ git HEAD
* The info accessor now strips the mostly useless "k.A." / "pünktlich"
(train on time) text parts
* Use the new info_raw accessor to get the old info results
+ * Add (slightly experimental) route_interesting accessor
Travel::Status::DE::DeutscheBahn 0.03 - Wed Jul 13 2011
diff --git a/bin/db-ris b/bin/db-ris
index 962b5cd..d787073 100755
--- a/bin/db-ris
+++ b/bin/db-ris
@@ -84,47 +84,9 @@ sub display_result {
return;
}
-sub filter_via {
- my (@via) = @_;
-
- my ( @via_main, @via_show, $last_stop );
-
- for my $stop (@via) {
- if ( $stop =~ m{ ?Hbf} ) {
- 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 < 3 ) {
- @via_show = @via;
- }
- else {
- @via_show = splice( @via, 0, ( @via_main > 2 ? 1 : 3 - @via_main ) );
-
- while ( @via_show < 3 and @via_main ) {
- my $stop = shift(@via_main);
- if ( $stop ~~ \@via_show or $stop eq $last_stop ) {
- next;
- }
- push( @via_show, $stop );
- }
- }
-
- for my $stop (@via_show) {
- $stop =~ s{ ?Hbf}{};
- }
-
- return @via_show;
-}
-
for my $d ( $status->results() ) {
- my ( @via, @via_main, @via_show );
+ my @via;
my $re_late = qr{ (?: ^ | , ) ca[.] \s \d+ \s Minuten \s sp.ter}ox;
@@ -142,7 +104,7 @@ for my $d ( $status->results() ) {
@output,
[
$d->time, $d->train,
- join( q{ }, filter_via(@via) ), $d->destination,
+ join( q{ }, $d->route_interesting ), $d->destination,
$d->platform, $d->info,
]
);
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<max>])
+
+Returns a list of (at most I<max>) 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.