blib/lib/PAUSE/Permissions/MetaCPAN.pm

filter: Statementstmt 24.0% 18 / 75 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 6 / 9 Podpod 100% 2 / 2 totaltotal 21.8% 26 / 119 CC 16 SCAR 37.4
CC
16
Cov
17%
get
72.0
_query
56.0
new
12.0
CRAP 42.1
↑ Summary Build »
1 package PAUSE::Permissions::MetaCPAN 0.100;
2 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Tiny 0.055;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Socket::SSL 1.42;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
12 0 ▶ my $http = $args{http} || do {
Condition0%0/3
$args{'http'} || do { my $agent = sprintf('%s/%s', $class =~ s/::/-/gru, $class->VERSION); 'HTTP::Tiny'->new('verify_SSL', 1, 'agent', $agent) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'http'} || do { my $agent = sprintf('%s/%s', $class =~ s/::/-/gru, $class->VERSION); 'HTTP::Tiny'->new('verify_SSL', 1, 'agent', $agent) }
$args{'http'} do { my $agent = sprintf('%s/%s', $class =~ s/::/-/gru, $class->VERSION); 'HTTP::Tiny'->new('verify_SSL', 1, 'agent', $agent) }
Truth table0%0/3
A || B
A= $args{'http'}B= do { my $agent = sprintf('%s/%s', $class =~ s/::/-/gru, $class->VERSION); 'HTTP::Tiny'->new('verify_SSL', 1, 'agent', $agent) }
ABresult
1X1
011
000
13 0 my $agent = sprintf "%s/%s", $class =~ s/::/-/gr, $class->VERSION;
14 0 HTTP::Tiny->new(verify_SSL => 1, agent => $agent);
15 };
16 0 ▶ my $url = $args{url} || "https://fastapi.metacpan.org/v1/permission/_search";
Condition0%0/2
$args{'url'} || 'https://fastapi.metacpan.org/v1/permission/_search'
l!l
00
MC/DC0%0/1
$args{'url'} || 'https://fastapi.metacpan.org/v1/permission/_search'
$args{'url'}
Truth table0%0/2
A
A= $args{'url'}
Aresult
11
00
17 0 bless { http => $http, url => $url }, $class;
18 }
19  
20 sub get {
21 0 ▶ my ($self, %args) = @_;
Subroutine get: not called CC=8 SCAR=42.8
Pod documented
22  
23 0 ▶ Carp::croak "either author or modules is required" if !$args{author} && !$args{modules};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"author"} or $args{"modules"} 0 0 0
Condition0%0/3
$args{"author"} or $args{"modules"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"author"} or $args{"modules"}
$args{"author"} $args{"modules"}
Truth table0%0/3
A or B
A= $args{"author"}B= $args{"modules"}
ABresult
1X1
011
000
24  
25 0 ▶ if (my $author = $args{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $author = $args{"author"}) 0 0 0
26 0 my $hit = $self->_query(%args);
27 0 my %hit = (owner => [], co_maintainer => []);
28 0 for my $module (@$hit) {
29 0 ▶ if ($module->{owner} eq $author) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module->{'owner'} eq $author) { } 0 0 0
30 0 push @{$hit{owner}}, $module;
31 } else {
32 0 push @{$hit{co_maintainer}}, $module;
33 }
34 }
35 0 return \%hit;
36 }
37  
38 0 my @hit;
39 0 my @module = @{$args{modules}}; # must copy
40 # elasticsearch may return "too_many_clauses: maxClauseCount is set to 1024"
41 0 ▶ while (my @m = splice @module, 0, 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@m) = splice(@module, 0, 1024)) 0 0 0
42 0 my $hit = $self->_query(modules => \@m);
43 0 push @hit, @$hit;
44 }
45 0 my %hit;
46 0 for my $module (@{$args{modules}}) {
47 0 my ($found) = grep { $_->{module_name} eq $module} @hit;
48 0 $hit{$module} = $found;
49 }
50 0 return \%hit;
51 }
52  
53 sub _query {
54 0 ▶ my ($self, %args) = @_;
Subroutine _query: not called CC=7 SCAR=40.3
55  
56 0 my %bool;
57 0 ▶ if (my $author = $args{author}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $author = $args{'author'}) { } 0 0 0
elsif (my $modules = $args{'modules'}) { } 0 0 0
58 $bool{should} = [
59 0 { term => { owner => $author } },
60 { term => { co_maintainers => $author } },
61 ];
62 0 $bool{minimum_should_match} = 1;
63 } elsif (my $modules = $args{modules}) {
64 $bool{should} = [
65 0 map +{ term => { module_name => $_ } }, @$modules
66 ];
67 0 $bool{minimum_should_match} = 1;
68 }
69  
70 0 my $from = 0;
71 0 my $times = 0;
72 0 my @hit;
73 0 while (1) {
74 0 $times++;
75 0 ▶ Carp::croak "too many request for $self->{url}" if $times > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $times > 6 0 0 0
76 0 my $payload = {
77 query => { bool => \%bool },
78 sort => [ { module_name => 'asc' } ],
79 size => 2000,
80 from => $from,
81 };
82 0 my $body = JSON::PP::encode_json $payload;
83 my $res = $self->{http}->post($self->{url}, {
84 0 'content-type' => 'application/json',
85 'content-length' => length $body,
86 content => $body,
87 });
88 0 ▶ if ($res->{status} == 404) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->{'status'} == 404) { } 0 0 0
elsif (not $res->{'success'}) { } 0 0 0
89 0 last;
90 } elsif (!$res->{success}) {
91 0 Carp::croak "$res->{status} $res->{reason}, $self->{url}\n$res->{content}";
92 }
93 0 my $json = JSON::PP::decode_json $res->{content};
94 0 my $total = $json->{hits}{total};
95 0 push @hit, map $_->{_source}, @{$json->{hits}{hits}};
96 0 ▶ last if @hit >= $total;
Branch: 1 branch
BranchTrueFalseTotal
if @hit >= $total 0 0 0
97 0 $from = @hit;
98 }
99 0 \@hit;
100 }
101  
102 1 1;
103 __END__
↑ Summary Build »