File Coverage

blib/lib/Zeal/Feed.pm
Criterion Covered Total %
statement 34 36 94.4
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 46 48 95.8


line stmt bran cond sub pod time code
1             package Zeal::Feed;
2              
3 1     1   14703 use 5.014000;
  1         3  
  1         29  
4 1     1   4 use strict;
  1         1  
  1         29  
5 1     1   4 use warnings;
  1         5  
  1         24  
6 1     1   4 use re '/s';
  1         1  
  1         79  
7              
8             our $VERSION = '0.001001';
9              
10 1     1   427 use parent qw/Class::Accessor::Fast/;
  1         241  
  1         4  
11             __PACKAGE__->mk_ro_accessors(qw/version/);
12              
13 1     1   2789 use Cwd qw/getcwd/;
  1         2  
  1         55  
14 1     1   436 use File::Spec::Functions qw/catfile rel2abs/;
  1         599  
  1         58  
15 1     1   709 use HTTP::Tiny;
  1         38406  
  1         42  
16              
17 1     1   689 use Archive::Tar;
  1         106152  
  1         66  
18 1     1   552 use File::Slurp qw/read_file/;
  1         3556  
  1         54  
19 1     1   404 use File::Which;
  1         759  
  1         43  
20 1     1   192 use XML::Rules;
  0            
  0            
21              
22             sub new {
23             my ($class, $url) = @_;
24             $class->new_from_content(HTTP::Tiny->new->get($url)->{content});
25             }
26              
27             sub new_from_file {
28             my ($class, $file) = @_;
29             $class->new_from_content(scalar read_file $file);
30             }
31              
32             sub new_from_content {
33             my ($class, $xml) = @_;
34             my ($version, @urls) = @_;
35              
36             my $self = XML::Rules->parse(
37             rules => {
38             _default => 'content',
39             entry => 'pass',
40             url => 'content array',
41             'other-versions' => undef,
42             },
43             stripspaces => 3|4,
44             )->($xml);
45             bless $self, $class
46             }
47              
48             sub urls {
49             my ($self) = @_;
50             @{$self->{url}}
51             }
52              
53             sub url {
54             my ($self) = @_;
55             my @urls = $self->urls;
56             $urls[int rand @urls]
57             }
58              
59             sub _unpack_tar_to_dir {
60             my ($file, $dir) = @_;
61             $file = rel2abs $file;
62             my $oldwd = getcwd;
63             chdir $dir;
64             my $tar = which 'tar' or which 'gtar';
65              
66             # uncoverable branch true
67             # uncoverable condition false
68             local $ENV{ZEAL_USE_INTERNAL_TAR} = 1 if $file =~ /gz$|bz2$/ && $^O eq 'solaris';
69              
70             if ($tar && !$ENV{ZEAL_USE_INTERNAL_TAR}) {
71             my $arg = '-xf';
72             $arg = '-xzf' if $file =~ /[.]t?gz$/;
73             $arg = '-xjf' if $file =~ /[.]bz2$/;
74             system $tar, $arg => $file
75             } else {
76             Archive::Tar->extract_archive($file);
77             }
78             chdir $oldwd;
79             }
80              
81             sub download {
82             my ($self, $path) = @_;
83             my ($name) = $self->url =~ /([^\/])+$/;
84             my $file = catfile $path, $name;
85             HTTP::Tiny->new->mirror($self->url, $file);
86             _unpack_tar_to_dir $file, $path;
87             unlink $file;
88             }
89              
90             1;
91             __END__