Mail-Decency

 view release on metacpan or  search on metacpan

lib/Mail/Decency/Policy/GeoWeight.pm  view on Meta::CPAN

                - US
                - AU
            weight: 10
        -
            countries:
                - SE
                - DK
            weight: 5
        - { countries: [ 'XX' ], weight: -100 }
    
    weight_default: -5


=head1 CLASS ATTRIBUTES


=head2 weight_by_country : HashRef

Weight class, containging one or multiple country codes and a weighting (positive or negative)

=cut

has weight_by_country => ( is => 'rw', isa => 'HashRef[Int]', default => sub { {} } );

=head2 weight_default : Int

Weight for all values not specified via "weight_by_country"

=cut

has weight_default    => ( is => 'rw', isa => 'Int', default => 0 );

=head2 enable_stats : Bool

Enable statistics by country (either stats or weight or both should be enabled)

=cut

has enable_stats      => ( is => 'rw', isa => 'Bool', default => 0 );

=head2 enable_weight : Bool

Enable weighting (either stats or weight or both should be enabled)

=cut

has enable_weight     => ( is => 'rw', isa => 'Bool', default => 1 );

=head2 geo_ip : Geo::IP

=cut

has geo_ip            => ( is => 'ro', isa => 'Geo::IP', default => sub { Geo::IP->new( GEOIP_STANDARD ) } );

=head2 schema_definition

Database schema

=cut

has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub {
    {
        geo => {
            stats => {
                country   => [ varchar => 2 ],
                interval  => [ varchar => 25 ],
                counter   => 'integer',
                -unique   => [ 'country', 'interval' ]
            },
        }
    };
} );


=head1 METHODS

=head2 init

Checks weight classes

=cut

sub init {
    my ( $self ) = @_;
    
    # enable stats
    $self->enable_weight( 0 )
        if $self->config->{ disable_weight };
    
    if ( $self->enable_weight ) {
        
        # having weight classes -> check and setup
        if ( defined ( my $weight_classes_ref = $self->config->{ weight_classes } ) ) {
            my @classes = ref( $weight_classes_ref ) eq 'ARRAY'
                ? @{ $weight_classes_ref }
                : ( $weight_classes_ref )
            ;
            
            # check classes
            foreach my $class_ref( @classes ) {
                die "'$class_ref' is not a HashRef! weight_classes should be ArrayRef[HashRef]\n"
                    unless ref( $class_ref ) eq 'HASH';
                die "Require 'countries' as ArrayRef\n"
                    unless defined $class_ref->{ countries } && ref( $class_ref->{ countries } ) eq 'ARRAY';
                die "Require 'weight' for countries '". join( ", ", @{ $class_ref->{ countries } } ). "'\n"
                    unless defined $class_ref->{ weight } && $class_ref->{ weight } =~ /^\d+$/;
                
                foreach my $country( @{ $class_ref->{ countries } } ) {
                    die "Please use 2-char country code format like 'DE' or 'US'.. '$country' does not fit\n"
                        unless length( $country ) == 2 && $country =~ /^[a-z]{2}$/i;
                    $self->weight_by_country->{ uc( $country ) } = $class_ref->{ weight };
                }
            }
        }
        
        # having default weight ..
        $self->weight_default( $self->config->{ weight_default } )
            if defined $self->config->{ weight_default };
    }
    
    # enable stats



( run in 0.935 second using v1.01-cache-2.11-cpan-39bf76dae61 )