From 8d6c6d5aa8000fe753810085d56a34e156be503b Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 5 Nov 2015 17:04:43 +0100 Subject: move get_station into separate method to prepare for recurvise fetching --- lib/Travel/Status/DE/IRIS.pm | 62 +++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/Travel/Status/DE/IRIS.pm b/lib/Travel/Status/DE/IRIS.pm index f9300f0..c8c3c8b 100644 --- a/lib/Travel/Status/DE/IRIS.pm +++ b/lib/Travel/Status/DE/IRIS.pm @@ -43,31 +43,16 @@ sub new { $ua->env_proxy; - my $res_st = $ua->get( $self->{iris_base} . '/station/' . $opt{station} ); - - if ( $res_st->is_error ) { - $self->{errstr} - = 'Failed to fetch station data: ' . $res_st->status_line; - return $self; - } - - my $xml_st = XML::LibXML->load_xml( string => $res_st->decoded_content ); - - my $station_node = ( $xml_st->findnodes('//station') )[0]; - - # TODO parse 'meta' and maybe 'p' flags - # They're optional pointers to related platforms. For instance - # Berlin Hbf/BL -> Berlin HBf (tief), Berlin Hbf (S), ... + my ($station_code, $station_name, @related) = $self->get_station( + name => $opt{station}, + ); - if ( not $station_node ) { - $self->{errstr} - = "The station '$opt{station}' has no associated timetable"; + if ($self->{errstr}) { return $self; } - $self->{station} = $opt{station}; - $self->{station_code} = $station_node->getAttribute('eva'); - $self->{station_name} = $station_node->getAttribute('name'); + $self->{station_code} = $station_code; + $self->{station_name} = $station_name; my $dt_req = $self->{datetime}->clone; for ( 1 .. 3 ) { @@ -116,6 +101,35 @@ sub new { return $self; } +sub get_station { + my ($self, %opt) = @_; + + my $ua = $self->{user_agent}; + my $res_st = $ua->get( $self->{iris_base} . '/station/' . $opt{name} ); + + if ( $res_st->is_error ) { + $self->{errstr} + = 'Failed to fetch station data: ' . $res_st->status_line; + return $self; + } + + my $xml_st = XML::LibXML->load_xml( string => $res_st->decoded_content ); + + my $station_node = ( $xml_st->findnodes('//station') )[0]; + + # TODO parse 'meta' and maybe 'p' flags + # They're optional pointers to related platforms. For instance + # Berlin Hbf/BL -> Berlin HBf (tief), Berlin Hbf (S), ... + + if ( not $station_node ) { + $self->{errstr} + = "The station '$opt{name}' has no associated timetable"; + return; + } + + return ($station_node->getAttribute('eva'), $station_node->getAttribute('name')); +} + sub add_result { my ( $self, $station, $s ) = @_; @@ -367,6 +381,12 @@ sub create_replacement_refs { } } +sub station_code { + my ($self) = @_; + + return $self->{station_code}; +} + sub errstr { my ($self) = @_; -- cgit v1.2.3