blib/lib/WWW/Ohloh/API/Projects.pm

filter: Statementstmt 52.5% 42 / 80 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 78.5% 11 / 14 Podpod 66.6% 2 / 3 totaltotal 44.7% 55 / 123 CC 12 SCAR 28.9
CC
12
Cov
41%
next
42.0
_gather_more
30.0
all
12.0
CRAP 18.0
↑ Summary blib/lib/WWW/Ohloh/API.pm »
1 package WWW::Ohloh::API::Projects;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Object::InsideOut;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use List::MoreUtils qw/ none any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶use overload '<>' => \&next;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 15 our $VERSION = '0.3.2';
15  
16 15 my @all_read : Field : Default(0);
17 15 my @cache_of : Field;
18 15 my @total_entries_of : Field : Default(-1);
19 15 my @ohloh_of : Field : Arg(ohloh);
20 15 my @page_of : Field : Default(0);
21 15 my @query_of : Field : Arg(query);
22 15 my @sort_order_of : Field : Arg(sort) :
23 Type(\&WWW::Ohloh::API::Projects::is_allowed_sort);
24 15 my @max_entries_of : Field : Arg(max) : Get(max);
25  
26 15 Readonly our @ALLOWED_SORTING => map { $_, $_ . '_reverse' }
27 qw/ created_at description id name stack_count updated_at /;
28  
29 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30  
31 sub is_allowed_sort {
32 1 ▶ my $s = shift;
Subroutine is_allowed_sort: called CC=1 SCAR=0.0
Pod undocumented
33 1 ▶ return any { $s eq $_ } @ALLOWED_SORTING;
Subroutine __ANON__: called CC=1 SCAR=0.0
34 }
35  
36 sub _init : Init {
37 my $self = shift;
38  
39 $cache_of[$$self] = []; # initialize to empty array
40  
41 if ( my $s = $sort_order_of[$$self] ) {
42 if ( none { $s eq $_ } @ALLOWED_SORTING ) {
43 croak "sorting order given: $s, must be one of the following: "
44 . join ', ' => @ALLOWED_SORTING;
45 }
46 }
47 15 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50  
51 sub next {
52 0 ▶ my $self = shift;
Subroutine next: not called CC=6 SCAR=37.4
Pod documented
53 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
54  
55 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
56 and not $all_read[$$self] ) {
57 0 $self->_gather_more;
58 }
59  
60 0 my @bunch = splice @{ $cache_of[$$self] }, 0, $nbr_requested;
61  
62 0 ▶ if (@bunch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bunch) 0 0 0
63 0 ▶ return wantarray ? @bunch : $bunch[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
64 }
65  
66 # we've nothing else to return
67  
68 0 $page_of[$$self] = 0;
69 0 $all_read[$$self] = 0;
70  
71 0 return;
72 }
73  
74 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75  
76 sub _gather_more {
77 0 ▶ my $self = shift;
Subroutine _gather_more: not called CC=5 SCAR=34.0
78  
79 0 my ( $url, $xml ) = $ohloh_of[$$self]->_query_server(
80 'projects.xml',
81 { ( query => $query_of[$$self] ) x !!$query_of[$$self],
82 ( sort => $sort_order_of[$$self] ) x !!$sort_order_of[$$self],
83 page => ++$page_of[$$self] } );
84  
85 my @new_batch =
86 map {
87 0 WWW::Ohloh::API::Project->new(
88 ohloh => $ohloh_of[$$self],
89 xml => $_,
90 )
91 } $xml->findnodes('project');
92  
93 # get total projects + where we are
94  
95 0 $total_entries_of[$$self] =
96 $xml->findvalue('/response/items_available/text()');
97  
98 0 my $first_item = $xml->findvalue('/response/first_item_position/text()');
99  
100 0 ▶ $all_read[$$self] = 1 unless $total_entries_of[$$self];
Branch: 1 branch
BranchTrueFalseTotal
unless $total_entries_of[$$self] 0 0 0
101  
102 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
103 0 $all_read[$$self] = 1;
104 }
105  
106 0 ▶ if ( my $max = $self->max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max = $self->max) 0 0 0
107 0 ▶ if ( $first_item + @new_batch - 1 >= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_item + @new_batch - 1 >= $max) 0 0 0
108 0 @new_batch = splice @new_batch, 0, $max - $first_item;
109 0 $all_read[$$self] = 1;
110 }
111 }
112  
113 0 push @{ $cache_of[$$self] }, @new_batch;
114  
115 0 return;
116 }
117  
118 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119  
120 sub all {
121 0 ▶ my $self = shift;
Subroutine all: not called CC=3 SCAR=24.8
Pod documented
122  
123 0 ▶ unless ( $self->max ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->max) 0 0 0
124 0 croak "call to all only permitted if 'max' set in get_projects()";
125 }
126  
127 0 ▶ $self->_gather_more until ( $all_read[$$self] );
Branch: 1 branch
BranchTrueFalseTotal
$all_read[$$self] or do { $self->_gather_more } 0 0 0
128  
129 0 my @bunch = @{ $cache_of[$$self] };
130 0 $cache_of[$$self] = [];
131  
132 0 $page_of[$$self] = 0;
133 0 $all_read[$$self] = 0;
134  
135 0 return @bunch;
136 }
137  
138 15 'end of WWW::Ohloh::API::Projects';
139 __END__
140  
141 =head1 NAME
142  
143 WWW::Ohloh::API::Projects - a set of Ohloh projects
144  
145 =head1 SYNOPSIS
146  
147 use WWW::Ohloh::API;
148  
149 my $ohloh = WWW::Ohloh::API->new( api_key => $my_api_key );
150 my $projects = $ohloh->get_projects( query => 'Ohloh', max => 100 );
151  
152 while ( my $p = $projects->next ) {
153 print $p->name;
154 }
155  
156 =head1 DESCRIPTION
157  
158 W::O::A::Projects returns the results of an Ohloh project query.
159 To be properly populated, it must be created via
160 the C<get_projects> method of a L<WWW::Ohloh::API> object.
161  
162 The results of a query are not all captured at the call of
163 <get_projects>, but are retrieved from Ohloh as required, usually
164 by batches of 25 items.
165  
166 =head1 METHODS
167  
168 =head2 next( [ $n ] )
169  
170 Return the next project of the set or, if I<$n> is given,
171 the I<$n> following projects (or all remaining projects if they
172 are less than I<$n>). When all projects have been returned, C<next()>
173 returns C<undef> once, and then restarts from the beginning.
174  
175 Examples:
176  
177 # typical iterator
178 while( my $project = $projects->next ) {
179 print $project->name;
180 }
181  
182 # read projects 10 at a time
183 while( my @p = $project->next( 10 ) ) {
184 # do something with them...
185 }
186  
187 =head2 all
188  
189 Return all the remaining projects of the set. A call
190 to C<all()> immediatly reset the set. I.e., a subsequent call
191 to C<next()> would return the first project of the set, not I<undef>.
192  
193 As a precaution against
194 memory meltdown, calls to C<all()> are not permited unless the parameter
195 I<max> of C<get_projects> has been set.
196  
197 Example:
198  
199 my $projects = $ohloh->get_projects( sort => 'stack_count', max => 100 );
200 # get the first project special
201 $top = $projects->next;
202 # get the rest
203 @most_stacked = $projects->all;
204  
205 =head1 OVERLOADING
206  
207 =head2 Iteration
208  
209 If called on a W:O:A:Projects object, the iteration operator (<>) acts
210 as a call to '$projects->next'.
211  
212 E.g.,
213  
214 while( my $p = <$projects> ) {
215 ### do stuff with $p
216 }
217  
218 =head1 SEE ALSO
219  
220 =over
221  
222 =item *
223  
224 L<WWW::Ohloh::API>,
225 L<WWW::Ohloh::API::Project>,
226 L<WWW::Ohloh::API::Analysis>,
227 L<WWW::Ohloh::API::Account>.
228  
229  
230 =item *
231  
232 Ohloh API reference: http://www.ohloh.net/api/getting_started
233  
234 =item *
235  
236 Ohloh Account API reference: http://www.ohloh.net/api/reference/project
237  
238 =back
239  
240 =head1 VERSION
241  
242 This document describes WWW::Ohloh::API version 0.3.2
243  
244 =head1 BUGS AND LIMITATIONS
245  
246 WWW::Ohloh::API is very extremely alpha quality. It'll improve,
247 but till then: I<Caveat emptor>.
248  
249 Please report any bugs or feature requests to
250 C<bug-www-ohloh-api@rt.cpan.org>, or through the web interface at
251 L<http://rt.cpan.org>.
252  
253  
254 =head1 AUTHOR
255  
256 Yanick Champoux C<< <yanick@cpan.org> >>
257  
258 =head1 LICENCE AND COPYRIGHT
259  
260 Copyright (c) 2008, Yanick Champoux C<< <yanick@cpan.org> >>. All rights reserved.
261  
262 This module is free software; you can redistribute it and/or
263 modify it under the same terms as Perl itself. See L<perlartistic>.
264  
265  
266  
↑ Summary blib/lib/WWW/Ohloh/API.pm »