diff --git a/Makefile.PL b/Makefile.PL index 3378c31..03e44bf 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -29,6 +29,9 @@ WriteMakefile( 'https://github.com/peczenyj/GDPR-IAB-TCFv2/issues' }, }, + recommends => { + 'JSON' => 0, + }, x_authority => 'cpan:PACMAN', } ) diff --git a/lib/GDPR/IAB/TCFv2.pm b/lib/GDPR/IAB/TCFv2.pm index 4dbd20b..f081137 100644 --- a/lib/GDPR/IAB/TCFv2.pm +++ b/lib/GDPR/IAB/TCFv2.pm @@ -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") ) @@ -132,6 +133,12 @@ sub Parse { return $self; } +sub tc_string { + my $self = shift; + + return $self->{tc_string}; +} + sub version { my $self = shift; @@ -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 @@ -675,6 +683,12 @@ See L for more details. =head1 METHODS +=head2 tc_string + +Returns the original consent string. + +The consent object L will call this method on string interpolations. + =head2 version Version number of the encoding format. The value is 2 for this format. diff --git a/t/01-parse.t b/t/01-parse.t index 31b38c0..74407db 100644 --- a/t/01-parse.t +++ b/t/01-parse.t @@ -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, @@ -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,