Commit 8d6c6d5a authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

move get_station into separate method to prepare for recurvise fetching

parent 7a24a7d8
Loading
Loading
Loading
Loading
+41 −21
Original line number Diff line number Diff line
@@ -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) = @_;