Skip to content

Commit

Permalink
add TO_JSON and tc_string method
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Dec 9, 2023
1 parent 9038f89 commit 98994ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ WriteMakefile(
'https://github.com/peczenyj/GDPR-IAB-TCFv2/issues'
},
},
recommends => {
'JSON' => 0,
},
x_authority => 'cpan:PACMAN',
}
)
Expand Down
18 changes: 16 additions & 2 deletions lib/GDPR/IAB/TCFv2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use constant {
VENDOR_CONSENT_OFFSET => 230,
};

use overload q<""> => \&tc_string;

INIT {
if ( my $native_decode_base64url = MIME::Base64->can("decode_base64url") )
Expand Down Expand Up @@ -132,6 +133,12 @@ sub Parse {
return $self;
}

sub tc_string {
my $self = shift;

return $self->{tc_string};
}

sub version {
my $self = shift;

Expand Down Expand Up @@ -302,8 +309,9 @@ sub TO_JSON {
my $use_epoch = !!$self->{options}->{use_epoch};

return {
version => $self->version,
created => $use_epoch
tc_string => $self->tc_string,
version => $self->version,
created => $use_epoch
? $self->created
: _format_date_iso8601( $self->created ),
last_updated => $use_epoch
Expand Down Expand Up @@ -675,6 +683,12 @@ See L<TO_JSON> for more details.
=head1 METHODS
=head2 tc_string
Returns the original consent string.
The consent object L<GDPR::IAB::TCFv2> will call this method on string interpolations.
=head2 version
Version number of the encoding format. The value is 2 for this format.
Expand Down
22 changes: 16 additions & 6 deletions t/01-parse.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ use GDPR::IAB::TCFv2;
subtest "valid tcf v2 consent string using bitfield" => sub {
my $consent;

my $tc_string =
'CLcVDxRMWfGmWAVAHCENAXCkAKDAADnAABRgA5mdfCKZuYJez-NQm0TBMYA4oCAAGQYIAAAAAAEAIAEgAA.argAC0gAAAAAAAAAAAA';
lives_ok {
$consent = GDPR::IAB::TCFv2->Parse(
'CLcVDxRMWfGmWAVAHCENAXCkAKDAADnAABRgA5mdfCKZuYJez-NQm0TBMYA4oCAAGQYIAAAAAAEAIAEgAA.argAC0gAAAAAAAAAAAA'
);
$consent = GDPR::IAB::TCFv2->Parse($tc_string);
}
'should not throw exception';

isa_ok $consent, 'GDPR::IAB::TCFv2', 'gdpr iab tcf v2 consent';

is $consent->tc_string, $tc_string, 'should return the original tc string';

is "${consent}", $tc_string,
'should return the original tc string in string context';

is $consent->version, 2, 'should return version 2';

is $consent->created, 1228644257,
Expand Down Expand Up @@ -152,15 +157,20 @@ subtest "valid tcf v2 consent string using bitfield" => sub {
subtest "valid tcf v2 consent string using range" => sub {
my $consent;

my $tc_string =
'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA';
lives_ok {
$consent = GDPR::IAB::TCFv2->Parse(
'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA'
);
$consent = GDPR::IAB::TCFv2->Parse($tc_string);
}
'should not throw exception';

isa_ok $consent, 'GDPR::IAB::TCFv2', 'gdpr iab tcf v2 consent';

is $consent->tc_string, $tc_string, 'should return the original tc string';

is "${consent}", $tc_string,
'should return the original tc string in string context';

is $consent->version, 2, 'should return version 2';

is $consent->created, 1587946020,
Expand Down

0 comments on commit 98994ce

Please sign in to comment.