summaryrefslogtreecommitdiff
path: root/lib/Travel/Routing/DE/DBRIS/Connection
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2025-01-18 22:18:48 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2025-01-18 22:18:48 +0100
commit9bac2c56e91db08d9081727549a8bbf84f3a7ee9 (patch)
tree929e5549eb174f8892a58822b7ec2fedcce1614e /lib/Travel/Routing/DE/DBRIS/Connection
Initial Commit
Diffstat (limited to 'lib/Travel/Routing/DE/DBRIS/Connection')
-rw-r--r--lib/Travel/Routing/DE/DBRIS/Connection/Segment.pm79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/Travel/Routing/DE/DBRIS/Connection/Segment.pm b/lib/Travel/Routing/DE/DBRIS/Connection/Segment.pm
new file mode 100644
index 0000000..b8134bb
--- /dev/null
+++ b/lib/Travel/Routing/DE/DBRIS/Connection/Segment.pm
@@ -0,0 +1,79 @@
+package Travel::Routing::DE::DBRIS::Connection::Segment;
+
+use strict;
+use warnings;
+use 5.020;
+
+use parent 'Class::Accessor';
+
+use DateTime::Duration;
+
+our $VERSION = '0.01';
+
+Travel::Routing::DE::DBRIS::Connection::Segment->mk_ro_accessors(
+ qw(
+ dep_name dep_eva arr_name arr_eva
+ train train_long train_mid train_short direction
+ sched_dep rt_dep dep
+ sched_arr rt_arr arr
+ sched_duration rt_duration duration duration_percent
+ journey_id
+ )
+);
+
+sub new {
+ my ( $obj, %opt ) = @_;
+
+ my $json = $opt{json};
+ my $strptime = $opt{strptime_obj};
+
+ my $ref = {
+ arr_eva => $json->{ankunftsOrtExtId},
+ arr_name => $json->{ankunftsOrt},
+ dep_eva => $json->{abfahrtsOrtExtId},
+ dep_name => $json->{abfahrtsOrt},
+ train => $json->{verkehrsmittel}{name},
+ train_short => $json->{verkehrsmittel}{kurzText},
+ train_mid => $json->{verkehrsmittel}{mittelText},
+ train_long => $json->{verkehrsmittel}{langText},
+ direction => $json->{verkehrsmittel}{richtung},
+ };
+
+ if ( my $ts = $json->{abfahrtsZeitpunkt} ) {
+ $ref->{sched_dep} = $strptime->parse_datetime($ts);
+ }
+ if ( my $ts = $json->{ezAbfahrtsZeitpunkt} ) {
+ $ref->{rt_dep} = $strptime->parse_datetime($ts);
+ }
+ $ref->{dep} = $ref->{rt_dep} // $ref->{sched_dep};
+
+ if ( my $ts = $json->{ankunftsZeitpunkt} ) {
+ $ref->{sched_arr} = $strptime->parse_datetime($ts);
+ }
+ if ( my $ts = $json->{ezAnkunftsZeitpunkt} ) {
+ $ref->{rt_arr} = $strptime->parse_datetime($ts);
+ }
+ $ref->{arr} = $ref->{rt_arr} // $ref->{sched_arr};
+
+ if ( my $d = $json->{abschnittsDauerInSeconds} ) {
+ $ref->{sched_duration} = DateTime::Duration->new(
+ hours => int( $d / 3600 ),
+ minutes => int( ( $d % 3600 ) / 60 ),
+ seconds => $d % 60,
+ );
+ }
+ if ( my $d = $json->{ezAbschnittsDauerInSeconds} ) {
+ $ref->{rt_duration} = DateTime::Duration->new(
+ hours => int( $d / 3600 ),
+ minutes => int( ( $d % 3600 ) / 60 ),
+ seconds => $d % 60,
+ );
+ }
+ $ref->{duration} = $ref->{rt_duration} // $ref->{sched_duration};
+
+ bless( $ref, $obj );
+
+ return $ref;
+}
+
+1;