Skip to content

Instantly share code, notes, and snippets.

@Su-Shee
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save Su-Shee/7521f109663cbccd94b0 to your computer and use it in GitHub Desktop.

Select an option

Save Su-Shee/7521f109663cbccd94b0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
package Cats;
use strict;
use warnings;
use Web::Machine;
use Web::Machine::Util qw/bind_path/;
use Template::Jade qw/render_file/;
use Template;
use CSS::Tiny;
use parent 'Web::Machine::Resource';
opendir(my $dir, 'catpics/') or die "Can't open kittydir!";
my @catpics = readdir($dir);
sub allowed_methods { [qw[ GET POST ]] }
sub content_types_provided { [{ 'text/html' => 'to_html' }, { 'text/css' => 'to_css' }] }
sub extract_user_from_route {
return bind_path( '/:user', shift->request->path_info );
}
sub to_html {
my $self = shift;
my $user = $self->extract_user_from_route;
return render_file('allcats.jade') unless $user;
if ( length($user) < 4) {
return render_file('shortname.jade');
} else {
return render_file('userscat.jade');
}
}
sub to_css {
my $css = CSS::Tiny->new;
$css->read('machineexample.css');
return $css->write_string;
}
sub process_post {
my $self = shift;
print $self->request->param('username');
$self->response->header('Location' => '/cats');
return \301;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment