File Coverage

blib/lib/WWW/Freshmeat.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WWW::Freshmeat;
2            
3 3     3   581474 use 5.008;
  3         14  
  3         129  
4 3     3   17 use strict;
  3         8  
  3         103  
5 3     3   16 use warnings;
  3         10  
  3         150  
6            
7             =head1 NAME
8            
9             WWW::Freshmeat - automates usage of Freshmeat.net
10            
11             =head1 VERSION
12            
13             Version 0.22
14            
15             =cut
16            
17             our $VERSION = '0.22';
18            
19 3     3   138388 use XML::Simple qw();
  0            
  0            
20             use WWW::Freshmeat::Project;
21             use Carp;
22            
23            
24             =head1 SYNOPSIS
25            
26             use WWW::Freshmeat;
27            
28             my $fm = WWW::Freshmeat->new(token=>'freshmeat_token');
29            
30             my $project = $fm->retrieve_project('project_id');
31            
32             foreach my $p ( @projects, $project ) {
33             print $p->name(), "\n";
34             print $p->version(), "\n";
35             print $p->description(), "\n";
36             }
37            
38             =cut
39            
40             package WWW::Freshmeat;
41            
42             use base qw( LWP::UserAgent );
43            
44             sub new {
45             my $class=shift;
46             my $self=LWP::UserAgent->new();
47             bless $self,$class;
48             my %data=@_;
49             $self->{fm_token}=$data{token};
50             return $self;
51             }
52            
53             sub _token {
54             my $self = shift;
55             croak "No token" unless $self->{fm_token};
56             return $self->{fm_token};
57             }
58            
59             =head1 DESCRIPTION
60            
61             C derives from C, so it accepts all the methods
62             that C does, notably C, C, C...
63            
64             =head2 Methods
65            
66             =over 4
67            
68             =item B I
69            
70             Query the freshmeat.net site for the project I (should be the Freshmeat
71             ID of the requested project) and returns a C object or
72             undef if the project entry cannot be found.
73            
74             =cut
75            
76             sub retrieve_project {
77             my $self = shift;
78             my $id = shift;
79            
80             my $url = "http://freshmeat.net/projects/$id.xml?auth_code=".$self->_token;
81            
82             my $response = $self->get($url);
83             if ($response->is_success) {
84             my $xml = $response->content();
85             return $self->project_from_xml($xml);
86             } else {
87             if ($response->code eq '404') {
88             return undef;
89             } else {
90             die "Could not GET freshmeat project (".$response->status_line.")";
91             }
92             }
93             }
94            
95             =item B I
96            
97             Receives Freshmeat project XML record and returns a C
98             object or undef if the project entry cannot be found.
99            
100             =cut
101            
102             sub project_from_xml {
103             my $self = shift;
104             my $xml = shift;
105            
106             if ($xml eq 'Error: project not found.') {
107             return undef;
108             }
109             die "XML is empty" unless $xml;
110            
111             my $data = XML::Simple::XMLin($xml,ForceArray => ['approved-url','recent-release']);
112             #die unless exists $data->{'project'};
113             die unless $data->{'name'};
114            
115             return WWW::Freshmeat::Project->new($data, $self); #->{'project'}
116             }
117            
118             sub retrieve_user {
119             croak "'User' is temporarily removed";
120             my $self = shift;
121             my $id = shift;
122             require WWW::Freshmeat::User;
123             return WWW::Freshmeat::User->new($self,$id);
124             }
125            
126             =item B I
127            
128             Receives URL and returns URL which it redirects to.
129            
130             =cut
131            
132             sub redir_url {
133             my $self = shift;
134             my $url=shift;
135             $self->requests_redirectable([]);
136             my $response = $self->get($url) or return $url;
137             if ($response->is_redirect) {
138             #http://www.perlmonks.org/?node_id=147608
139             my $referral_uri = $response->header('Location');
140             {
141             # Some servers erroneously return a relative URL for redirects,
142             # so make it absolute if it not already is.
143             local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
144             my $base = $response->base;
145             $referral_uri = $HTTP::URI_CLASS->new($referral_uri, $base)
146             ->abs($base)->as_string;
147             }
148             return $referral_uri;
149             } else {
150             return $url;
151             }
152             }
153            
154             =back
155            
156             =head1 SEE ALSO
157            
158             L.
159            
160             =head1 AUTHOR
161            
162             Cedric Bouvier, C<< >>. Alexandr Ciornii.
163            
164             =head1 BUGS
165            
166             Please report any bugs or feature requests to
167             C, or through the web interface at
168             L.
169             I will be notified, and then you'll automatically be notified of progress on
170             your bug as I make changes.
171            
172             =head1 SUPPORT
173            
174             You can find documentation for this module with the perldoc command.
175            
176             perldoc WWW::Freshmeat
177            
178             You can also look for information at:
179            
180             =over 4
181            
182             =item * AnnoCPAN: Annotated CPAN documentation
183            
184             L
185            
186             =item * CPAN Ratings
187            
188             L
189            
190             =item * RT: CPAN's request tracker
191            
192             L
193            
194             =item * Search CPAN
195            
196             L
197            
198             =back
199            
200             =head1 ACKNOWLEDGEMENTS
201            
202             =head1 COPYRIGHT & LICENSE
203            
204             Copyright 2006 Cedric Bouvier (version 0.01).
205             Copyright 2009-2012 Alexandr Ciornii.
206            
207             This program is free software; you can redistribute it and/or modify it
208             under the same terms as Perl itself.
209            
210             =cut
211            
212             1; # End of WWW::Freshmeat