Permalink
Browse files

- Added explicit strict and warnings (because "use v5.012" is now gone).

- Skip requests other than "/".
- Replaced sprintf with variables because of faulty behaviour.
  • Loading branch information...
1 parent 039c101 commit 752a448e8ba0e43c24d3ec9583494b7a35072dfa @soulchild committed Mar 12, 2013
Showing with 77 additions and 61 deletions.
  1. +77 −61 lib/App/DevReactions.pm
View
@@ -1,6 +1,9 @@
package App::DevReactions;
use utf8;
+use strict;
+use warnings;
+
use URI;
use Encode;
use XML::Feed;
@@ -42,7 +45,7 @@ my $feeds = [
Love the animated GIFs from sites like devopsreactions and thecodinglove?
-This app lets you view them randomly in full screen mode in your browser.
+This app lets you view them in full screen mode in your browser.
=cut
@@ -82,69 +85,82 @@ sub app {
return sub {
my $env = shift;
+ # 404 for all requests other than /
+ if( $env->{ REQUEST_URI } !~ /^\/$/ ) {
+ return [ 404, [], [] ];
+ }
+
# Reload feeds every 100 requests
$reactions = load_reactions() if $requests++ % 100 == 0;
- # Grab a random reaction
- my $rand = int( rand( scalar @{ $reactions } ) );
- my $reaction = $reactions->[ $rand ];
-
- my $body = sprintf( <<EOF, $reaction->{ 'url' }, $rand, scalar @$reactions, $reaction->{ 'title' }, $feeds->[ $reaction->{ 'feed_id' } ]->{ 'name' }, $reaction->{ 'image' } );
- <!doctype html>
- <html>
- <head>
- <title>DevReactions</title>
- <style type="text/css">
- body {
- margin:auto;
- background:#000;
- font-family:Helvetica, Arial, sans-serif;
- color:white;
- font-smoothing: antialiased;
- -webkit-font-smoothing: antialiased;
- text-rendering: optimizeLegibility;
- overflow:hidden;
- }
- header {
- position:absolute;
- margin:0;
- background:rgba(0,0,0,.6);
- padding:12px 16px;
- top:0;
- left:0;
- }
- h1, h2 {
- margin:0;
- }
- h2 {
- font-weight:normal;
- }
- article img {
- border:none;
- height:100%;
- min-height:100%;
- max-width:100%;
- }
- a, a:link, a:visited, a:hover {
- color:white;
- text-decoration:none;
- }
- </style>
- <meta http-equiv="refresh" content="10; URL=/">
- </head>
- <body>
- <header>
- <a href="%s">
- <h1>%d/%d: %s</h1>
- <h2>from %s</h2>
- </a>
- </header>
- <article>
- <img src="%s">
- </article>
- <a href="https://github.com/soulchild"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
- </body>
- </html>
+ # Grab next random reaction
+ my $reaction_total = scalar @$reactions;
+ my $no = int( rand( $reaction_total ) );
+ my $reaction = $reactions->[ $no ];
+ my $reaction_url = $reaction->{ 'url' };
+ my $reaction_no = $no + 1;
+ my $reaction_title = $reaction->{ 'title' };
+ my $reaction_feed = $feeds->[ $reaction->{ 'feed_id' } ]->{ 'name' };
+ my $reaction_image = $reaction->{ 'image' };
+
+ my $body = <<EOF;
+<!doctype html>
+<html>
+ <head>
+ <title>DevReactions</title>
+ <style type="text/css">
+ body {
+ margin:auto;
+ background:#000;
+ font-family:Helvetica, Arial, sans-serif;
+ color:white;
+ font-smoothing: antialiased;
+ -webkit-font-smoothing: antialiased;
+ text-rendering: optimizeLegibility;
+ overflow:hidden;
+ }
+ header {
+ position:absolute;
+ margin:0;
+ background:rgba(0,0,0,.6);
+ padding:12px 16px;
+ top:0;
+ left:0;
+ }
+ h1, h2 {
+ margin:0;
+ }
+ h2 {
+ font-weight:normal;
+ }
+ article img {
+ border:none;
+ height:100%;
+ min-height:100%;
+ max-width:100%;
+ }
+ a, a:link, a:visited, a:hover {
+ color:white;
+ text-decoration:none;
+ }
+ </style>
+ <meta http-equiv="refresh" content="15; URL=/">
+ <meta name="description" content="Watch the animated GIFs from DevOpReactions/SecurityReactions/TheCodingLove in full-screen.">
+ <meta name="author" content="Tobias Kremer/soulchild">
+ </head>
+ <body>
+ <header>
+ <a href="$reaction_url">
+ <h1>$reaction_no/$reaction_total: $reaction_title</h1>
+ <h2>from $reaction_feed</h2>
+ </a>
+ </header>
+ <article>
+ <img src="$reaction_image">
+ </article>
+ <a href="https://github.com/soulchild"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
+ </body>
+</html>
EOF
return [ 200, [], [ encode_utf8( $body ) ] ];

0 comments on commit 752a448

Please sign in to comment.