API/Role/Readable.pm

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