| 1 | # $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks/Rule/Fresh.pm 40501 2008-01-24T04:48:27.359921Z daisuke $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package GunghoX::FollowLinks::Rule::Fresh; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use Gungho::Util; | ||
| 10 | use GunghoX::FollowLinks::Rule qw(FOLLOW_ALLOW FOLLOW_DENY); | ||
| 11 | use base qw(GunghoX::FollowLinks::Rule); | ||
| 12 | |||
| 13 | __PACKAGE__->mk_accessors($_) for qw(storage); | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my $class = shift; | ||
| 18 | my %args = @_; | ||
| 19 | |||
| 20 | my $storage_config = delete $args{storage}; | ||
| 21 | my $storage_module = Gungho::Util::load_module( | ||
| 22 | $storage_config->{module} || 'Memory', | ||
| 23 | 'GunghoX::FollowLinks::Rule::Fresh' | ||
| 24 | ); | ||
| 25 | my $storage = $storage_module->new( %{ $storage_config->{config} || {} } ); | ||
| 26 | |||
| 27 | $class->next::method(storage => $storage); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub apply | ||
| 31 | { | ||
| 32 | my ($self, $c, $response, $url, $attrs) = @_; | ||
| 33 | |||
| 34 | my $storage = $self->storage; | ||
| 35 | if ($storage->get($url->as_string)) { | ||
| 36 | return FOLLOW_DENY; | ||
| 37 | } else { | ||
| 38 | $storage->put($url->as_string); | ||
| 39 | return FOLLOW_ALLOW; | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | 1; | ||
| 44 | |||
| 45 | __END__ |