File Coverage

blib/lib/WWW/XKCD/AsText.pm
Criterion Covered Total %
statement 34 35 97.1
branch 5 6 83.3
condition 4 9 44.4
subroutine 10 11 90.9
pod 1 2 50.0
total 54 63 85.7


line stmt bran cond sub pod time code
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 $text = $xkcd->retrieve(1);
11              
12             =cut
13              
14             package WWW::XKCD::AsText;
15             $WWW::XKCD::AsText::VERSION = '0.004';
16 2     2   42067 use warnings;
  2         4  
  2         51  
17 2     2   6 use strict;
  2         3  
  2         30  
18              
19 2     2   5 use Carp;
  2         8  
  2         107  
20 2     2   463 use URI;
  2         5499  
  2         41  
21 2     2   632 use JSON;
  2         8654  
  2         12  
22 2     2   674 use Furl;
  2         21527  
  2         45  
23 2     2   538 use Try::Tiny;
  2         998  
  2         443  
24              
25             sub new {
26 1     1 0 9 my $class = shift;
27 1         5 return bless {furl => Furl->new}, $class;
28             }
29              
30             =head1 METHODS
31              
32             =head2 retrieve
33              
34             Takes XKCD comic number, returns its transcript.
35              
36             =cut
37              
38             sub retrieve {
39 5     5 1 817 my ($self, $id) = @_;
40 5 100 100     67 croak 'ID must be a valid number' unless $id && $id=~/^\d{1,6}$/;
41              
42 2         4 my ($xkcd_url, $furl_get, $decoded, $transcript);
43              
44 2         21 $xkcd_url = URI->new("http://xkcd.com/$id/info.0.json");
45 2         10118 $furl_get = $self->{furl}->get($xkcd_url);
46 2 100 50     300184 croak 'Cannot retrieve '. ($id // 'undef') unless $furl_get->is_success;
47              
48             try {
49 1     1   57 $decoded = decode_json($furl_get->content);
50             } catch {
51 0   0 0   0 croak 'Cannot decode JSON for '. ($id // 'undef');
52 1         34 };
53              
54 1         37 $transcript = $decoded->{transcript};
55              
56 1 50 0     4 croak 'No transcript found for '. ($id // 'undef') unless $transcript;
57 1         42 return $transcript;
58             }
59              
60             1;
61              
62             =head1 SEE ALSO
63              
64             L
65              
66             =head1 AUTHOR
67              
68             Original author is Zoffix Znet, Ezoffix@cpan.orgE,
69             currently maintained by Kivanc Yazan, Ekyzn@cpan.orgE.
70              
71             =head1 LICENSE
72              
73             This is free software; you can redistribute it and/or modify it under
74             the same terms as the Perl 5 programming language system itself.
75              
76             =cut
77              
78             __END__