File Coverage

blib/lib/WWW/Ohloh/API.pm
Criterion Covered Total %
statement 114 158 72.1
branch 1 22 4.5
condition 0 5 0.0
subroutine 34 39 87.1
pod 12 15 80.0
total 161 239 67.3


line stmt bran cond sub pod time code
1             package WWW::Ohloh::API;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Ohloh API implementation (DEPRECATED)
4              
5 28     28   4100773 use warnings;
  28         61  
  28         2006  
6 28     28   236 use strict;
  28         51  
  28         683  
7 28     28   133 use Carp;
  28         55  
  28         1966  
8              
9 28     28   14488 use Object::InsideOut;
  28         1121536  
  28         315  
10              
11 28     28   26406 use LWP::UserAgent;
  28         1836372  
  28         1619  
12 28     28   17821 use Readonly;
  28         142616  
  28         2500  
13 28     28   21661 use XML::LibXML;
  28         1915350  
  28         307  
14 28     28   23118 use Params::Validate qw(:all);
  28         228445  
  28         6606  
15              
16 28     28   18062 use WWW::Ohloh::API::Account;
  28         147  
  28         301  
17 28     28   19358 use WWW::Ohloh::API::Analysis;
  28         106  
  28         231  
18 28     28   18721 use WWW::Ohloh::API::Project;
  28         127  
  28         257  
19 28     28   18553 use WWW::Ohloh::API::Projects;
  28         170  
  28         318  
20 28     28   19561 use WWW::Ohloh::API::Languages;
  28         121  
  28         345  
21 28     28   17672 use WWW::Ohloh::API::ActivityFact;
  28         119  
  28         215  
22 28     28   17344 use WWW::Ohloh::API::ActivityFacts;
  28         114  
  28         213  
23 28     28   17504 use WWW::Ohloh::API::Kudos;
  28         129  
  28         202  
24 28     28   18114 use WWW::Ohloh::API::ContributorLanguageFact;
  28         110  
  28         227  
25 28     28   17366 use WWW::Ohloh::API::Enlistments;
  28         119  
  28         237  
26 28     28   18022 use WWW::Ohloh::API::Factoid;
  28         115  
  28         219  
27 28     28   17795 use WWW::Ohloh::API::SizeFact;
  28         141  
  28         208  
28              
29 28     28   2520 use Digest::MD5 qw/ md5_hex /;
  28         58  
  28         63112  
