blib/lib/WWW/XKCD/AsText.pm

filter: Statementstmt 100% 43 / 43 Branchbran 83.3% 5 / 6 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 50.0% 1 / 2 totaltotal 91.5% 65 / 71 CC 8 SCAR 10.4
CC
8
Cov
95%
retrieve
5.0
_build_ua
3.3
BUILD
2.1
CRAP 2.8
↑ Summary
1 =head1 NAME
2  
3 WWW::XKCD::AsText - retrieve text versions of comics on www.xkcd.com
4  
5 =head1 SYNOPSIS
6  
7 use WWW::XKCD::AsText;
8  
9 my $xkcd = WWW::XKCD::AsText->new;
10 my $xkcd_retrieve = $xkcd->retrieve(1); # "A boy sits in a barrel..."
11  
12 $xkcd_retrieve->uri; # "https://www.xkcd.com/1/"
13 $xkcd_retrieve->text; # "A boy sits in a barrel..."
14 $xkcd_retrieve->error; # line_status
15  
16 =cut
17  
18 package WWW::XKCD::AsText;
19 2 $WWW::XKCD::AsText::VERSION = '0.006';
20 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 has 'timeout' => (
29 is => 'ro',
30 isa => 'Maybe[Int]',
31 required => 1,
32 default => 30,
33 );
34  
35 2 has 'agent' => (
36 is => 'ro',
37 isa => 'Maybe[Str]',
38 );
39  
40 2 has 'ua' => (
41 is => 'rw',
42 isa => 'Maybe[LWP::UserAgent]',
43 builder => '_build_ua',
44 );
45  
46 1 ▶sub _build_ua{
Subroutine _build_ua: called CC=3 SCAR=12.0
47 1 my $self = shift;
48 1 ▶ return LWP::UserAgent->new(
Condition50%2/4
$self->timeout // 30
l!l
10
$self->agent // ""
l!l
01
MC/DC0%0/1
$self->timeout // 30
$self->timeout
MC/DC0%0/1
$self->agent // ""
$self->agent
Truth table50%1/2
A
A= $self->timeout
Aresult
11
00
Truth table50%1/2
A
A= $self->agent
Aresult
11
00
49 timeout => $self->timeout // 30,
50 agent => $self->agent // '',
51 );
52 }
53  
54 2 has 'uri' => (
55 is => 'rw',
56 isa => 'Maybe[URI]',
57 );
58  
59 2 has 'error' => (
60 is => 'rw',
61 isa => 'Maybe[Str]',
62 );
63  
64 2 has 'text' => (
65 is => 'rw',
66 isa => 'Maybe[Str]',
67 );
68  
69 1 ▶sub BUILD{
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
70 1 my $self = shift;
71 1 ▶ $self->ua($self->_build_ua) unless defined $self->ua;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->ua 0 1 1
72 }
73  
74 =head1 METHODS
75  
76 =head2 retrieve
77  
78 Takes XKCD comic number, returns its transcript.
79  
80 =cut
81  
82 5 ▶sub retrieve {
Subroutine retrieve: called CC=5 SCAR=16.1
Pod documented
83 5 my ($self, $id) = @_;
84  
85 5 $self->$_(undef) foreach (qw(uri text error));
86  
87 5 ▶ croak 'ID must be a valid number' unless $id && $id=~/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $id and $id =~ /^\d+$/ 3 2 5
Condition100%3/3
$id and $id =~ /^\d+$/
!ll&&!rl&&r
122
MC/DC100%2/2
$id and $id =~ /^\d+$/
$id $id =~ /^\d+$/
Truth table100%3/3
A and B
A= $idB= $id =~ /^\d+$/
ABresult
0X0
100
111
88  
89 2 $self->uri(URI->new("http://xkcd.com/$id/"));
90 2 my $json_uri = URI->new("https://xkcd.com/$id/info.0.json");
91 2 my $response = $self->ua->get($json_uri);
92  
93 2 ▶ if ($response->is_success){
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) 1 1 2
94 1 my $decoded = decode_json($response->content);
95 1 $self->text($decoded->{transcript});
96 1 return $self->text;
97 }
98  
99 1 $self->error($response->status_line);
100 1 return;
101  
102 }
103  
104 2 1;
105 2 __PACKAGE__->meta->make_immutable;
106  
107 =head1 SEE ALSO
108  
109 L<WWW::xkcd|https://metacpan.org/pod/WWW::xkcd>
110  
111 =head1 AUTHOR
112  
113 Original author is Zoffix Znet, C<< <zoffix at cpan.org> >>,
114  
115 currently maintained by Kivanc Yazan, C<< <kyzn at cpan.org> >>.
116  
117 =head1 LICENSE
118  
119 This is free software; you can redistribute it and/or modify it under
120 the same terms as the Perl 5 programming language system itself.
121  
122 =cut
123  
124 __END__
↑ Summary