From e360b55ec5d4d9e725510400a2ecd14dd14fb8c5 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 21 Dec 2013 16:39:14 +0100 Subject: Result: Remove _date, _time accessors, add more route support --- lib/Travel/Status/DE/IRIS.pm | 11 ++++--- lib/Travel/Status/DE/IRIS/Result.pm | 62 ++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 37 deletions(-) (limited to 'lib') diff --git a/lib/Travel/Status/DE/IRIS.pm b/lib/Travel/Status/DE/IRIS.pm index 7cb35ea..16883a4 100644 --- a/lib/Travel/Status/DE/IRIS.pm +++ b/lib/Travel/Status/DE/IRIS.pm @@ -53,6 +53,9 @@ sub new { $dt_req->add( hours => 1 ); } + @{ $self->{results} } + = sort { $a->{datetime} <=> $b->{datetime} } @{ $self->{results} }; + return $self; } @@ -60,9 +63,6 @@ sub get_timetable { my ( $self, $eva, $dt ) = @_; my $ua = $self->{user_agent}; - say $dt->strftime( - "http://iris.noncd.db.de/iris-tts/timetable/plan/${eva}/%y%m%d/%H"); - my $res = $ua->get( $dt->strftime( "http://iris.noncd.db.de/iris-tts/timetable/plan/${eva}/%y%m%d/%H") @@ -75,6 +75,8 @@ sub get_timetable { my $xml = XML::LibXML->load_xml( string => $res->decoded_content ); + my $station = ( $xml->findnodes('/timetable') )[0]->getAttribute('station'); + for my $s ( $xml->findnodes('/timetable/s') ) { my $id = $s->getAttribute('id'); my $e_tl = ( $s->findnodes('./tl') )[0]; @@ -92,6 +94,7 @@ sub get_timetable { train_no => $e_tl->getAttribute('n'), # dep number type => $e_tl->getAttribute('c'), # S/ICE/ERB/... line_no => $e_tl->getAttribute('l'), # 1 -> S1, ... + station => $station, unknown_o => $e_tl->getAttribute('o'), # owner: 03/80/R2/... ); @@ -115,8 +118,6 @@ sub get_timetable { ); } - say $xml->toString(1); - return $self; } diff --git a/lib/Travel/Status/DE/IRIS/Result.pm b/lib/Travel/Status/DE/IRIS/Result.pm index a952cd9..d422f6d 100644 --- a/lib/Travel/Status/DE/IRIS/Result.pm +++ b/lib/Travel/Status/DE/IRIS/Result.pm @@ -14,11 +14,9 @@ use DateTime::Format::Strptime; our $VERSION = '0.00'; Travel::Status::DE::IRIS::Result->mk_ro_accessors( - qw(arrival_date arrival_datetime arrival_time date datetime - departure_date departure_datetime departure_time line_no raw_id - start_date start_datetime start_time stop_no - time train_id train_no - type unknown_t unknown_o) + qw(arrival date datetime departure line_no raw_id route route_post route_pre + route_start route_end + start stop_no time train_id train_no type unknown_t unknown_o) ); sub new { @@ -33,55 +31,61 @@ sub new { my ( $train_id, $start_ts, $stop_no ) = split( /.\K-/, $opt{raw_id} ); - $ref->{start_datetime} = $strp->parse_datetime($start_ts); - $ref->{start_date} = $ref->{start_datetime}->strftime('%d.%m.%Y'); - $ref->{start_time} = $ref->{start_datetime}->strftime('%H:%M'); + $ref->{start} = $strp->parse_datetime($start_ts); $ref->{train_id} = $train_id; $ref->{stop_no} = $stop_no; - $ref->{arrival_datetime} = $strp->parse_datetime( $opt{arrival_ts} ); - $ref->{departure_datetime} = $strp->parse_datetime( $opt{departure_ts} ); + my $ar = $ref->{arrival} = $strp->parse_datetime( $opt{arrival_ts} ); + my $dp = $ref->{departure} = $strp->parse_datetime( $opt{departure_ts} ); - if ( not( $ref->{arrival_datetime} or $ref->{departure_datetime} ) ) { + if ( not( $ar or $dp ) ) { cluck( sprintf( - "Neither '%s' nor '%s' are valid timestamps", + "Neither arrival '%s' nor departure '%s' are valid " + . "timestamps - can't handle this train", $opt{arrival_ts}, $opt{departure_ts} ) ); } - if ( $ref->{arrival_datetime} ) { - my $dt = $ref->{datetime} = $ref->{arrival_datetime}; - $ref->{arrival_date} = $ref->{date} = $dt->strftime('%d.%m.%Y'); - $ref->{arrival_time} = $ref->{time} = $dt->strftime('%H:%M'); - } - if ( $ref->{departure_datetime} ) { - my $dt = $ref->{datetime} = $ref->{departure_datetime}; - $ref->{departure_date} = $ref->{date} = $dt->strftime('%d.%m.%Y'); - $ref->{departure_time} = $ref->{time} = $dt->strftime('%H:%M'); - } + my $dt = $ref->{datetime} = $ar // $dp; + + $ref->{date} = $dt->strftime('%d.%m.%Y'); + $ref->{time} = $dt->strftime('%H:%M'); + + $ref->{route_pre} = [ split( qr{\|}, $ref->{route_pre} // q{} ) ]; + $ref->{route_post} = [ split( qr{\|}, $ref->{route_post} // q{} ) ]; + + $ref->{route_end} = $ref->{route_post}[-1] || $ref->{station}; + $ref->{route_start} = $ref->{route_pre}[0] || $ref->{station}; return bless( $ref, $obj ); } +sub origin { + my ($self) = @_; + + return $self->route_start; +} + sub destination { my ($self) = @_; - return $self->{route_end}; + return $self->route_end; } sub line { my ($self) = @_; - return $self->{train}; + return + sprintf( '%s %s', $self->{type}, $self->{line_no} // $self->{train_no} ); } -sub origin { +sub train { my ($self) = @_; - return $self->{route_end}; + return $self->line; } sub route_interesting { @@ -134,12 +138,6 @@ sub route_interesting { } -sub route_timetable { - my ($self) = @_; - - return @{ $self->{route} }; -} - sub TO_JSON { my ($self) = @_; -- cgit v1.2.3