API/Role/Readable.pm

filter: Statementstmt 62.1% 41 / 66 Branchbran 6.2% 1 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 12 / 16 Podpod 100% 5 / 5 totaltotal 57.2% 59 / 103 CC 10 SCAR 23.2
CC
10
Cov
51%
read_from_tarball
30.0
read_from_repo_uri
20.0
read_from_string
3.5
CRAP 10.2
↑ Summary API.pm »
1 package CPAN::Index::API::Role::Readable;
2  
3 3 our $VERSION = '0.008';
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Compress::Zlib qw(gzopen Z_STREAM_END), '$gzerrno';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 requires 'parse';
18 3 requires 'default_location';
19  
20 sub read_from_string
21 5 ▶{
Subroutine read_from_string: called CC=3 SCAR=12.5
Pod documented
22 5 my ($self, $content, %args) = @_;
23  
24 5 %args = ( $self->parse($content), %args );
25  
26 5 ▶ if ( blessed $self )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 0 5 5
27 {
28 0 foreach my $key ( keys %args )
29 {
30 0 $self->$key($args{$key});
31 }
32 }
33 else
34 {
35 5 return $self->new(%args);
36 }
37 }
38  
39 0 ▶sub read_from_file {
Subroutine read_from_file: not called CC=1 SCAR=6.9
Pod documented
40 0 my ($self, $file, %args) = @_;
41 0 my $content = path($file)->slurp_utf8;
42 0 return $self->read_from_string($content, %args);
43 }
44  
45 sub read_from_tarball
46 0 ▶{
Subroutine read_from_tarball: not called CC=5 SCAR=34.0
Pod documented
47 0 my ($self, $tarball, %args) = @_;
48  
49 0 ▶ my $gz = gzopen($tarball, 'rb') or croak "Cannot open $tarball: $gzerrno";
Branch: 1 branch
BranchTrueFalseTotal
my $gz = &gzopen($tarball, "rb") or croak("Cannot open ${tarball}: $CPAN::Index::API::Role::Readable::gzerrno") 0 0 0
50  
51 0 my ($buffer, $content);
52  
53 0 ▶ $content .= $buffer while $gz->gzread($buffer) > 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$gz->gzread($buffer) > 0 and do { $content .= $buffer } 0 0 0
54  
55 0 ▶ croak "Error reading from $tarball: $gzerrno" . ($gzerrno+0) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $CPAN::Index::API::Role::Readable::gzerrno != Z_STREAM_END() 0 0 0
56 if $gzerrno != Z_STREAM_END;
57  
58 0 ▶ $gz->gzclose and croak "Error closing $tarball";
Branch: 1 branch
BranchTrueFalseTotal
$gz->gzclose and croak("Error closing $tarball") 0 0 0
59  
60 0 return $self->read_from_string($content, %args);
61 }
62  
63 sub read_from_repo_path
64 0 ▶{
Subroutine read_from_repo_path: not called CC=1 SCAR=6.9
Pod documented
65 0 my ($self, $repo_path, %args) = @_;
66  
67 0 $args{repo_path} = $repo_path;
68  
69 0 return $self->read_from_tarball(
70 file( $repo_path, $self->default_location )->stringify, %args
71 );
72 }
73  
74 sub read_from_repo_uri
75 0 ▶{
Subroutine read_from_repo_uri: not called CC=4 SCAR=30.0
Pod documented
76 0 my ($self, $repo_uri, %args) = @_;
77  
78 0 $args{repo_uri} = $repo_uri;
79  
80 0 my $uri = URI->new( $repo_uri );
81  
82 0 $uri->path_segments(
83 $uri->path_segments,
84 file($self->default_location)->dir->dir_list,
85 file($self->default_location)->basename,
86 );
87  
88 0 my $uri_as_string = $uri->as_string;
89  
90 0 ▶ my $content = LWP::Simple::get( $uri_as_string )
Branch: 1 branch
BranchTrueFalseTotal
my $content = &LWP::Simple::get($uri_as_string) or croak("Failed to fetch $uri_as_string") 0 0 0
91 or croak "Failed to fetch $uri_as_string";
92  
93 0 my ( $fh, $filename ) = tempfile;
94 0 ▶ print $fh LWP::Simple::get( $uri->as_string ) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh &LWP::Simple::get(scalar $uri->as_string) or croak($!) 0 0 0
95 0 ▶ close $fh or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak($!) 0 0 0
96  
97 0 return $self->read_from_tarball( $filename, %args );
98 }
99  
100 3 1;
101  
102 =pod
103  
104 =encoding UTF-8
105  
106 =head1 NAME
107  
108 CPAN::Index::Role::Readable - Reads index files
109  
110 =head1 DESCRIPTION
111  
112 This role provides a collection of utility constructors for CPAN index file
113 objects.
114  
115 =head1 REQUIRES
116  
117 =head2 default_location
118  
119 Class method that returns a string specifying the path to the default location
120 of this file relative to the repository root.
121  
122 =head2 parse
123  
124 This class method (generally invoked as part of the construction phase)
125 should accept a string containing an index file, and return a list of
126 key/value pairs suitable for passing to the constructor of the consuming class.
127  
128 =head1 PROVIDES
129  
130 =head2 read_from_string
131  
132 Construct a new index file object by reading the file contents from a string.
133  
134 =head2 read_from_file
135  
136 Construct a new index file object by reading the file contents from a filename.
137  
138 =head2 read_from_tarball
139  
140 Construct a new index file object by reading the file contents from a tarball.
141  
142 =head2 read_from_repo_path
143  
144 Construct a new index file object by locating and parsing a file in a local
145 repository.
146  
147 =head2 read_from_repo_uri
148  
149 Construct a new index file object by locating and parsing a file in a remote
150 repository.
↑ Summary API.pm »