Skip to content

Commit

Permalink
add a stringify method to the Policy class (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigio authored Dec 10, 2024
1 parent ad52c4e commit 73aca2f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Mail/DMARC/Policy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ sub parse {
return bless \%policy, ref $self; # inherited defaults + overrides
}

sub stringify {
my $self = shift;

my %dmarc_record = %{$self};
delete $dmarc_record{domain};

my $dmarc_txt = 'v=' . (delete $dmarc_record{v}); # "v" tag must be first
foreach my $key ( keys %dmarc_record ) {
$dmarc_txt .= "; $key=$dmarc_record{$key}";
}
return $dmarc_txt;
}

sub apply_defaults {
my $self = shift;

Expand Down Expand Up @@ -269,6 +282,13 @@ via DNS.
$pol->parse( 'v=DMARC1; p=none; rua=mailto:dmarc@example.com' );
$pol->parse( 'v=DMARC1' ); # external reporting record
=head2 stringify
Returns the textual representation of the DMARC record.
my $pol = Mail::DMARC::Policy->new('v=DMARC1; p=none;');
print $pol->stringify;
=head1 Record Tags
=head2 Tag Overview
Expand Down
6 changes: 6 additions & 0 deletions t/01.Policy.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ stderr_is { test_parse() } $expected_parse_warning, 'STDERR yields parse warning
test_setter_values();
test_apply_defaults();
test_is_valid();
test_stringify();
handles_common_record_errors();

done_testing();
Expand Down Expand Up @@ -259,6 +260,11 @@ sub test_is_valid {
ok( $pol && $pol->is_valid, "is_valid, pos, implicit p=none w/rua" );
}

sub test_stringify {
$pol = Mail::DMARC::Policy->new( 'v=DMARC1; p=reject' );
cmp_ok($pol->stringify, 'eq', 'v=DMARC1; p=reject');
}

sub handles_common_record_errors {

foreach my $d (<DATA>) {
Expand Down

0 comments on commit 73aca2f

Please sign in to comment.