30              
31             our $VERSION = '1.0_1';
32              
33             Readonly our $OHLOH_URL => 'http://www.ohloh.net/';
34              
35             our $useragent_signature = "WWW-Ohloh-API/$VERSION";
36              
37             my @api_key_of : Field : Std(api_key) : Arg(api_key);
38             my @api_version_of : Field : Default(1) : Std(api_version)
39             ; # for now, there's only v1
40              
41             my @user_agent_of : Field;
42              
43             my @debugging : Field : Arg(debug) : Default(0) : Std(debug);
44              
45             my @parser_of : Field;
46              
47             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48              
49             sub fetch_messages {
50 1     1 1 535959 my $self = shift;
51              
52 1         516 require WWW::Ohloh::API::Messages;
53              
54 1         23 return WWW::Ohloh::API::Messages->new( ohloh => $self, @_ );
55             }
56              
57             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58              
59             sub fetch_account_stack {
60 1     1 1 65 my $self = shift;
61              
62 1         3 my $id = shift;
63              
64 1         850 require WWW::Ohloh::API::Stack;
65              
66 1         15 return WWW::Ohloh::API::Stack->fetch(
67             ohloh => $self,
68             id => $id
69             );
70              
71 0 0       0 $id = md5_hex($id) if -1 < index $id, '@'; # it's an email
72              
73 0         0 my ( $url, $xml ) =
74             $self->_query_server("accounts/$id/stacks/default.xml");
75              
76 0         0 return WWW::Ohloh::API::Stack->new(
77             ohloh => $self,
78             request_url => $url,
79             xml => $xml->findnodes('stack[1]'),
80             );
81             }
82              
83             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84              
85             sub fetch_project_stacks {
86 1     1 1 629133 my $self = shift;
87              
88 1         4 my $project = shift;
89              
90 1         8 my ( $url, $xml ) = $self->_query_server("projects/$project/stacks.xml");
91              
92 1         681 require WWW::Ohloh::API::Stack;
93              
94             return map {
95 1         25 WWW::Ohloh::API::Stack->new(
  4         729  
96             ohloh => $self,
97             request_url => $url,
98             xml => $_,
99             )
100             } $xml->findnodes('//result/stack');
101             }
102              
103             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104              
105             sub fetch_size_facts {
106 1     1 1 618903 my $self = shift;
107              
108 1         34 my ( $project_id, $analysis_id ) =
109             validate_pos( @_, 1, { default => 'latest' }, );
110              
111 1         9 my ( $url, $xml ) = $self->_query_server(
112             "projects/$project_id/analyses/$analysis_id/size_facts.xml");
113              
114             return map {
115 1         21 WWW::Ohloh::API::SizeFact->new(
  73         21682  
116             ohloh => $self,
117             request_url => $url,
118             xml => $_
119             )
120             } $xml->findnodes('//size_fact');
121              
122             }
123              
124             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125              
126             sub fetch_account {
127 5     5 1 597660 my ( $self, $id ) = @_;
128              
129 5         43 require WWW::Ohloh::API::Account;
130              
131 5         52 return WWW::Ohloh::API::Account->fetch(
132             ohloh => $self,
133             id => $id,
134             );
135             }
136              
137             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138              
139             sub fetch_enlistments {
140 1     1 1 550155 my $self = shift;
141 1         4 my %arg = @_;
142              
143             return WWW::Ohloh::API::Enlistments->new(
144             ohloh => $self,
145             project_id => $arg{project_id},
146             ( sort => $arg{sort} ) x !!$arg{sort},
147 1         7 );
148             }
149              
150             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151              
152             sub fetch_factoids {
153 2     2 0 371919 my $self = shift;
154              
155 2         6 my $project_id = shift;
156              
157 2         15 my ( $url, $xml ) =
158             $self->_query_server("projects/$project_id/factoids.xml");
159              
160             return map {
161 2         37 WWW::Ohloh::API::Factoid->new(
  8         313  
162             ohloh => $self,
163             request_url => $url,
164             xml => $_
165             )
166             } $xml->findnodes('//factoid');
167             }
168              
169             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170              
171             sub fetch_contributor_language_facts {
172 1     1 1 529997 my $self = shift;
173              
174 1         29 my %param = validate(
175             @_,
176             { project_id => 1,
177             contributor_id => 1,
178             } );
179              
180 1         13 my ( $url, $xml ) = $self->_query_server(
181             "projects/$param{project_id}/contributors/$param{contributor_id}.xml"
182             );
183              
184             return map {
185 1         21 WWW::Ohloh::API::ContributorLanguageFact->new(
  7         981  
186             ohloh => $self,
187             request_url => $url,
188             xml => $_
189             )
190             } $xml->findnodes('//contributor_language_fact');
191              
192             }
193              
194             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195              
196             sub fetch_kudos {
197 2     2 1 624797 my $self = shift;
198 2         7 my ($id) = @_;
199              
200 2 50       10 $id = md5_hex($id) if -1 < index $id, '@';
201              
202 2         15 return WWW::Ohloh::API::Kudos->new(
203             ohloh => $self,
204             id => $id,
205             );
206             }
207              
208             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209              
210             sub fetch_project {
211 2     2 1 1105059 my $self = shift;
212 2         6 my $id = shift;
213              
214 2         13 my ( $url, $xml ) = $self->_query_server("projects/$id.xml");
215              
216 2         36 return WWW::Ohloh::API::Project->new(
217             ohloh => $self,
218             request_url => $url,
219             xml => $xml->findnodes('project[1]'),
220             );
221             }
222              
223             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
224              
225             sub fetch_projects {
226 1     1 1 376778 my $self = shift;
227 1         30 my %arg = validate( @_, { query => 0, sort => 0, max => 0 } );
228              
229             return WWW::Ohloh::API::Projects->new(
230             ohloh => $self,
231             query => $arg{query},
232             sort => $arg{sort},
233             max => $arg{max},
234 1         18 );
235             }
236              
237             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
238              
239             sub fetch_analysis {
240 0     0 0 0 my $self = shift;
241 0         0 my $project = shift;
242              
243 0   0     0 $_[0] ||= 'latest';
244              
245 0         0 my ( $url, $xml ) =
246             $self->_query_server("projects/$project/analyses/$_[0].xml");
247              
248 0         0 my $analysis = WWW::Ohloh::API::Analysis->new(
249             request_url => $url,
250             xml => $xml->findnodes('analysis[1]'),
251             );
252              
253 0 0       0 unless ( $analysis->project_id == $project ) {
254 0         0 croak "analysis $_[0] doesn't apply to project $project";
255             }
256              
257 0         0 return $analysis;
258             }
259              
260             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
261              
262             sub fetch_languages {
263 1     1 1 781 my $self = shift;
264 1         3 my %arg = @_;
265              
266             return WWW::Ohloh::API::Languages->new(
267             ohloh => $self,
268             ( sort => $arg{sort} ) x !!$arg{sort},
269 1         5 );
270             }
271              
272             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273              
274             sub fetch_language {
275 0     0 0 0 my $self = shift;
276 0         0 my $id = shift;
277              
278 0         0 my ( $url, $xml ) = $self->_query_server("languages/$id.xml");
279              
280 0         0 return WWW::Ohloh::API::Language->new(
281             request_url => $url,
282             xml => $xml->findnodes('language[1]'),
283             );
284              
285             }
286              
287             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288              
289             sub fetch_activity_facts {
290 1     1 1 536375 my $self = shift;
291 1         27 my ( $project, $analysis ) =
292             validate_pos( @_, 1, { default => 'latest' }, );
293              
294 1         8 return WWW::Ohloh::API::ActivityFacts->new(
295             ohloh => $self,
296             project => $project,
297             analysis => $analysis,
298             );
299             }
300              
301             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
302              
303             sub _ua {
304 0     0     my $self = shift;
305 0           my $ua;
306 0 0         unless ( $ua = $user_agent_of[$$self] ) {
307 0           $ua = $user_agent_of[$$self] = LWP::UserAgent->new;
308 0           $ua->agent($useragent_signature);
309             }
310 0           return $ua;
311             }
312              
313             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314              
315             sub _parser {
316 0     0     my $self = shift;
317 0   0       return $parser_of[$$self] ||= XML::LibXML->new;
318             }
319              
320             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
321              
322             sub _query_server {
323 0     0     my $self = shift;
324 0           my $url = shift;
325 0 0         my %param = $_[0] ? %{ $_[0] } : ();
  0            
326              
327 0 0         if ( $url !~ /^http/ ) {
328 0 0         $param{api_key} = $self->get_api_key
329             or croak "api key not configured";
330              
331 0           $param{v} = $api_version_of[$$self];
332              
333 0           $url = $OHLOH_URL . $url;
334              
335 0           $url .= '?' . join '&', map { "$_=$param{$_}" } keys %param;
  0            
336             }
337              
338 0 0         warn "querying ohloh server with $url" if $debugging[$$self];
339              
340             # TODO: beef up here for failures
341 0           my $request = HTTP::Request->new( GET => $url );
342 0           my $response = $self->_ua->request($request);
343              
344 0 0         unless ( $response->is_success ) {
345 0           croak "http query to Ohloh server failed: " . $response->status_line;
346             }
347              
348 0           my $result = $response->content;
349              
350 0 0         my $dom = eval { $self->_parser->parse_string($result) }
  0            
351             or croak "server didn't feed back valid xml: $@";
352              
353 0 0         if ( $dom->findvalue('/response/status/text()') ne 'success' ) {
354 0           croak "query to Ohloh server failed: ",
355             $dom->findvalue('/response/status/text()');
356             }
357              
358 0           return $url, $dom->findnodes('/response/result[1]');
359             }
360              
361             1; # Magic true value required at end of module
362              
363             __END__