| 1 | ############################################################################### | ||||||||||||||
| 2 | package CrawlerCommons::RobotDirective; | ||||||||||||||
| 3 | |||||||||||||||
| 4 | # MODULE IMPORTS | ||||||||||||||
| 5 | ######################################## | ||||||||||||||
| 6 | # Pragmas | ||||||||||||||
| 7 | #------------------# | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # CPAN/Core | ||||||||||||||
| 10 | #------------------# | ||||||||||||||
| 11 | 2 | ▶ | use Const::Fast; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | # Moose Setup | ||||||||||||||
| 14 | #------------------# | ||||||||||||||
| 15 | |||||||||||||||
| 16 | # Moose Pragmas | ||||||||||||||
| 17 | #------------------# | ||||||||||||||
| 18 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 2 | ▶ | use MooseX::ClassAttribute; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 2 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | # Custom Modules | ||||||||||||||
| 23 | #------------------# | ||||||||||||||
| 24 | |||||||||||||||
| 25 | |||||||||||||||
| 26 | # VARIABLES/CONSTANTS | ||||||||||||||
| 27 | ######################################## | ||||||||||||||
| 28 | # Constants | ||||||||||||||
| 29 | #------------------# | ||||||||||||||
| 30 | 2 | ▶ | const my $DEBUG => $ENV{DEBUG} // 0; | ||||||||||||
|
Condition50%1/2
$ENV{"DEBUG"} // 0
MC/DC0%0/1
$ENV{"DEBUG"} // 0
$ENV{"DEBUG"}
Truth table50%1/2
A
A= $ENV{"DEBUG"}
| |||||||||||||||
| 31 | 2 | ▶ | const my $TEST => $ENV{TEST} // 1; | ||||||||||||
|
Condition50%1/2
$ENV{"TEST"} // 1
MC/DC0%0/1
$ENV{"TEST"} // 1
$ENV{"TEST"}
Truth table50%1/2
A
A= $ENV{"TEST"}
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | 2 | const my $CRAWLDELAY_MISSPELLINGS=>["crawl delay"]; | |||||||||||||
| 34 | 2 | const my $DISALLOW_MISSPELLINGS => [qw(desallow dissalow dssalow dsallow)]; | |||||||||||||
| 35 | 2 | const my $DIRECTIVES_LIST => [ | |||||||||||||
| 36 | 'USER_AGENT', 'DISALLOW', | ||||||||||||||
| 37 | 'ALLOW', 'CRAWL_DELAY', 'SITEMAP', | ||||||||||||||
| 38 | 'HOST', | ||||||||||||||
| 39 | 'NO_INDEX', | ||||||||||||||
| 40 | # Extended standard | ||||||||||||||
| 41 | 'REQUEST_RATE', 'VISIT_TIME', 'ROBOT_VERSION', 'COMMENT', | ||||||||||||||
| 42 | # Treated as sitemap directive | ||||||||||||||
| 43 | 'HTTP']; | ||||||||||||||
| 44 | 2 | const my $PREFIX_DIRECTIVES => [qw(ACAP_)]; | |||||||||||||
| 45 | 2 | const my $SPECIAL_DIRECTIVES => [qw(UNKNOWN MISSING)]; | |||||||||||||
| 46 | 2 | const my $USERAGENT_MISSPELLINGS=> [qw(useragent useg-agent ser-agent)]; | |||||||||||||
| 47 | const my $VALUES_ENUM_LIST => | ||||||||||||||
| 48 | 30 | [ map {my $v = lc( $_ ); $v =~ s/\_$//; $v;} | |||||||||||||
| 49 | 2 | map { @{ $_ } } | |||||||||||||
| 50 | ( $DIRECTIVES_LIST, $PREFIX_DIRECTIVES, $SPECIAL_DIRECTIVES ) ]; | ||||||||||||||
| 51 | |||||||||||||||
| 52 | # Variables | ||||||||||||||
| 53 | #------------------# | ||||||||||||||
| 54 | 2 | our $VERSION = '0.03'; | |||||||||||||
| 55 | |||||||||||||||
| 56 | |||||||||||||||
| 57 | # MOOSE ATTRIBUTES | ||||||||||||||
| 58 | ######################################## | ||||||||||||||
| 59 | # Class | ||||||||||||||
| 60 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 61 | 2 | class_has 'directive_map' => ( | |||||||||||||
| 62 | builder => 'load_directives_map', | ||||||||||||||
| 63 | handles => { | ||||||||||||||
| 64 | directive_exists => 'exists', | ||||||||||||||
| 65 | get_directive => 'get', | ||||||||||||||
| 66 | }, | ||||||||||||||
| 67 | is => 'ro', | ||||||||||||||
| 68 | isa => 'HashRef', | ||||||||||||||
| 69 | lazy => 1, | ||||||||||||||
| 70 | traits => ['Hash'], | ||||||||||||||
| 71 | ); | ||||||||||||||
| 72 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 73 | |||||||||||||||
| 74 | # Instance | ||||||||||||||
| 75 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 76 | 2 | has 'is_prefix' => ( | |||||||||||||
| 77 | default => 0, | ||||||||||||||
| 78 | is => 'ro', | ||||||||||||||
| 79 | isa => 'Bool', | ||||||||||||||
| 80 | ); | ||||||||||||||
| 81 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 82 | 2 | has 'is_special' => ( | |||||||||||||
| 83 | default => 0, | ||||||||||||||
| 84 | is => 'ro', | ||||||||||||||
| 85 | isa => 'Bool', | ||||||||||||||
| 86 | ); | ||||||||||||||
| 87 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 88 | 2 | has 'value' => ( | |||||||||||||
| 89 | enum => $VALUES_ENUM_LIST, | ||||||||||||||
| 90 | handles => 1, | ||||||||||||||
| 91 | is => 'ro', | ||||||||||||||
| 92 | required => 1, | ||||||||||||||
| 93 | traits => ['Enumeration'], | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 96 | |||||||||||||||
| 97 | # METHODS | ||||||||||||||
| 98 | ######################################## | ||||||||||||||
| 99 | # Construction | ||||||||||||||
| 100 | #------------------# | ||||||||||||||
| 101 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 102 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # Class Methods | ||||||||||||||
| 105 | #------------------# | ||||||||||||||
| 106 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 107 | 1 | ▶ | sub load_directives_map { | ||||||||||||
|
Subroutine
load_directives_map: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 108 | 1 | my $pkg = shift; | |||||||||||||
| 109 | |||||||||||||||
| 110 | 1 | my $map = { | |||||||||||||
| 111 | $pkg->_map_directive_list( $DIRECTIVES_LIST, 0, 0), | ||||||||||||||
| 112 | $pkg->_map_directive_list( $PREFIX_DIRECTIVES, 1, 0), | ||||||||||||||
| 113 | $pkg->_map_directive_list( $SPECIAL_DIRECTIVES, 0, 1), | ||||||||||||||
| 114 | }; | ||||||||||||||
| 115 | |||||||||||||||
| 116 | # setup common user_agent, disallow and crawl_delya directive misspellings | ||||||||||||||
| 117 | 1 | $map->{$_} = $map->{'crawl-delay'} for @{ $CRAWLDELAY_MISSPELLINGS }; | |||||||||||||
| 118 | 1 | $map->{$_} = $map->{disallow} for @{ $DISALLOW_MISSPELLINGS }; | |||||||||||||
| 119 | 1 | $map->{$_} = $map->{'user-agent'} for @{ $USERAGENT_MISSPELLINGS }; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 1 | return $map; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 124 | |||||||||||||||
| 125 | # Instance Methods | ||||||||||||||
| 126 | #------------------# | ||||||||||||||
| 127 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 128 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 129 | |||||||||||||||
| 130 | # Private Methods | ||||||||||||||
| 131 | #------------------# | ||||||||||||||
| 132 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 133 | 3 | ▶ | sub _map_directive_list { | ||||||||||||
|
Subroutine
_map_directive_list: called CC=4 SCAR=13.9
| |||||||||||||||
| 134 | 3 | my ($pkg, $directives_list, $is_prefix, $is_special) = @_; | |||||||||||||
| 135 | 3 | my %map = (); | |||||||||||||
| 136 | |||||||||||||||
| 137 | 3 | for my $directive_str ( @{ $directives_list } ) { | |||||||||||||
| 138 | 15 | (my $prefix = lc($directive_str)) =~ s!_!\-!g; | |||||||||||||
| 139 | 15 | (my $value = lc( $directive_str )) =~ s!_$!!g; | |||||||||||||
| 140 | 15 | ▶ | $map{$prefix} = $is_prefix ? | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 141 | $pkg->new(value => $value, is_prefix => 1) : | ||||||||||||||
| 142 | ($is_special ? | ||||||||||||||
| 143 | $pkg->new(value => $value, is_special => 1) : | ||||||||||||||
| 144 | $pkg->new(value => $value) ); | ||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 3 | return %map; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | #-----------------------------------------------------------------------------# | ||||||||||||||
| 150 | |||||||||||||||
| 151 | ############################################################################### | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 154 | |||||||||||||||
| 155 | ############################################################################### | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 2 | 1; | |||||||||||||
| 158 | |||||||||||||||
| 159 | __END__ | ||||||||||||||