Unverified Commit a85266ef authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Support JSON output for /z/:train/*station

Closes #13
parent 2d66be08
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -279,8 +279,9 @@ sub startup {

	$r->get('/_ajax_mapinfo/:tripid/:lineno')->to('map#ajax_route');
	$r->get('/map/:tripid/:lineno')->to('map#route');
	$r->get( '/z/:train/*station' => 'train_at_station' )
	  ->to('stationboard#station_train_details');
	$r->get( '/z/:train/*station' => [ format => [ 'html', 'json' ] ] )
	  ->to( 'stationboard#station_train_details', format => undef )
	  ->name('train_at_station');
	$r->get( '/z/:train' => [ format => [ 'html', 'json' ] ] )
	  ->to( 'stationboard#train_details', format => undef )->name('train');

+34 −21
Original line number Diff line number Diff line
@@ -937,8 +937,15 @@ sub render_train {
	# Defer rendering until all requests have completed
	Mojo::Promise->all(@requests)->then(
		sub {
			$self->render(
				$template // '_train_details',
			$self->respond_to(
				json => {
					json => {
						departure    => $departure,
						station_name => $station_name,
					},
				},
				any => {
					template    => $template // '_train_details',
					description => sprintf(
						'%s %s%s%s nach %s',
						$departure->{train_type},
@@ -952,12 +959,14 @@ sub render_train {
					dt_now    => DateTime->now( time_zone => 'Europe/Berlin' ),
					station_name => $station_name,
					nav_link     =>
				  $self->url_for( 'station', station => $station_name )->query(
					  $self->url_for( 'station', station => $station_name )
					  ->query(
						{
							detailed => $self->param('detailed'),
							hafas    => $self->param('hafas')
						}
					  ),
				},
			);
		}
	)->wait;
@@ -973,6 +982,10 @@ sub station_train_details {
		delete $self->stash->{layout};
	}

	if ( $station =~ s{ [.] json $ }{}x ) {
		$self->stash( format => 'json' );
	}

	my %opt = (
		cache_iris_main => $self->app->cache_iris_main,
		cache_iris_rt   => $self->app->cache_iris_rt,