summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2014-10-20 09:06:40 +0200
committerDaniel Friesel <derf@finalrewind.org>2014-10-20 09:06:40 +0200
commit75ac18c0f062317451d8b96212c4a7ed991a5045 (patch)
treec0a47167d0411b817283fbec982bf0897d63a886
parentbe21dec79365be950f9bc8cb78baa826e3e0f704 (diff)
add jsonp support
-rw-r--r--index.pl45
1 files changed, 36 insertions, 9 deletions
diff --git a/index.pl b/index.pl
index fb3b3f0..9d2ead2 100644
--- a/index.pl
+++ b/index.pl
@@ -62,6 +62,7 @@ sub handle_request {
my $hide_low_delay = $self->param('hidelowdelay') // 0;
my $hide_opts = $self->param('hide_opts') // 0;
my $backend = $self->param('backend') // 'ris';
+ my $callback = $self->param('callback');
$self->stash( departures => [] );
$self->stash( title => 'db-fakedisplay' );
@@ -80,24 +81,38 @@ sub handle_request {
my @results = get_results_for( $backend, $station );
if ( not @results and $template eq 'json' ) {
+ my $json;
if ( $backend eq 'iris' ) {
my @candidates = map { { code => $_->[0], name => $_->[1] } }
Travel::Status::DE::IRIS::Stations::get_station($station);
- $self->render(
+ $json = $self->render_to_string(
json => {
version => $VERSION,
error => 'ambiguous station code/name',
candidates => \@candidates,
}
);
- return;
}
- $self->render(
- json => {
- version => $VERSION,
- error => 'unknown station code/name',
- }
- );
+ else {
+ $json = $self->render_to_string(
+ json => {
+ version => $VERSION,
+ error => 'unknown station code/name',
+ }
+ );
+ }
+ if ($callback) {
+ $self->render(
+ data => "$callback($json);",
+ format => 'json'
+ );
+ }
+ else {
+ $self->render(
+ data => $json,
+ format => 'json'
+ );
+ }
return;
}
@@ -218,13 +233,25 @@ sub handle_request {
}
if ( $template eq 'json' ) {
- $self->render(
+ my $json = $self->render_to_string(
json => {
preformatted => \@departures,
version => $VERSION,
raw => \@results,
}
);
+ if ($callback) {
+ $self->render(
+ data => "$callback($json);",
+ format => 'json'
+ );
+ }
+ else {
+ $self->render(
+ data => $json,
+ format => 'json'
+ );
+ }
}
else {
$self->render(