File Coverage

blib/lib/WWW/Speakerdeck/Download.pm
Criterion Covered Total %
statement 37 37 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 6 6 100.0
pod 1 1 100.0
total 61 61 100.0


line stmt bran cond sub pod time code
1             package WWW::Speakerdeck::Download;
2             $WWW::Speakerdeck::Download::VERSION = '0.01';
3             # ABSTRACT: Download a deck from speakerdeck.com
4              
5 1     1   761 use Mojo::Base -base, -signatures;
  1         2  
  1         10  
6              
7 1     1   4543 use Carp;
  1         2  
  1         54  
8 1     1   7 use Mojo::UserAgent;
  1         2  
  1         16  
9 1     1   32 use Mojo::File qw(path);
  1         3  
  1         48  
10 1     1   5 use Scalar::Util qw(blessed);
  1         3  
  1         391  
11              
12             has ua => sub {
13             Mojo::UserAgent->new
14             };
15              
16             has base_url => 'https://speakerdeck.com';
17              
18 8     8 1 9329 sub download ($self, $user, $name, $to = '' ) {
  8         21  
  8         15  
  8         16  
  8         18  
  8         15  
19              
20 8 100 100     153 croak 'need plain path to target or Mojo::File object' if !blessed( $to ) && ref $to;
21              
22 7         28 my $url = join '/', $self->base_url, $user, $name;
23 7         71 my $tx = $self->ua->get( $url );
24              
25 7 100       10733 croak 'cannot get deck ' . $url . ': ' . $tx->res->message if $tx->res->is_error;
26              
27 5         119 my $icon = $tx->res->dom->find("svg.icon-download")->first;
28              
29 5 100       361737 croak 'cannot download ' . $name . ' deck: download link not found' if !$icon;
30              
31 4         41 my $link = $icon->parent->attr('href');
32              
33 4         301 my $basename = path( $link )->basename;
34              
35 4 100       383 $to = path( '.', $basename ) if !$to;
36 4 100       60 $to = path($to) if !blessed( $to );
37              
38 4 100       219 croak 'need plain path to target or Mojo::File object' if !$to->isa('Mojo::File');
39              
40 3         17 my $download_tx = $self->ua->get( $link );
41 3         17770 my $res = $download_tx->res;
42              
43 3 100       23 croak 'cannot download deck: ' . $res->message if $res->is_error;
44              
45 2         53 $to->spurt( $res->body );
46              
47 2         2478 return 1;
48             }
49              
50             1;
51              
52             __END__