Marvel/Client.pm

filter: Statementstmt 70.9% 61 / 86 Branchbran 40.0% 8 / 20 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 78.2% 18 / 23 Podpod 0.0% 0 / 15 totaltotal 51.4% 91 / 177 CC 19 SCAR 19.6
CC
19
Cov
58%
get
30.0
uri
10.6
json
6.0
CRAP 7.1
« Marvel/Factory/Entity.pm ↑ Summary Marvel/Entity/Character.pm »
1 package WWW::Marvel::Client;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
11 1 my ($class, $args) = @_;
12 1 my $self = bless {}, $class;
13 1 for my $k (qw/ private_key public_key /) {
14 2 ▶ $self->{$k} = $args->{$k} if exists $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$k} 2 0 2
15 }
16 1 return $self;
17 }
18  
19 3 ▶sub get_private_key { $_[0]->{private_key} }
Subroutine get_private_key: called CC=1 SCAR=0.0
Pod undocumented
20 4 ▶sub get_public_key { $_[0]->{public_key} }
Subroutine get_public_key: called CC=1 SCAR=0.0
Pod undocumented
21 4 ▶sub get_timestamp { $_[0]->{timestamp} }
Subroutine get_timestamp: called CC=1 SCAR=0.0
Pod undocumented
22 1 ▶sub set_timestamp {
Subroutine set_timestamp: called CC=1 SCAR=0.0
Pod undocumented
23 1 my ($self, $timestamp) = @_;
24 1 $self->{timestamp} = $timestamp;
25 1 return $self;
26 }
27  
28 1 ▶sub get_endpoint { "http://gateway.marvel.com" }
Subroutine get_endpoint: called CC=1 SCAR=0.0
Pod undocumented
29 1 ▶sub get_api_version { "v1" }
Subroutine get_api_version: called CC=1 SCAR=0.0
Pod undocumented
30  
31 1 ▶sub auth_query_params {
Subroutine auth_query_params: called CC=2 SCAR=7.0
Pod undocumented
32 1 my ($self) = @_;
33  
34 1 ▶ my $time = $self->get_timestamp || time;
Condition50%1/2
$self->get_timestamp || time
l!l
10
MC/DC0%0/1
$self->get_timestamp || time
$self->get_timestamp
Truth table50%1/2
A
A= $self->get_timestamp
Aresult
11
00
35  
36 1 my %params = (
37 ts => $time,
38 apikey => $self->get_public_key,
39 hash => $self->hash($time),
40 );
41 1 return \%params;
42 }
43  
44 0 ▶sub characters {
Subroutine characters: not called CC=1 SCAR=6.9
Pod undocumented
45 0 my ($self, $params) = @_;
46 0 my %p = %$params;
47 0 my $uri = $self->uri({ path => 'characters', params => \%p });
48 0 my $http_res = $self->get($uri);
49 0 return $self->_get_json_content($http_res);
50 }
51  
52 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod undocumented
53 0 my ($self, $uri) = @_;
54 0 my $attempts_limit = 3;
55 0 my $http_res;
56 0 ▶ while ($attempts_limit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempts_limit > 0) 0 0 0
57 0 $http_res = $self->ua->get($uri);
58 0 ▶ last if $http_res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $http_res->is_success 0 0 0
59  
60 0 carp sprintf("Status line: '%s'", $http_res->status_line);
61 0 $attempts_limit--;
62 0 ▶ next if $attempts_limit && $http_res->status_line eq "500 read timeout";
Branch: 1 branch
BranchTrueFalseTotal
if $attempts_limit and $http_res->status_line eq "500 read timeout" 0 0 0
Condition0%0/3
$attempts_limit and $http_res->status_line eq "500 read timeout"
!ll&&!rl&&r
000
MC/DC0%0/2
$attempts_limit and $http_res->status_line eq "500 read timeout"
$attempts_limit $http_res->status_line eq "500 read timeout"
Truth table0%0/3
A and B
A= $attempts_limitB= $http_res->status_line eq "500 read timeout"
ABresult
0X0
100
111
63  
64 0 croak sprintf("Status line: '%s'\nReq: %s\nRes: %s",
65 $http_res->status_line,
66 $http_res->request->as_string,
67 $http_res->as_string);
68 }
69  
70 0 return $http_res;
71 }
72  
73 # ref: https://developer.marvel.com/documentation/authorization
74 3 ▶sub hash {
Subroutine hash: called CC=3 SCAR=11.0
Pod undocumented
75 3 my ($self, $time) = @_;
76  
77 3 ▶ if (!defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 1 2 3
78 1 ▶ croak "need a timestamp to create a md5 hash" if !defined $self->get_timestamp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_timestamp 0 1 1
79 1 $time = $self->get_timestamp;
80 }
81  
82 3 my $md5 = Digest::MD5->new;
83 3 $md5->add( $time, $self->get_private_key, $self->get_public_key );
84 3 return $md5->hexdigest;
85 }
86  
87 0 ▶sub _get_json_content {
Subroutine _get_json_content: not called CC=1 SCAR=6.9
88 0 my ($self, $http_res) = @_;
89 0 my $content = $self->json->decode( $http_res->decoded_content );
90 0 return $content;
91 }
92  
93 0 ▶sub json { $_[0]->{_json} //= JSON->new()->allow_nonref->relaxed }
Subroutine json: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"_json"} //= "JSON"->new->allow_nonref->relaxed
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"_json"} //= "JSON"->new->allow_nonref->relaxed
$_[0]{"_json"} "JSON"->new->allow_nonref->relaxed
Truth table0%0/3
A //= B
A= $_[0]{"_json"}B= "JSON"->new->allow_nonref->relaxed
ABresult
1X1
011
000
94  
95 0 ▶sub ua { $_[0]->{_ua} //= LWP::UserAgent->new() }
Subroutine ua: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
$_[0]{"_ua"} //= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"_ua"} //= "LWP::UserAgent"->new
$_[0]{"_ua"} "LWP::UserAgent"->new
Truth table0%0/3
A //= B
A= $_[0]{"_ua"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
96  
97 1 ▶sub uri {
Subroutine uri: called CC=7 SCAR=23.6
Pod undocumented
98 1 my ($self, $args) = @_;
99  
100  
101 1 my @paths;
102 1 ▶ if (exists $args->{path} && ref $args->{path} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{'path'} and ref $args->{'path'} eq 'ARRAY') { } 0 1 1
elsif (exists $args->{'path'} and ref $args->{'path'} eq '') { } 1 0 1
Condition33%1/3
exists $args->{"path"} and ref $args->{"path"} eq "ARRAY"
!ll&&!rl&&r
010
MC/DC0%0/2
exists $args->{"path"} and ref $args->{"path"} eq "ARRAY"
exists $args->{"path"} ref $args->{"path"} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $args->{"path"}B= ref $args->{"path"} eq "ARRAY"
ABresult
0X0
100
111
103 0 @paths = @{ $args->{path} };
104 }
105 ▶ elsif (exists $args->{path} && ref $args->{path} eq '') {
Condition33%1/3
exists $args->{"path"} and ref $args->{"path"} eq ""
!ll&&!rl&&r
001
MC/DC0%0/2
exists $args->{"path"} and ref $args->{"path"} eq ""
exists $args->{"path"} ref $args->{"path"} eq ""
Truth table33%1/3
A and B
A= exists $args->{"path"}B= ref $args->{"path"} eq ""
ABresult
0X0
100
111
106 1 push(@paths, $args->{path});
107 }
108  
109 1 my %params;
110 1 ▶ if (exists $args->{params} && ref $args->{params} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"params"} and ref $args->{"params"} eq "HASH") 0 1 1
Condition33%1/3
exists $args->{"params"} and ref $args->{"params"} eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args->{"params"} and ref $args->{"params"} eq "HASH"
exists $args->{"params"} ref $args->{"params"} eq "HASH"
Truth table33%1/3
A and B
A= exists $args->{"params"}B= ref $args->{"params"} eq "HASH"
ABresult
0X0
100
111
111 0 %params = %{ $args->{params} };
112 }
113  
114  
115 1 my $auth_query_params = $self->auth_query_params;
116 1 my %query_params = ( %params, %$auth_query_params );
117  
118 1 my $uri = URI->new( $self->get_endpoint );
119 1 $uri->path( $self->uri_path(@paths) );
120 1 $uri->query_form(%query_params);
121  
122 1 return $uri;
123 }
124  
125 1 ▶sub uri_path {
Subroutine uri_path: called CC=2 SCAR=7.1
Pod undocumented
126 1 my ($self, @resources) = @_;
127 1 ▶ croak "need a resource to query" if @resources < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @resources < 1 0 1 1
128 1 return join('/', $self->get_api_version, 'public', @resources );
129 }
130  
131 2 1;
« Marvel/Factory/Entity.pm ↑ Summary Marvel/Entity/Character.pm »