blib/lib/WWW/xkcd.pm

filter: Statementstmt 100% 87 / 87 Branchbran 91.6% 22 / 24 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 95.6% 132 / 138 CC 15 SCAR 10.8
CC
15
Cov
97%
fetch_metadata
5.0
_decode_json
4.0
_parse_args
4.0
CRAP 2.9
↑ Summary
1 package WWW::xkcd;
2 # ABSTRACT: Synchronous and asynchronous interfaces to xkcd comics
3 6 $WWW::xkcd::VERSION = '0.009';
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 my $can_async = eval { require AnyEvent; require AnyEvent::HTTP; 1 };
11  
12 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 5 my $class = shift;
14 5 my %args = (
15 'baseurl' => 'https://xkcd.com',
16 'infopath' => 'info.0.json',
17 @_,
18 );
19  
20 5 return bless { %args }, $class;
21 }
22  
23 13 ▶sub fetch_metadata {
Subroutine fetch_metadata: called CC=5 SCAR=16.1
Pod documented
24 13 my $self = shift;
25 13 my $base = $self->{'baseurl'};
26 13 my $path = $self->{'infopath'};
27 13 my ( $comic, $cb ) = $self->_parse_args(@_);
28  
29 13 ▶ my $url = defined $comic ? "$base/$comic/$path" : "$base/$path";
Branch: 1 branch
BranchTrueFalseTotal
defined $comic ? : 7 6 13
30  
31 13 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 6 7 13
32 # this is async
33 6 ▶ $can_async
Branch: 1 branch
BranchTrueFalseTotal
$can_async or croak("AnyEvent and AnyEvent::HTTP are required for async mode") 6 0 6
34 or croak 'AnyEvent and AnyEvent::HTTP are required for async mode';
35  
36 6 ▶ AnyEvent::HTTP::http_get( $url, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 6 my $body = shift;
38 6 my $meta = $self->_decode_json($body);
39  
40 6 return $cb->($meta);
41 6 } );
42  
43 6 return 0;
44 }
45  
46 # this is sync
47 7 my $result = HTTP::Tiny->new->get($url);
48  
49 $result->{'success'} or croak "Can't fetch $url: " .
50 7 ▶ $result->{'reason'};
Branch: 1 branch
BranchTrueFalseTotal
$result->{"success"} or croak("Can't fetch ${url}: " . $result->{"reason"}) 6 1 7
51  
52 6 my $meta = $self->_decode_json( $result->{'content'} );
53  
54 6 return $meta;
55 }
56  
57 2 ▶sub fetch_random {
Subroutine fetch_random: called CC=3 SCAR=11.0
Pod documented
58 2 my $self = shift;
59 2 my $callback = shift;
60  
61 2 ▶ if ( $callback && ref $callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback and ref $callback) 1 1 2
Condition66%2/3
$callback and ref $callback
!ll&&!rl&&r
101
MC/DC50%1/2
$callback and ref $callback
$callback ref $callback
Truth table66%2/3
A and B
A= $callbackB= ref $callback
ABresult
0X0
100
111
62 1 ▶ $self->fetch_metadata( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1 my $metadata = shift;
64 1 my $random = int(rand($metadata->{'num'})) + 1;
65 1 return $self->fetch($random, $callback);
66 1 } );
67 1 return 0;
68 }
69  
70 1 my $metadata = $self->fetch_metadata;
71 1 my $random = int(rand($metadata->{'num'})) + 1;
72 1 return $self->fetch($random);
73 }
74  
75 7 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.0
Pod documented
76 7 my $self = shift;
77 7 my $base = $self->{'baseurl'};
78 7 my $path = $self->{'infopath'};
79 7 my ( $comic, $cb ) = $self->_parse_args(@_);
80  
81 7 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 3 4 7
82 3 ▶ $self->fetch_metadata( $comic, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
83 3 my $meta = shift;
84 3 my $img = $meta->{'img'};
85  
86 3 ▶ AnyEvent::HTTP::http_get( $img, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 3 my $img_data = shift;
88  
89 # call original callback
90 3 return $cb->( $img_data, $meta );
91 3 } );
92 3 } );
93  
94 3 return 0;
95 }
96  
97 4 my $meta = $self->fetch_metadata($comic);
98 4 my $img = $meta->{'img'};
99 4 my $result = HTTP::Tiny->new->get($img);
100  
101 $result->{'success'} or croak "Can't fetch $img: " .
102 4 ▶ $result->{'reason'};
Branch: 1 branch
BranchTrueFalseTotal
$result->{"success"} or croak("Can't fetch ${img}: " . $result->{"reason"}) 3 1 4
103  
104 3 return ( $result->{'content'}, $meta );
105 }
106  
107 20 ▶sub _parse_args {
Subroutine _parse_args: called CC=4 SCAR=13.9
108 20 my $self = shift;
109 20 my @args = @_;
110 20 my ( $comic, $cb );
111  
112 # @_ = $num, $cb
113 # @_ = $num
114 # @_ = $cb
115 20 ▶ if ( @_ == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 6 14 20
elsif (@_ == 1) { } 11 3 14
116 6 ( $comic, $cb ) = @_;
117 } elsif ( @_ == 1 ) {
118 11 ▶ if ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 3 8 11
119 3 $cb = $_[0];
120 } else {
121 8 $comic = $_[0];
122 }
123 }
124  
125 20 return ( $comic, $cb );
126 }
127  
128 14 ▶sub _decode_json {
Subroutine _decode_json: called CC=4 SCAR=13.9
129 14 my $self = shift;
130 14 my $json = shift;
131 14 my $data = {};
132  
133 14 ▶ defined $json or $json = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $json or $json = "" 14 0 14
134  
135 14 ▶ eval { $data = decode_json $json; 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $data = &decode_json($json); 1 } } or do { my $error = $@ || "Zombie error"; croak("Can't decode JSON content '${json}': $error") } 13 1 14
136 1 ▶ my $error = $@ || 'Zombie error';
Condition50%1/2
$@ || 'Zombie error'
l!l
10
MC/DC0%0/1
$@ || 'Zombie error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
137 1 croak "Can't decode JSON content '$json': $error";
138 };
139  
140 13 return $data;
141 }
142  
143 6 1;
144  
145 __END__
↑ Summary