blib/lib/PAUSE/Permissions/MetaCPAN.pm

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