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