summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Helper
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-08-06 16:04:12 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-08-06 16:04:12 +0200
commit717cc18a403d6705c64a9a6fd43578c1efbb159f (patch)
treed5e1ab56c42b85a0fa37f73c403f2eb177213701 /lib/Travelynx/Helper
parentadaf65dc634b68e0890899e93bed62f0bbcb548a (diff)
Move get_departures to a separate IRIS helper
Diffstat (limited to 'lib/Travelynx/Helper')
-rw-r--r--lib/Travelynx/Helper/IRIS.pm71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/Travelynx/Helper/IRIS.pm b/lib/Travelynx/Helper/IRIS.pm
new file mode 100644
index 0000000..3b98a51
--- /dev/null
+++ b/lib/Travelynx/Helper/IRIS.pm
@@ -0,0 +1,71 @@
+package Travelynx::Helper::IRIS;
+
+use strict;
+use warnings;
+use 5.020;
+
+use Travel::Status::DE::IRIS;
+
+sub new {
+ my ( $class, %opt ) = @_;
+
+ return bless( \%opt, $class );
+}
+
+sub get_departures {
+ my ( $self, %opt ) = @_;
+ my $station = $opt{station};
+ my $lookbehind = $opt{lookbehind} // 180;
+ my $lookahead = $opt{lookahead} // 30;
+ my $with_related = $opt{with_related} // 0;
+
+ my @station_matches
+ = Travel::Status::DE::IRIS::Stations::get_station($station);
+
+ if ( @station_matches == 1 ) {
+ $station = $station_matches[0][0];
+ my $status = Travel::Status::DE::IRIS->new(
+ station => $station,
+ main_cache => $self->{main_cache},
+ realtime_cache => $self->{realtime_cache},
+ keep_transfers => 1,
+ lookbehind => 20,
+ datetime => DateTime->now( time_zone => 'Europe/Berlin' )
+ ->subtract( minutes => $lookbehind ),
+ lookahead => $lookbehind + $lookahead,
+ lwp_options => {
+ timeout => 10,
+ agent => 'travelynx/'
+ . $self->{version}
+ . ' +https://travelynx.de',
+ },
+ with_related => $with_related,
+ );
+ return {
+ results => [ $status->results ],
+ errstr => $status->errstr,
+ station_ds100 =>
+ ( $status->station ? $status->station->{ds100} : undef ),
+ station_eva =>
+ ( $status->station ? $status->station->{uic} : undef ),
+ station_name =>
+ ( $status->station ? $status->station->{name} : undef ),
+ related_stations => [ $status->related_stations ],
+ };
+ }
+ elsif ( @station_matches > 1 ) {
+ return {
+ results => [],
+ errstr => 'Mehrdeutiger Stationsname. Mögliche Eingaben: '
+ . join( q{, }, map { $_->[1] } @station_matches ),
+ };
+ }
+ else {
+ return {
+ results => [],
+ errstr => 'Unbekannte Station',
+ };
+ }
+}
+
+1;