diff options
| author | Birte Kristina Friesel <birte.friesel@uos.de> | 2024-04-24 18:18:27 +0200 | 
|---|---|---|
| committer | Birte Kristina Friesel <birte.friesel@uos.de> | 2024-04-24 18:18:27 +0200 | 
| commit | a85266ef8432271d988f00e83da9f5ed350f7097 (patch) | |
| tree | 25d3353eab0bb129d6801c21a385a13ab8a1f640 | |
| parent | 2d66be08e488198f69497fed3299280d43c89b29 (diff) | |
Support JSON output for /z/:train/*station
Closes #13
| -rw-r--r-- | lib/DBInfoscreen.pm | 5 | ||||
| -rw-r--r-- | lib/DBInfoscreen/Controller/Stationboard.pm | 55 | 
2 files changed, 37 insertions, 23 deletions
| diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm index dc3b403..49e7402 100644 --- a/lib/DBInfoscreen.pm +++ b/lib/DBInfoscreen.pm @@ -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'); diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 838c63d..078be87 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -937,27 +937,36 @@ sub render_train {  	# Defer rendering until all requests have completed  	Mojo::Promise->all(@requests)->then(  		sub { -			$self->render( -				$template // '_train_details', -				description => sprintf( -					'%s %s%s%s nach %s', -					$departure->{train_type}, -					$departure->{train_line} // $departure->{train_no}, -					$departure->{origin} ? ' von ' : q{}, -					$departure->{origin}      // q{}, -					$departure->{destination} // 'unbekannt' -				), -				departure    => $departure, -				linetype     => $linetype, -				dt_now       => DateTime->now( time_zone => 'Europe/Berlin' ), -				station_name => $station_name, -				nav_link     => -				  $self->url_for( 'station', station => $station_name )->query( -					{ -						detailed => $self->param('detailed'), -						hafas    => $self->param('hafas') -					} -				  ), +			$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}, +						$departure->{train_line} // $departure->{train_no}, +						$departure->{origin} ? ' von ' : q{}, +						$departure->{origin}      // q{}, +						$departure->{destination} // 'unbekannt' +					), +					departure => $departure, +					linetype  => $linetype, +					dt_now    => DateTime->now( time_zone => 'Europe/Berlin' ), +					station_name => $station_name, +					nav_link     => +					  $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, | 
