summaryrefslogtreecommitdiffstats
path: root/lib/Narwhal/Component/Redirect.pm
blob: 58a84780ab5b78e0dfe792936767a05d92aebf88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Narwhal::Component::Redirect;
use Moose;

sub permanent {
    my $self = shift;
    my ($req) = @_;
    my $res = $req->new_response(301);
    $res->location($self->_get_location($req));
    return $res;
}

sub temporary {
    my $self = shift;
    my ($req) = @_;
    my $res = $req->new_response(302);
    $res->location($self->_get_location($req));
    return $res;
}

sub _get_location {
    my $self = shift;
    my ($req) = @_;
    my $to = $req->env->{'plack.router.match'}->route->defaults->{to};
    die "must supply a location to redirect to" unless $to;
    return $to;
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;