CPANIDX/Tie.pm

filter: Statementstmt 36.1% 43 / 119 Branchbran 0.0% 0 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 63.6% 14 / 22 Podpod n/a totaltotal 32.2% 57 / 177 CC 20 SCAR 40.3
CC
20
Cov
28%
FETCH
156.0
FIRSTKEY
56.0
TIEHASH
6.0
CRAP 56.3
« CPANIDX/HTTP.pm ↑ Summary CPANIDX.pm »
1 package CPANPLUS::Internals::Source::CPANIDX::Tie;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $CPANPLUS::Internals::Source::CPANIDX::Tie::VERSION = '0.04';
4 }
5  
6 #ABSTRACT: A tie for the CPANIDX source engine
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use CPANPLUS::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use CPANPLUS::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use CPANPLUS::Module::Fake;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use CPANPLUS::Module::Author::Fake;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use CPANPLUS::Internals::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 ▶use Params::Check qw[check];
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Module::Load::Conditional qw[can_load];
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use CPANPLUS::Internals::Source::CPANIDX::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Parse::CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 require Tie::Hash;
26 2 ▶use vars qw[@ISA];
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 push @ISA, 'Tie::StdHash';
28  
29 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=2 SCAR=17.9
30 0 my $class = shift;
31 0 my %hash = @_;
32
33 0 my $tmpl = {
34 idx => { required => 1 },
35 table => { required => 1 },
36 key => { required => 1 },
37 cb => { required => 1 },
38 offset => { default => 0 },
39 };
40
41 0 ▶ my $args = check( $tmpl, \%hash ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $args = check($tmpl, \%hash) or return 0 0 0
42 0 my $obj = bless { %$args, store => {} } , $class;
43  
44 0 return $obj;
45 }
46  
47 0 ▶sub FETCH {
Subroutine FETCH: not called CC=12 SCAR=50.5
48 0 my $self = shift;
49 0 ▶ my $key = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $key = shift() or return 0 0 0
50 0 my $idx = $self->{idx};
51 0 my $cb = $self->{cb};
52 0 my $table = $self->{table};
53
54 0 ▶ my $lkup = $table eq 'module' ? 'mod' : 'auth';
Branch: 1 branch
BranchTrueFalseTotal
$table eq 'module' ? : 0 0 0
55
56 ### did we look this one up before?
57 0 ▶ if( my $obj = $self->{store}->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->{"store"}{$key}) 0 0 0
58 0 return $obj;
59 }
60
61 0 my $url = $self->{idx} . "yaml/$lkup/" . $key;
62 0 my $str;
63  
64 0 my $http = CPANPLUS::Internals::Source::CPANIDX::HTTP->new();
65  
66 0 ▶ my $status = $http->request( $url ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $status = $http->request($url) or return 0 0 0
67 0 ▶ return unless $status eq '200';
Branch: 1 branch
BranchTrueFalseTotal
unless $status eq 200 0 0 0
68 0 ▶ return unless $str = $http->body;
Branch: 1 branch
BranchTrueFalseTotal
unless $str = $http->body 0 0 0
69  
70 0 my $res;
71 0 eval { $res = Parse::CPAN::Meta::Load( $str ); };
72 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
73  
74 0 my $href = $res->[0];
75
76 ### no results?
77 0 ▶ return unless keys %$href;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$href 0 0 0
78
79 ### expand author if needed
80 ### XXX no longer generic :(
81 0 ▶ if( $table eq 'module' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table eq 'module') { } 0 0 0
82 0 $href->{author} = delete $href->{cpan_id};
83 0 $href->{module} = delete $href->{mod_name};
84 0 $href->{version} = delete $href->{mod_vers};
85 my ($author, $package) = $href->{dist_file} =~
86 0 m| (?:[A-Z\d-]/)?
87 (?:[A-Z\d-]{2}/)?
88 ([A-Z\d-]+) (?:/[\S]+)?/
89 ([^/]+)$
90 |xsg;
91  
92 ### remove file name from the path
93 0 $href->{dist_file} =~ s|/[^/]+$||;
94 0 $href->{path} = join '/', 'authors/id', delete $href->{dist_file};
95 0 $href->{package} = $package;
96 0 $href->{comment} = $href->{description} = $href->{dslip} = $href->{mtime} = '';
97 0 delete $href->{$_} for qw(dist_vers dist_name);
98 0 ▶ $href->{author} = $cb->author_tree( $href->{author} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$href->{"author"} = $cb->author_tree($href->{"author"}) or return 0 0 0
99 }
100 else {
101 0 $href->{author} = delete $href->{fullname};
102 0 $href->{cpanid} = delete $href->{cpan_id};
103 }
104  
105 my $class = {
106 module => 'CPANPLUS::Module',
107 author => 'CPANPLUS::Module::Author',
108 0 }->{ $table };
109  
110 0 my $obj = $self->{store}->{$key} = $class->new( %$href, _id => $cb->_id );
111
112 0 return $obj;
113 }
114  
115 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
116 0 my $self = shift;
117 0 my $key = shift;
118 0 my $val = shift;
119
120 0 $self->{store}->{$key} = $val;
121 }
122  
123 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=7 SCAR=40.3
124 0 my $self = shift;
125 0 my $idx = $self->{'idx'};
126 0 my $table = $self->{table};
127  
128 0 ▶ my $lkup = $table eq 'module' ? 'mod' : 'auth';
Branch: 1 branch
BranchTrueFalseTotal
$table eq 'module' ? : 0 0 0
129 0 my $url = $idx . "yaml/${lkup}keys";
130  
131 0 my $str;
132  
133 0 my $http = CPANPLUS::Internals::Source::CPANIDX::HTTP->new();
134  
135 0 ▶ my $status = $http->request( $url ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $status = $http->request($url) or return 0 0 0
136 0 ▶ return unless $status eq '200';
Branch: 1 branch
BranchTrueFalseTotal
unless $status eq 200 0 0 0
137 0 ▶ return unless $str = $http->body;
Branch: 1 branch
BranchTrueFalseTotal
unless $str = $http->body 0 0 0
138  
139 0 my $res;
140 0 eval { $res = Parse::CPAN::Meta::Load( $str ); };
141 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
142  
143 0 ▶ my $ref = $table eq 'module' ? 'mod_name' : 'cpan_id';
Branch: 1 branch
BranchTrueFalseTotal
$table eq 'module' ? : 0 0 0
144 0 @{ $self->{keys} } =
145 0 map { $_->{$ref} } @$res;
146  
147 0 $self->{offset} = 0;
148  
149 0 return $self->{keys}->[0];
150 }
151  
152 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
153 0 my $self = shift;
154 0 my $idx = $self->{'idx'};
155 0 my $table = $self->{table};
156  
157 0 my $key = $self->{keys}->[ $self->{offset} ];
158
159 0 $self->{offset} +=1;
160  
161 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
162 ### use each() semantics
163 0 my $val = $self->FETCH( $key );
164 0 return ( $key, $val );
165 }
166 0 return $key;
167 }
168  
169 0 ▶sub EXISTS { !!$_[0]->FETCH( $_[1] ) }
Subroutine EXISTS: not called CC=1 SCAR=6.9
170  
171 ### intentionally left blank
172 0 ▶sub DELETE { }
Subroutine DELETE: not called CC=1 SCAR=0.0
173 0 ▶sub CLEAR { }
Subroutine CLEAR: not called CC=1 SCAR=0.0
174  
175 2 qq[Tie your mother down];
176  
177  
178 __END__
« CPANIDX/HTTP.pm ↑ Summary CPANIDX.pm »