File Coverage

blib/lib/License/SPDX.pm
Criterion Covered Total %
statement 88 88 100.0
branch 28 28 100.0
condition n/a
subroutine 23 23 100.0
pod 9 9 100.0
total 148 148 100.0


line stmt bran cond sub pod time code
1             package License::SPDX;
2              
3 10     10   170670 use strict;
  10         12  
  10         317  
4 10     10   90 use warnings;
  10         14  
  10         422  
5              
6 10     10   4224 use Class::Utils qw(set_params);
  10         94162  
  10         192  
7 10     10   10145 use Cpanel::JSON::XS;
  10         54502  
  10         701  
8 10     10   66 use Error::Pure qw(err);
  10         16  
  10         397  
9 10     10   3514 use File::Share ':all';
  10         273533  
  10         1667  
10 10     10   74 use List::Util qw(first);
  10         13  
  10         545  
11 10     10   4516 use Perl6::Slurp qw(slurp);
  10         17312  
  10         62  
12              
13             our $VERSION = 0.07;
14              
15             # Constructor.
16             sub new {
17 22     22 1 1819240 my ($class, @params) = @_;
18              
19             # Create object.
20 22         52 my $self = bless {}, $class;
21              
22             # Process parameters.
23 22         116 set_params($self, @params);
24              
25             # Load all SPDX licenses.
26 22         250 open my $data_fh, '<', dist_dir('License-SPDX').'/licenses.json';
27 22         4095 my $data = slurp($data_fh);
28 22         80332 $self->{'licenses'} = Cpanel::JSON::XS->new->ascii->pretty->allow_nonref->decode($data);
29              
30             # Load all SPDX exceptions.
31 22         308 open my $data_exc_fh, '<', dist_dir('License-SPDX').'/exceptions.json';
32 22         3600 my $data_exc = slurp($data_exc_fh);
33 22         10449 $self->{'exceptions'} = Cpanel::JSON::XS->new->ascii->pretty->allow_nonref->decode($data_exc);
34              
35 22         9827 return $self;
36             }
37              
38             sub check_exception {
39 7     7 1 59 my ($self, $check_string, $opts_hr) = @_;
40              
41 7 100       19 if (! defined $opts_hr) {
42 2         3 $opts_hr = {};
43             }
44 7 100       13 if (! exists $opts_hr->{'check_type'}) {
45 2         6 $opts_hr->{'check_type'} = 'id';
46             }
47              
48             my $check_cb = sub {
49 406     406   322 my $exception_hr = shift;
50 406 100       432 if ($opts_hr->{'check_type'} eq 'id') {
    100          
51 270 100       364 if ($check_string eq $exception_hr->{'licenseExceptionId'}) {
52 2         10 return 1;
53             }
54             } elsif ($opts_hr->{'check_type'} eq 'name') {
55 135 100       183 if ($check_string eq $exception_hr->{'name'}) {
56 1         2 return 1;
57             }
58             } else {
59 1         7 err "Check type '$opts_hr->{'check_type'}' doesn't supported.";
60             }
61 7         26 };
62              
63 7 100   406   22 if (first { $check_cb->($_); } @{$self->{'exceptions'}->{'exceptions'}}) {
  406         404  
  7         45  
64 3         7 return 1;
65             } else {
66 3         8 return 0;
67             }
68             }
69              
70             sub check_license {
71 7     7 1 91 my ($self, $check_string, $opts_hr) = @_;
72              
73 7 100       25 if (! defined $opts_hr) {
74 2         4 $opts_hr = {};
75             }
76 7 100       19 if (! exists $opts_hr->{'check_type'}) {
77 2         5 $opts_hr->{'check_type'} = 'id';
78             }
79              
80             my $check_cb = sub {
81 3532     3532   4292 my $license_hr = shift;
82 3532 100       5909 if ($opts_hr->{'check_type'} eq 'id') {
    100          
83 2354 100       4558 if ($check_string eq $license_hr->{'licenseId'}) {
84 2         15 return 1;
85             }
86             } elsif ($opts_hr->{'check_type'} eq 'name') {
87 1177 100       3006 if ($check_string eq $license_hr->{'name'}) {
88 1         6 return 1;
89             }
90             } else {
91 1         6 err "Check type '$opts_hr->{'check_type'}' doesn't supported.";
92             }
93 7         39 };
94              
95 7 100   3532   25 if (first { $check_cb->($_); } @{$self->{'licenses'}->{'licenses'}}) {
  3532         5172  
  7         140  
96 3         14 return 1;
97             } else {
98 3         20 return 0;
99             }
100             }
101              
102             sub exception {
103 2     2 1 14 my ($self, $exception_id) = @_;
104              
105 2     135   7 return first { $_->{'licenseExceptionId'} eq $exception_id } @{$self->{'exceptions'}->{'exceptions'}};
  135         141  
  2         13  
106             }
107              
108             sub exceptions {
109 1     1 1 7 my $self = shift;
110              
111 1         2 return @{$self->{'exceptions'}->{'exceptions'}};
  1         6  
112             }
113              
114             sub license {
115 2     2 1 13 my ($self, $license_id) = @_;
116              
117 2     1177   8 return first { $_->{'licenseId'} eq $license_id } @{$self->{'licenses'}->{'licenses'}};
  1177         1169  
  2         33  
118             }
119              
120             sub licenses {
121 1     1 1 5 my $self = shift;
122              
123 1         2 return @{$self->{'licenses'}->{'licenses'}};
  1         62  
124             }
125              
126             sub spdx_release_date {
127 1     1 1 6 my $self = shift;
128              
129 1         4 return $self->{'licenses'}->{'releaseDate'};
130             }
131              
132             sub spdx_version {
133 1     1 1 6 my $self = shift;
134              
135 1         3 return $self->{'licenses'}->{'licenseListVersion'};
136             }
137              
138             1;
139              
140             __END__