| 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
$self->agent // ""
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
Truth table50%1/2
A
A= $self->agent
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$id and $id =~ /^\d+$/
MC/DC100%2/2
$id and $id =~ /^\d+$/
$id
$id =~ /^\d+$/
Truth table100%3/3
A and B
A= $idB= $id =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||