API/Projects.pm

filter: Statementstmt 49.4% 43 / 87 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 68.7% 11 / 16 Podpod 66.6% 2 / 3 totaltotal 41.1% 56 / 136 CC 14 SCAR 28.2
CC
14
Cov
38%
next
42.0
_gather_more
30.0
_init
12.0
CRAP 16.8
↑ Summary API.pm »
1 package WWW::Ohloh::API::Projects;
2 29 our $AUTHORITY = 'cpan:YANICK';
3  
4 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use Object::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use List::MoreUtils qw/ none any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 29 ▶use overload '<>' => \&next;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 29 our $VERSION = '1.0_1';
16  
17 29 my @all_read : Field : Default(0);
18 29 my @cache_of : Field;
19 29 my @total_entries_of : Field : Default(-1);
20 29 my @ohloh_of : Field : Arg(ohloh);
21 29 my @page_of : Field : Default(0);
22 29 my @query_of : Field : Arg(query);
23 29 my @sort_order_of : Field : Arg(sort) :
24 Type(\&WWW::Ohloh::API::Projects::is_allowed_sort);
25 29 my @max_entries_of : Field : Arg(max) : Get(max);
26  
27 29 Readonly our @ALLOWED_SORTING => map { $_, $_ . '_reverse' }
28 qw/ created_at description id name stack_count updated_at /;
29  
30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31  
32 1 ▶sub is_allowed_sort {
Subroutine is_allowed_sort: called CC=1 SCAR=0.0
Pod undocumented
33 1 my $s = shift;
34 1 ▶ return any { $s eq $_ } @ALLOWED_SORTING;
Subroutine __ANON__: called CC=1 SCAR=0.0
35 }
36  
37 0 ▶sub _init : Init {
Subroutine _init: not called CC=3 SCAR=24.8
38 0 my $self = shift;
39  
40 0 $cache_of[$$self] = []; # initialize to empty array
41  
42 0 ▶ if ( my $s = $sort_order_of[$$self] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $sort_order_of[$$self]) 0 0 0
43 0 ▶ if ( none { $s eq $_ } @ALLOWED_SORTING ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $s eq $_; } , @WWW::Ohloh::API::Projects::ALLOWED_SORTING)) 0 0 0
44 0 croak "sorting order given: $s, must be one of the following: "
45 . join ', ' => @ALLOWED_SORTING;
46 }
47 }
48 29 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51  
52 0 ▶sub next {
Subroutine next: not called CC=6 SCAR=37.4
Pod documented
53 0 my $self = shift;
54 0 ▶ my $nbr_requested = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
55  
56 0 ▶ while ( @{ $cache_of[$$self] } < $nbr_requested
Branch: 1 branch
BranchTrueFalseTotal
if (@{$cache_of[$$self];} < $nbr_requested and not $all_read[$$self]) 0 0 0
Condition0%0/3
@{$cache_of[$$self];} < $nbr_requested and not $all_read[$$self]
!ll&&!rl&&r
000
MC/DC0%0/2
@{$cache_of[$$self];} < $nbr_requested and not $all_read[$$self]
@{$cache_of[$$self];} < $nbr_requested not $all_read[$$self]
Truth table0%0/3
A and not(B)
A= @{$cache_of[$$self];} < $nbr_requestedB= not $all_read[$$self]
ABresult
0X0
100
111
57 and not $all_read[$$self] ) {
58 0 $self->_gather_more;
59 }
60  
61 0 my @bunch = splice @{ $cache_of[$$self] }, 0, $nbr_requested;
62  
63 0 ▶ if (@bunch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bunch) 0 0 0
64 0 ▶ return wantarray ? @bunch : $bunch[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
65 }
66  
67 # we've nothing else to return
68  
69 0 $page_of[$$self] = 0;
70 0 $all_read[$$self] = 0;
71  
72 0 return;
73 }
74  
75 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76  
77 0 ▶sub _gather_more {
Subroutine _gather_more: not called CC=5 SCAR=34.0
78 0 my $self = shift;
79  
80 0 my ( $url, $xml ) = $ohloh_of[$$self]->_query_server(
81 'projects.xml',
82 { ( query => $query_of[$$self] ) x !!$query_of[$$self],
83 ( sort => $sort_order_of[$$self] ) x !!$sort_order_of[$$self],
84 page => ++$page_of[$$self] } );
85  
86 my @new_batch =
87 map {
88 0 WWW::Ohloh::API::Project->new(
89 ohloh => $ohloh_of[$$self],
90 xml => $_,
91 )
92 } $xml->findnodes('project');
93  
94 # get total projects + where we are
95  
96 0 $total_entries_of[$$self] =
97 $xml->findvalue('/response/items_available/text()');
98  
99 0 my $first_item = $xml->findvalue('/response/first_item_position/text()');
100  
101 0 ▶ $all_read[$$self] = 1 unless $total_entries_of[$$self];
Branch: 1 branch
BranchTrueFalseTotal
unless $total_entries_of[$$self] 0 0 0
102  
103 0 ▶ if ( $first_item + @new_batch - 1 >= $total_entries_of[$$self] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_item + @new_batch - 1 >= $total_entries_of[$$self]) 0 0 0
104 0 $all_read[$$self] = 1;
105 }
106  
107 0 ▶ if ( my $max = $self->max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max = $self->max) 0 0 0
108 0 ▶ if ( $first_item + @new_batch - 1 >= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_item + @new_batch - 1 >= $max) 0 0 0
109 0 @new_batch = splice @new_batch, 0, $max - $first_item;
110 0 $all_read[$$self] = 1;
111 }
112 }
113  
114 0 push @{ $cache_of[$$self] }, @new_batch;
115  
116 0 return;
117 }
118  
119 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120  
121 0 ▶sub all {
Subroutine all: not called CC=3 SCAR=24.8
Pod documented
122 0 my $self = shift;
123  
124 0 ▶ unless ( $self->max ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->max) 0 0 0
125 0 croak "call to all only permitted if 'max' set in get_projects()";
126 }
127  
128 0 ▶ $self->_gather_more until ( $all_read[$$self] );
Branch: 1 branch
BranchTrueFalseTotal
$all_read[$$self] or do { $self->_gather_more } 0 0 0
129  
130 0 my @bunch = @{ $cache_of[$$self] };
131 0 $cache_of[$$self] = [];
132  
133 0 $page_of[$$self] = 0;
134 0 $all_read[$$self] = 0;
135  
136 0 return @bunch;
137 }
138  
139 29 'end of WWW::Ohloh::API::Projects';
140  
141 __END__
↑ Summary API.pm »