summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-10-30 11:34:35 +0100
committerDaniel Friesel <derf@finalrewind.org>2011-10-30 11:34:35 +0100
commitfbabb4e97410884c911dafabcbf33493427ab698 (patch)
tree687aeb2da67d272f67cb1a4b1fa30518767a5648
parentb9e6f97704f219837270910f159692f2bbe18608 (diff)
cleanup
-rw-r--r--cgi/index.pl136
-rw-r--r--lib/App/VRR/Fakedisplay.pm.PL32
2 files changed, 85 insertions, 83 deletions
diff --git a/cgi/index.pl b/cgi/index.pl
index d2c5217..8c8dcdb 100644
--- a/cgi/index.pl
+++ b/cgi/index.pl
@@ -11,7 +11,7 @@ use Travel::Status::DE::VRR;
our $VERSION = '0.00';
sub get_results_for {
- my ($city, $stop) = @_;
+ my ( $city, $stop ) = @_;
my $cache = Cache::File->new(
cache_root => '/tmp/vrr-fake',
@@ -21,9 +21,11 @@ sub get_results_for {
my $results = $cache->thaw("${city} _ ${stop}");
if ( not $results ) {
- my $status
- = Travel::Status::DE::VRR->new(place => $city, name => $stop);
- $results = [ [$status->results], $status->errstr ];
+ my $status = Travel::Status::DE::VRR->new(
+ place => $city,
+ name => $stop
+ );
+ $results = [ [ $status->results ], $status->errstr ];
$cache->freeze( "${city} _ ${stop}", $results );
}
@@ -31,41 +33,41 @@ sub get_results_for {
}
sub handle_request {
- my $self = shift;
- my $city = $self->stash('city');
- my $stop = $self->stash('stop');
+ my $self = shift;
+ my $city = $self->stash('city');
+ my $stop = $self->stash('stop');
- $self->stash( title => 'vrr-fakedisplay' );
- $self->stash( version => $VERSION );
+ $self->stash( title => 'vrr-fakedisplay' );
+ $self->stash( version => $VERSION );
- $self->stash( params => $self->req->params->to_string);
+ $self->stash( params => $self->req->params->to_string );
$self->stash( height => 50 );
- $self->stash( width => 180);
+ $self->stash( width => 180 );
$self->render(
'main',
- city => $city,
- stop => $stop,
- version => $VERSION,
- title => "departures for ${city} ${stop}",
+ city => $city,
+ stop => $stop,
+ version => $VERSION,
+ title => "departures for ${city} ${stop}",
);
}
sub shorten_destination {
- my ($dest, $city) = @_;
+ my ( $dest, $city ) = @_;
$dest =~ s{ ^ $city \s }{}x;
- if (length($dest) > 20) {
- $dest =~ s{^Dortmund}{DO} or
- $dest =~ s{^Duisburg}{DU} or
- $dest =~ s{^Düsseldorf}{D} or
- $dest =~ s{^Essen}{E} or
- $dest =~ s{^Gelsenkirchen}{GE} or
- $dest =~ s{^Mülheim}{MH};
+ if ( length($dest) > 20 ) {
+ $dest =~ s{^Dortmund}{DO}
+ or $dest =~ s{^Duisburg}{DU}
+ or $dest =~ s{^Düsseldorf}{D}
+ or $dest =~ s{^Essen}{E}
+ or $dest =~ s{^Gelsenkirchen}{GE}
+ or $dest =~ s{^Mülheim}{MH};
}
- $dest = substr($dest, 0, 20);
+ $dest = substr( $dest, 0, 20 );
return $dest;
}
@@ -75,60 +77,63 @@ sub render_image {
my $city = $self->stash('city');
my $stop = $self->stash('stop');
- my $dt_now = DateTime->now(time_zone => 'Europe/Berlin');
+ my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' );
my $color = $self->param('color') || '255,208,0';
- my (@grep_line, @grep_platform);
-
+ my ( @grep_line, @grep_platform );
- my ($results, $errstr) = get_results_for($city, $stop);
+ my ( $results, $errstr ) = get_results_for( $city, $stop );
- my $png = App::VRR::Fakedisplay->new(width => 180, height => 50, color => [split(qr{,}, $color)]);
+ my $png = App::VRR::Fakedisplay->new(
+ width => 180,
+ height => 50,
+ color => [ split( qr{,}, $color ) ]
+ );
my $strp_simple = DateTime::Format::Strptime->new(
- pattern => '%H:%M',
+ pattern => '%H:%M',
time_zone => 'floating',
);
my $strp_full = DateTime::Format::Strptime->new(
- pattern => '%d.%m.%Y %H:%M',
+ pattern => '%d.%m.%Y %H:%M',
time_zone => 'floating',
);
-
- if ($self->param('line')) {
- @grep_line = split(qr{,}, $self->param('line'));
+ if ( $self->param('line') ) {
+ @grep_line = split( qr{,}, $self->param('line') );
}
- if ($self->param('platform')) {
- @grep_platform = split(qr{,}, $self->param('platform'));
+ if ( $self->param('platform') ) {
+ @grep_platform = split( qr{,}, $self->param('platform') );
}
$self->res->headers->content_type('image/png');
- for my $d (@{$results}) {
+ for my $d ( @{$results} ) {
- my $line = $d->line;
- my $platform = (split(qr{ }, $d->platform))[-1];
+ my $line = $d->line;
+ my $platform = ( split( qr{ }, $d->platform ) )[-1];
my $destination = $d->destination;
- my $time = $d->time;
+ my $time = $d->time;
my $etr;
my $dt_dep = $strp_full->parse_datetime($time)
- // $strp_simple->parse_datetime($time);
+ // $strp_simple->parse_datetime($time);
my $dt;
- if ((@grep_line and not ($line ~~ \@grep_line)) or
- (@grep_platform and not ($platform ~~ \@grep_platform))) {
+ if ( ( @grep_line and not( $line ~~ \@grep_line ) )
+ or ( @grep_platform and not( $platform ~~ \@grep_platform ) ) )
+ {
next;
}
- if ($time =~ m{ ^ \d\d? : \d\d $ }x) {
+ if ( $time =~ m{ ^ \d\d? : \d\d $ }x ) {
$dt = DateTime->new(
- year => $dt_now->year,
- month => $dt_now->month,
- day => $dt_now->day,
- hour => $dt_dep->hour,
- minute => $dt_dep->minute,
- second => $dt_dep->second,
+ year => $dt_now->year,
+ month => $dt_now->month,
+ day => $dt_now->day,
+ hour => $dt_dep->hour,
+ minute => $dt_dep->minute,
+ second => $dt_dep->second,
time_zone => 'Europe/Berlin',
);
}
@@ -138,42 +143,39 @@ sub render_image {
my $duration = $dt->subtract_datetime($dt_now);
- if ($duration->is_negative) {
+ if ( $duration->is_negative ) {
next;
}
- elsif ($duration->in_units('minutes') == 0) {
+ elsif ( $duration->in_units('minutes') == 0 ) {
$etr = 'sofort';
}
- elsif ($duration->in_units('hours') == 0) {
- $etr = sprintf(
- ' %2d',
- $duration->in_units('minutes'),
- );
+ elsif ( $duration->in_units('hours') == 0 ) {
+ $etr = sprintf( ' %2d', $duration->in_units('minutes'), );
}
else {
last;
}
- $destination = shorten_destination($destination, $city);
+ $destination = shorten_destination( $destination, $city );
- $png->draw_at(0, $line);
- $png->draw_at(25, $destination);
- $png->draw_at(144, $etr);
+ $png->draw_at( 0, $line );
+ $png->draw_at( 25, $destination );
+ $png->draw_at( 144, $etr );
- if ($etr ne 'sofort') {
- $png->draw_at(161, 'min');
+ if ( $etr ne 'sofort' ) {
+ $png->draw_at( 161, 'min' );
}
$png->new_line();
}
- $self->render(data => $png->png);
+ $self->render( data => $png->png );
}
get '/_redirect' => sub {
- my $self = shift;
- my $city = $self->param('city');
- my $stop = $self->param('stop');
+ my $self = shift;
+ my $city = $self->param('city');
+ my $stop = $self->param('stop');
$self->redirect_to("/${city}/${stop}");
};
diff --git a/lib/App/VRR/Fakedisplay.pm.PL b/lib/App/VRR/Fakedisplay.pm.PL
index a6d9e5f..8621d32 100644
--- a/lib/App/VRR/Fakedisplay.pm.PL
+++ b/lib/App/VRR/Fakedisplay.pm.PL
@@ -8,23 +8,24 @@ use GD;
my ($out_file) = @ARGV;
my $font = GD::Image->new('share/font.png');
-my $black = $font->colorClosest(0, 0, 0);
+my $black = $font->colorClosest( 0, 0, 0 );
-open(my $out_fh, '>:encoding(UTF-8)', $out_file) or die("open ${out_file}: $!");
+open( my $out_fh, '>:encoding(UTF-8)', $out_file )
+ or die("open ${out_file}: $!");
sub write_out {
- my ($off_x, $off_y, $char) = @_;
+ my ( $off_x, $off_y, $char ) = @_;
my $char_w = 0;
say $out_fh "'${char}' => {";
say $out_fh 'matrix => [';
- for my $pos_y ( $off_y .. ($off_y + 10)) {
+ for my $pos_y ( $off_y .. ( $off_y + 10 ) ) {
print $out_fh '[ ';
- for my $pos_x ( $off_x .. ($off_x + 10)) {
- if ($font->getPixel($pos_x, $pos_y) == $black) {
+ for my $pos_x ( $off_x .. ( $off_x + 10 ) ) {
+ if ( $font->getPixel( $pos_x, $pos_y ) == $black ) {
- if (($pos_x - $off_x) > $char_w) {
+ if ( ( $pos_x - $off_x ) > $char_w ) {
$char_w = $pos_x - $off_x;
}
@@ -40,7 +41,7 @@ sub write_out {
# spacing (one empty column)
$char_w++;
- if ($char eq q{ }) {
+ if ( $char eq q{ } ) {
$char_w = 5;
}
@@ -50,18 +51,17 @@ sub write_out {
}
sub parse_char_row {
- my ($off_y, @chars) = @_;
+ my ( $off_y, @chars ) = @_;
my $off_x = 0;
for my $char (@chars) {
- write_out($off_x, $off_y, $char);
+ write_out( $off_x, $off_y, $char );
$off_x += 10;
}
return;
}
-
print $out_fh <<'___CUT___';
package App::VRR::Fakedisplay;
@@ -99,11 +99,11 @@ sub new {
___CUT___
-parse_char_row( 0, 'A' .. 'Z');
-parse_char_row(10, 'a' .. 'z');
-parse_char_row(20, '0' .. '9');
-parse_char_row(30, q{:}, q{-}, q{.}, q{,}, q{/}, q{ });
-parse_char_row(40, qw(ä ö ü));
+parse_char_row( 0, 'A' .. 'Z' );
+parse_char_row( 10, 'a' .. 'z' );
+parse_char_row( 20, '0' .. '9' );
+parse_char_row( 30, q{:}, q{-}, q{.}, q{,}, q{/}, q{ } );
+parse_char_row( 40, qw(ä ö ü) );
print $out_fh <<'___CUT___';