Acme-Plack-Middleware-Acme-Werewolf
view release on metacpan or search on metacpan
NAME
Acme::Plack::Middleware::Acme::Werewolf - Plack middleware of
Acme::Apache::Werewolf
SYNOPSIS
my $app = sub { ... };
builder {
enable "Acme::Werewolf", moonlength => 4;
$app;
};
DESCRIPTION
Plack middleware implementation of Acme::Apache::Werewolf which keeps
werewolves out of your web site during the full moon.
See to Plack::Middleware::Acme::Werewolf.
AUTHOR
lib/Acme/Plack/Middleware/Acme/Werewolf.pm view on Meta::CPAN
=pod
=head1 NAME
Acme::Plack::Middleware::Acme::Werewolf - Plack middleware of Acme::Apache::Werewolf
=head1 SYNOPSIS
my $app = sub { ... };
builder {
enable "Acme::Werewolf", moonlength => 4;
$app;
};
=head1 DESCRIPTION
Plack middleware implementation of L<Acme::Apache::Werewolf>
which keeps werewolves out of your web site during the full moon.
See to L<Plack::Middleware::Acme::Werewolf>.
lib/Plack/Middleware/Acme/Werewolf.pm view on Meta::CPAN
package Plack::Middleware::Acme::Werewolf;
use strict;
use warnings;
use Astro::MoonPhase ();
use parent qw( Plack::Middleware );
use Plack::Util::Accessor qw( moonlength message handler );
our $VERSION = '0.02';
sub prepare_app {
my ( $self ) = @_;
die "Set moonlength" unless $self->moonlength;
die "handler must be a code reference." if $self->handler && ref( $self->handler ) ne 'CODE';
}
sub call {
my ( $self, $env ) = @_;
my $moonage = ( Astro::MoonPhase::phase( time ) )[2];
#print "moonage:$moonage\n";
if ( abs( 14 - $moonage ) > $self->moonlength / 2 ) {
return $self->app->( $env );
}
else {
my $body = $self->message || 'Forbidden';
if ( $self->handler ) {
return $self->handler->( $self, $env, $moonage );
}
return [
403,
[
'Content-Type' => 'text/plain',
'Content-Length' => length $body,
],
[ $body ]
];
}
}
1;
__END__
=pod
=head1 NAME
Plack::Middleware::Acme::Werewolf - Plack middleware of Acme::Apache::Werewolf
=head1 SYNOPSIS
my $app = sub { ... };
builder {
enable "Acme::Werewolf", moonlength => 4;
$app;
};
=head1 DESCRIPTION
Plack middleware implementation of L<Acme::Apache::Werewolf>
which keeps werewolves out of your web site during the full moon.
=head1 CONFIGURATION
=over
=item moonlength
Required. The period considered as a full moon (in day).
If you set moonlength with 4, the moon age from 12 to 16 is full moon.
=item message
Optional. The forbidden message. Default is 'Forbidden'.
=item handler
Optional. The subroutine reference for resoneses takes the plack middleware itself, environment variable and moon age.
handler => sub {
t/02_simple.t view on Meta::CPAN
my $epoch = timegm( 0, 0, 12, 10, 12 - 1, 2012 - 1900 );
$epoch = timegm( 0, 0, 12, 28, 12 - 1, 2012 - 1900 ) if $fullmoon;
return $epoch;
}
};
use Plack::Middleware::Acme::Werewolf;
my $handler = builder {
enable "Plack::Middleware::Acme::Werewolf",
moonlength => 4,
;
sub {
[ 200, ['Content-Type' => 'text/plain'], ['ok'] ];
};
};
test_psgi
app => $handler,
client => sub {
my $cb = shift;
t/03_message.t view on Meta::CPAN
my $epoch = timegm( 0, 0, 12, 10, 12 - 1, 2012 - 1900 );
$epoch = timegm( 0, 0, 12, 28, 12 - 1, 2012 - 1900 ) if $fullmoon;
return $epoch;
}
};
use Plack::Middleware::Acme::Werewolf;
my $handler = builder {
enable "Plack::Middleware::Acme::Werewolf",
moonlength => 4,
message => 'Werewolf!',
;
sub {
[ 200, ['Content-Type' => 'text/plain'], ['ok'] ];
};
};
test_psgi
app => $handler,
client => sub {
t/04_handler.t view on Meta::CPAN
BEGIN {
*CORE::GLOBAL::time = sub {
return timegm( 0, 0, 12, 28, 12 - 1, 2012 - 1900 );
}
};
use Plack::Middleware::Acme::Werewolf;
my $handler = builder {
enable "Plack::Middleware::Acme::Werewolf",
moonlength => 4,
handler => sub {
my ( $c, $env, $moonage ) = @_;
like $moonage, qr/^14/;
isa_ok $c, 'Plack::Middleware::Acme::Werewolf';
is ref($env), 'HASH';
return [ 403, ['Content-Type' => 'text/plain'], ['Werewolf!'] ];
},
;
sub {
[ 200, ['Content-Type' => 'text/plain'], ['ok'] ];
( run in 0.416 second using v1.01-cache-2.11-cpan-65fba6d93b7 )