blib/lib/GDPR/IAB/TCFv2/Publisher.pm

filter: Statementstmt 100% 51 / 51 Branchbran 59.0% 13 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 66.6% 4 / 6 totaltotal 87.7% 79 / 90 CC 12 SCAR 12.6
CC
12
Cov
88%
Parse
7.2
TO_JSON
3.0
has_restrictions
2.1
CRAP 3.5
« blib/lib/GDPR/IAB/TCFv2/PublisherTC.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Validator/Reason.pm »
1 package GDPR::IAB::TCFv2::Publisher 0.520;
2 72 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 72 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 72 ▶use GDPR::IAB::TCFv2::PublisherRestrictions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 72 ▶use GDPR::IAB::TCFv2::PublisherTC;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub Parse {
11 11417 ▶ my ($klass, %args) = @_;
Subroutine Parse: called CC=7 SCAR=19.8
Pod undocumented
12  
13 11417 ▶ croak "missing 'core_data'" unless defined $args{core_data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"core_data"} 0 11417 11417
14 11417 ▶ croak "missing 'core_data_size'" unless defined $args{core_data_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"core_data_size"} 0 11417 11417
15 11417 ▶ croak "missing 'offset'" unless defined $args{offset};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"offset"} 0 11417 11417
16  
17 11417 ▶ croak "missing 'options'" unless defined $args{options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"} 0 11417 11417
18 11417 ▶ croak "missing 'options.json'" unless defined $args{options}->{json};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"options"}{"json"} 0 11417 11417
19  
20 11417 my $core_data = $args{core_data};
21 11417 my $core_data_size = $args{core_data_size}; # size in bytes
22 11417 my $offset = $args{offset};
23  
24 my $restrictions = GDPR::IAB::TCFv2::PublisherRestrictions->Parse(
25 data => $core_data,
26 data_size => $core_data_size << 3, # convert to bits
27 offset => $offset,
28 options => $args{options},
29 11417 );
30  
31 11417 my $self = {restrictions => $restrictions, publisher_tc => undef,};
32  
33 11417 ▶ if (defined $args{publisher_tc_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"publisher_tc_data"}) 9679 1738 11417
34 9679 my $publisher_tc_data = $args{publisher_tc_data};
35 9679 my $publisher_tc_data_bits = length($publisher_tc_data) << 3;
36  
37 my $publisher_tc = GDPR::IAB::TCFv2::PublisherTC->Parse(
38 data => $publisher_tc_data,
39 data_size => $publisher_tc_data_bits,
40 options => $args{options},
41 9679 );
42  
43 9679 $self->{publisher_tc} = $publisher_tc;
44 }
45  
46 11417 bless $self, $klass;
47  
48 11417 return $self;
49 }
50  
51 sub check_restriction {
52 21522 ▶ my ($self, $purpose_id, $restriction_type, $vendor_id) = @_;
Subroutine check_restriction: called CC=2 SCAR=7.1
Pod documented
53  
54 21522 ▶ return 0 unless defined $self->{restrictions};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"restrictions"} 0 21522 21522
55  
56 21522 return $self->{restrictions}->check_restriction($purpose_id, $restriction_type, $vendor_id);
57 }
58  
59 sub restrictions {
60 8 ▶ my ($self, $vendor_id) = @_;
Subroutine restrictions: called CC=2 SCAR=7.1
Pod documented
61  
62 8 ▶ return {} unless defined $self->{restrictions};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"restrictions"} 0 8 8
63  
64 8 return $self->{restrictions}->restrictions($vendor_id);
65 }
66  
67 sub has_restrictions {
68 2 ▶ my $self = shift;
Subroutine has_restrictions: called CC=2 SCAR=7.2
Pod undocumented
69  
70 2 ▶ return defined $self->{restrictions} ? $self->{restrictions}->has_restrictions : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'restrictions'} ? : 2 0 2
71 }
72  
73 sub publisher_tc {
74 4 ▶ my ($self) = @_;
Subroutine publisher_tc: called CC=1 SCAR=0.0
Pod documented
75  
76 4 return $self->{publisher_tc};
77 }
78  
79 sub TO_JSON {
80 1064 ▶ my ($self, $filter_id) = @_;
Subroutine TO_JSON: called CC=3 SCAR=11.0
Pod documented
81  
82 1064 my %tags;
83  
84 1064 ▶ if (defined $self->{restrictions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"restrictions"}) 1064 0 1064
85 1064 $tags{restrictions} = $self->{restrictions}->TO_JSON($filter_id);
86 }
87  
88 1064 ▶ if (defined $self->{publisher_tc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"publisher_tc"}) 882 182 1064
89 882 %tags = (%tags, %{$self->{publisher_tc}->TO_JSON});
90 }
91  
92 1064 return \%tags;
93 }
94  
95  
96 72 1;
97 __END__
98  
99 =head1 NAME
100  
101 GDPR::IAB::TCFv2::Publisher - TCF v2.3 publisher section parser
102  
103 Combines the creation of L<GDPR::IAB::TCFv2::PublisherRestrictions> and L<GDPR::IAB::TCFv2::PublisherTC> based on the data available.
104  
105 =head1 SYNOPSIS
106  
107 use GDPR::IAB::TCFv2::Publisher;
108 my $core_data = '...'; # raw binary
109 my $core_data_size = length($core_data);
110 my $publisher_tc_data = '...';
111  
112 my $publisher = GDPR::IAB::TCFv2::Publisher->Parse(
113 core_data => $core_data,
114 core_data_size => $core_data_size,
115 offset => 284,
116 publisher_tc_data => $publisher_tc_data, # optional
117 options => { json => {} },
118 );
119  
120 if ($publisher->check_restriction(1, 0, 284)) {
121 # ...
122 }
123  
124 =head1 CONSTRUCTOR
125  
126 Constructor C<Parse> receives a hash of 5 parameters:
127  
128 =over
129  
130 =item *
131  
132 Key C<core_data> is the binary core data
133  
134 =item *
135  
136 Key C<core_data_size> is the original binary core data size
137  
138 =item *
139  
140 Key C<offset> is the bit offset where the publisher section starts
141  
142 =item *
143  
144 Key C<publisher_tc_data> is the binary publisher data. Optional.
145  
146 =item *
147  
148 Key C<options> is the L<GDPR::IAB::TCFv2> options (includes the C<json> field to modify the L</TO_JSON> method output.
149  
150 =back
151  
152 =head1 METHODS
153  
154 =head2 check_restriction
155  
156 Return true for a given combination of purpose id, restriction type and vendor_id
157  
158 my $purpose_id = 1;
159 my $restriction_type = 0;
160 my $vendor_id = 284;
161 $ok = $publisher->check_restriction($purpose_id, $restriction_type, $vendor_id);
162  
163 =head2 restrictions
164  
165 Return a hashref of purpose => { restriction type => bool } for a given vendor id.
166  
167 Example, by parsing the consent C<COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA> we can generate this.
168  
169 my $restrictions = $publisher->restrictions(32);
170 # returns { 7 => { 1 => 1 } }
171  
172 =head2 publisher_tc
173  
174 If the consent string has a C<Publisher TC> section, we will decode this section as an instance of L<GDPR::IAB::TCFv2::PublisherTC>.
175  
176 Will return undefined if there is no C<Publisher TC> section.
177  
178 =head2 TO_JSON
179  
180 Returns a hashref with the following format:
181  
182 {
183 consents => ...,
184 legitimate_interests => ...,
185 custom_purposes => {
186 consents => ...,
187 legitimate_interests => ...,
188 },
189 restrictions => {
190 '[purpose id]' => {
191 # 0 - Not Allowed
192 # 1 - Require Consent
193 # 2 - Require Legitimate Interest
194 '[vendor_id id]' => 1,
195 },
196 }
197 }
198  
199 Example, by parsing the consent C<COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA.argAC0gAAAAAAAAAAAA> we can generate this compact hashref.
200  
201 {
202 "consents" : [
203 2,
204 4,
205 6,
206 8,
207 9,
208 10
209 ],
210 "legitimate_interests" : [
211 2,
212 4,
213 5,
214 7,
215 10
216 ],
217 "custom_purposes" : {
218 "consents" : [],
219 "legitimate_interests" : []
220 },
221 "restrictions" : {
222 "7" : {
223 "32" : 1
224 }
225 }
226 }
227  
228 However by parsing the consent C<COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA> without the C<Publisher TC>
229 section will omit all fields except C<restrictions>:
230  
231 {
232 "restrictions" : {
233 "7" : {
234 "32" : 1
235 }
236 }
237 }
« blib/lib/GDPR/IAB/TCFv2/PublisherTC.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Validator/Reason.pm »