File Coverage

blib/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm
Criterion Covered Total %
statement 69 69 100.0
branch 11 18 61.1
condition 4 4 100.0
subroutine 9 9 100.0
pod 3 4 75.0
total 96 104 92.3


line stmt bran cond sub pod time code
1             package GDPR::IAB::TCFv2::PublisherRestrictions;
2 5     5   33 use strict;
  5         12  
  5         224  
3 5     5   28 use warnings;
  5         11  
  5         295  
4              
5 5     5   29 use Carp qw;
  5         11  
  5         305  
6              
7 5         457 use GDPR::IAB::TCFv2::BitUtils qw<
8             get_uint2
9             get_uint6
10             get_uint12
11 5     5   27 >;
  5         23  
12              
13 5     5   33 use constant ASSUMED_MAX_VENDOR_ID => 0x7FFF; # 32767 or (1 << 15) -1
  5         9  
  5         5138  
14              
15              
16             sub Parse {
17 27     27 0 104 my ( $klass, %args ) = @_;
18              
19 27 50       81 croak "missing 'data'" unless defined $args{data};
20 27 50       82 croak "missing 'data_size'" unless defined $args{data_size};
21              
22 27 50       70 croak "missing 'options'" unless defined $args{options};
23 27 50       69 croak "missing 'options.json'" unless defined $args{options}->{json};
24              
25 27         55 my $data = $args{data};
26 27         77 my $data_size = $args{data_size};
27 27         42 my $offset = 0;
28 27         49 my $max_id = ASSUMED_MAX_VENDOR_ID;
29 27         102 my $options = $args{options};
30              
31 27         159 my ( $num_restrictions, $next_offset ) = get_uint12( $data, $offset );
32              
33 27         51 my %restrictions;
34              
35 27         100 for ( 1 .. $num_restrictions ) {
36 24         49 my ( $purpose_id, $restriction_type, $vendor_restrictions );
37              
38 24         127 ( $purpose_id, $next_offset ) = get_uint6( $data, $next_offset );
39              
40 24         133 ( $restriction_type, $next_offset ) = get_uint2( $data, $next_offset );
41              
42 24         181 ( $vendor_restrictions, $next_offset ) =
43             GDPR::IAB::TCFv2::RangeSection->Parse(
44             data => $data,
45             data_size => $data_size,
46             offset => $next_offset,
47             max_id => ASSUMED_MAX_VENDOR_ID,
48             options => $options,
49             );
50              
51 24   100     163 $restrictions{$purpose_id} ||= {};
52              
53 24         82 $restrictions{$purpose_id}->{$restriction_type} = $vendor_restrictions;
54             }
55              
56 27         98 my $self = {
57             restrictions => \%restrictions,
58             };
59              
60 27         59 bless $self, $klass;
61              
62 27         116 return $self;
63             }
64              
65             sub restrictions {
66 8     8 1 22 my ( $self, $vendor_id ) = @_;
67              
68 8         22 my %restrictions;
69              
70 8         18 foreach my $purpose_id ( keys %{ $self->{restrictions} } ) {
  8         42  
71 19         36 foreach my $restriction_type (
72 19         68 keys %{ $self->{restrictions}->{$purpose_id} } )
73             {
74 16 100       60 if ( $self->{restrictions}->{$purpose_id}->{$restriction_type}
75             ->contains($vendor_id) )
76             {
77 8   100     38 $restrictions{$purpose_id} ||= {};
78 8         25 $restrictions{$purpose_id}->{$restriction_type} = 1;
79             }
80             }
81             }
82              
83 8         41 return \%restrictions;
84             }
85              
86             sub check_restriction {
87 19     19 1 38 my $self = shift;
88              
89 19         44 my $nargs = scalar(@_);
90              
91 19 50       64 croak "missing arguments: purpose id, restriction type and vendor id"
92             if $nargs == 0;
93 19 50       56 croak "missing arguments: restriction type and vendor id" if $nargs == 1;
94 19 50       100 croak "missing argument: vendor id" if $nargs == 2;
95              
96 19         55 my ( $purpose_id, $restriction_type, $vendor_id ) = @_;
97              
98             return 0
99 19 100       180 unless exists $self->{restrictions}->{$purpose_id}->{$restriction_type};
100              
101 11         60 return $self->{restrictions}->{$purpose_id}->{$restriction_type}
102             ->contains($vendor_id);
103             }
104              
105             sub TO_JSON {
106 18     18 1 26 my $self = shift;
107              
108 18         30 my %publisher_restrictions;
109              
110 18         32 foreach my $purpose_id ( keys %{ $self->{restrictions} } ) {
  18         72  
111 6         15 my $restriction_map = $self->{restrictions}->{$purpose_id};
112              
113 6         9 my %purpose_restrictions;
114              
115 6         11 foreach my $restriction_type ( keys %{$restriction_map} ) {
  6         13  
116 6         28 my $vendors = $restriction_map->{$restriction_type}->all;
117              
118 6         9 foreach my $vendor ( @{$vendors} ) {
  6         12  
119 6         31 $purpose_restrictions{$vendor} = int($restriction_type);
120             }
121             }
122              
123 6         16 $publisher_restrictions{$purpose_id} = \%purpose_restrictions;
124             }
125              
126 18         68 return \%publisher_restrictions;
127             }
128              
129             1;
130             __END__