| 1 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package MetaCPAN::API; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 16 | our $VERSION = '0.52'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Types::Standard qw<Str ArrayRef InstanceOf>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 16 | ▶ | use JSON::MaybeXS 1.001000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 16 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 16 | ▶ | use HTTP::Tiny 0.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 16 | with qw/ | |||||||||||||||||||||||||||
| 17 | MetaCPAN::API::Author | ||||||||||||||||||||||||||||
| 18 | MetaCPAN::API::Distribution | ||||||||||||||||||||||||||||
| 19 | MetaCPAN::API::Favorite | ||||||||||||||||||||||||||||
| 20 | MetaCPAN::API::File | ||||||||||||||||||||||||||||
| 21 | MetaCPAN::API::Autocomplete | ||||||||||||||||||||||||||||
| 22 | MetaCPAN::API::Module | ||||||||||||||||||||||||||||
| 23 | MetaCPAN::API::POD | ||||||||||||||||||||||||||||
| 24 | MetaCPAN::API::Rating | ||||||||||||||||||||||||||||
| 25 | MetaCPAN::API::Release | ||||||||||||||||||||||||||||
| 26 | MetaCPAN::API::Source | ||||||||||||||||||||||||||||
| 27 | /; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | has base_url => ( | ||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | isa => Str, | ||||||||||||||||||||||||||||
| 32 | 16 | ▶ | default => sub{'https://fastapi.metacpan.org/v1'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 16 | ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 16 | has ua => ( | |||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | lazy => 1, | ||||||||||||||||||||||||||||
| 38 | builder => '_build_ua', | ||||||||||||||||||||||||||||
| 39 | isa => InstanceOf['HTTP::Tiny'], | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | has ua_args => ( | ||||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||
| 44 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 45 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | my $version = $MetaCPAN::API::VERSION || 'xx'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$MetaCPAN::API::VERSION || 'xx'
MC/DC0%0/1
$MetaCPAN::API::VERSION || 'xx'
$MetaCPAN::API::VERSION
Truth table50%1/2
A
A= $MetaCPAN::API::VERSION
| |||||||||||||||||||||||||||||
| 47 | 1 | return [ agent => "MetaCPAN::API/$version" ]; | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 16 | ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 16 | my $JSON = JSON::MaybeXS->new(canonical => 1, utf8 => 1); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 14 | ▶ | sub _build_ua { | ||||||||||||||||||||||||||
|
Subroutine
_build_ua: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | 14 | return HTTP::Tiny->new( @{ $self->ua_args } ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 15 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 15 | my $url = shift; | |||||||||||||||||||||||||||
| 61 | 15 | my $extra = $self->_build_extra_params(@_); | |||||||||||||||||||||||||||
| 62 | 15 | my $base = $self->base_url; | |||||||||||||||||||||||||||
| 63 | 15 | ▶ | my $req_url = $extra ? "$base/$url?$extra" : "$base/$url"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 15 | my $result = $self->ua->get($req_url); | |||||||||||||||||||||||||||
| 66 | 15 | return $self->_decode_result( $result, $req_url ); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | sub post { | ||||||||||||||||||||||||||
|
Subroutine
post: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 5 | my $url = shift; | |||||||||||||||||||||||||||
| 72 | 5 | my $query = shift; | |||||||||||||||||||||||||||
| 73 | 5 | my $base = $self->base_url; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | ▶ | defined $url | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | or croak 'First argument of URL must be provided'; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 4 | ▶ | ref $query and ref $query eq 'HASH' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $query and ref $query eq "HASH"
MC/DC50%1/2
ref $query and ref $query eq "HASH"
ref $query
ref $query eq "HASH"
Truth table66%2/3
A and B
A= ref $queryB= ref $query eq "HASH"
| |||||||||||||||||||||||||||||
| 79 | or croak 'Second argument of query hashref must be provided'; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | my $query_json = $JSON->encode( $query ); | |||||||||||||||||||||||||||
| 82 | 2 | my $result = $self->ua->request( | |||||||||||||||||||||||||||
| 83 | 'POST', | ||||||||||||||||||||||||||||
| 84 | "$base/$url", | ||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | headers => { 'Content-Type' => 'application/json' }, | ||||||||||||||||||||||||||||
| 87 | content => $query_json, | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | return $self->_decode_result( $result, $url, $query_json ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 24 | ▶ | sub _decode_result { | ||||||||||||||||||||||||||
|
Subroutine
_decode_result: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 95 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 24 | my ( $result, $url, $original ) = @_; | |||||||||||||||||||||||||||
| 97 | 24 | my $decoded_result; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 24 | ▶ | ref $result and ref $result eq 'HASH' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $result and ref $result eq "HASH"
MC/DC50%1/2
ref $result and ref $result eq "HASH"
ref $result
ref $result eq "HASH"
Truth table66%2/3
A and B
A= ref $resultB= ref $result eq "HASH"
| |||||||||||||||||||||||||||||
| 100 | or croak 'First argument must be hashref'; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 23 | ▶ | defined $url | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | or croak 'Second argument of a URL must be provided'; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 22 | ▶ | if ( defined ( my $success = $result->{'success'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 21 | ▶ | my $reason = $result->{'reason'} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$result->{'reason'} || ''
MC/DC100%1/1
$result->{'reason'} || ''
$result->{'reason'}
Truth table100%2/2
A
A= $result->{'reason'}
| |||||||||||||||||||||||||||||
| 107 | 21 | ▶ | $reason .= ( defined $original ? " (request: $original)" : '' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 21 | ▶ | $success or croak "Failed to fetch '$url': $reason - $result->{content}"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 1 | croak 'Missing success in return value'; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 19 | ▶ | defined ( my $content = $result->{'content'} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | or croak 'Missing content in return value'; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 19 | ▶ | try { $decoded_result = $JSON->decode( $content ) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 118 | 19 | ▶ | catch { croak "Couldn't decode '$content': $_" }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 18 | return $decoded_result; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 19 | ▶ | sub _build_extra_params { | ||||||||||||||||||||||||||
|
Subroutine
_build_extra_params: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 124 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 19 | ▶ | @_ % 2 == 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | or croak 'Incorrect number of params, must be key/value'; | ||||||||||||||||||||||||||||
| 128 | 18 | my %extra = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # if it's deep, JSON encoding needs to be involved | ||||||||||||||||||||||||||||
| 131 | 18 | ▶ | if (scalar grep { ref } values %extra) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 1 | my $query_json = $JSON->encode( \%extra ); | |||||||||||||||||||||||||||
| 133 | 1 | %extra = ( source => $query_json ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 18 | my $extra = $self->ua->www_form_urlencode(\%extra); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 18 | return $extra; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 16 | 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||