Marvel/Client.pm

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