/
Extension.pm
342 lines (225 loc) · 8.05 KB
/
Extension.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package WWW::PGXN::Extension;
use 5.8.1;
use strict;
our $VERSION = v0.12.5;
BEGIN {
# Hash accessors.
for my $k (qw(
stable
testing
unstable
)) {
no strict 'refs';
*{"$k\_info"} = sub { +{ %{ shift->{$k} || {} } } };
}
}
sub new {
my ($class, $pgxn, $data) = @_;
$data->{_pgxn} = $pgxn;
bless $data, $class;
}
sub name { shift->{extension} }
sub latest { shift->{latest} }
sub latest_info {
my $self = shift;
return { %{ $self->{$self->{latest}} } };
}
sub stable_distribution { shift->_dist_for_status('stable') }
sub testing_distribution { shift->_dist_for_status('testing') }
sub unstable_distribution { shift->_dist_for_status('unstable') }
sub latest_distribution {
my $self = shift;
$self->_dist_for_status($self->{latest});
}
sub distribution_for_version {
my $self = shift;
my $vdata = $self->info_for_version(shift) or return;
return $self->{_pgxn}->get_distribution(@{ $vdata->[0] }{qw(dist version)});
}
sub info_for_version {
my ($self, $version) = @_;
$self->{versions}{$version};
}
sub _dist_for_status {
my ($self, $status) = @_;
my $vdata = $self->{$status} or return;
return $self->{_pgxn}->get_distribution(@{ $vdata }{qw(dist version)});
}
sub download_stable_to {
my $self = shift;
$self->_download_to(shift, $self->{stable});
}
sub download_latest_to {
my $self = shift;
$self->_download_to(shift, $self->latest_info);
}
sub download_testing_to {
my $self = shift;
$self->_download_to(shift, $self->{testing});
}
sub download_unstable_to {
my $self = shift;
$self->_download_to(shift, $self->{unstable});
}
sub download_version_to {
my ($self, $version, $file) = @_;
my $info = $self->info_for_version($version) or return;
$self->_download_to($file, $info->[0]);
}
sub _download_to {
my ($self, $file, $info) = @_;
return unless $info;
$self->{_pgxn}->_download_to($file => {
dist => $info->{dist},
version => $info->{version},
});
}
1;
__END__
=head1 Name
WWW::PGXN::Extension - Extension metadata fetched from PGXN
=head1 Synopsis
my $pgxn = WWW::PGXN->new( url => 'http://api.pgxn.org/' );
my $ext = $pgxn->get_extension('pgTAP');
$ext->download_stable_to('.');
=head1 Description
This module represents PGXN extension metadata fetched from PGXN>. It is not
intended to be constructed directly, but via the L<WWW::PGXN/get_extension>
method of L<WWW::PGXN>.
=head1 Interface
=begin private
=head2 Constructor
=head3 C<new>
my $extension = WWW::PGXN::Extension->new($pgxn, $data);
Construct a new WWW::PGXN::Extension object. The first argument must be an
instance of L<WWW::PGXN> that connected to the PGXN server. The second
argument must be the data fetched.
=end private
=head2 Instance Accessors
=head3 C<name>
my $name = $extension->name;
$extension->name($name);
The name of the extension.
=head3 C<latest>
my $latest = $extension->latest;
$extension->latest($latest);
The status of the latest release. Should be one of:
=over
=item stable
=item testing
=item unstable
=back
=head2 Instance Methods
=head3 C<stable_info>
=head3 C<testing_info>
=head3 C<unstable_info>
my $stable_info = $extension->stable_info;
my $testing_info = $extension->testing_info;
my $unstable_info = $extension->unstable_info;
Returns a hash reference describing the latest version of the extension for
the named release status. The supported keys are:
=over
=item C<dist>
The name of the distribution in which the extension may be found.
=item C<version>
The version of the distribution in which the extension may be found.
=item C<abstract>
A brief description of the extension. Available only from PGXN API servers,
not mirrors.
=item C<sha1>
The SHA1 hash for the distribution archive file.
=item C<docpath>
A path to the documentation for the extension, if any.
=back
If no release has been made with the given status, an empty hash reference
will be returned. Here's an example of the structure for a distribution loaded
from an API server:
{
dist => 'pair',
version => '0.1.1',
abstract => 'A key/value pair data type',
sha1 => 'c552c961400253e852250c5d2f3def183c81adb3',
docpath => 'doc/pair',
}
=head3 C<latest_info>
my $latest_info = $extension->latest_info;
Returns a hash reference describing the latest version of the extension.
Essentially a convenience method for:
my $meth = $extension->latest . '_info';
my $info = $extension->$meth;
=head3 C<info_for_version>
my $version_info = $extension->info_for_version;
Returns a hash reference containing the distribution information for the named
version of the extension, if it exists. The supported keys are:
=over
=item C<dist>
The name of the distribution in which the version of the extension may be
found.
=item C<version>
The version of the distribution in which the version of the extension may be
found.
=item C<date>
The release date the distribution containing the version of the extension.
Available only from PGXN API servers, not mirrors.
Returns C<undef> if no such version of the extension exists.
=back
=head3 C<stable_distribution>
=head3 C<testing_distribution>
=head3 C<unstable_distribution>
my $stable_distribution = $extension->stable_distribution;
my $testing_distribution = $extension->testing_distribution;
my $unstable_distribution = $extension->unstable_distribution;
Returns a L<WWW::PGXN::Distribution> object describing the distribution
containing the distribution with the named release status. Returns C<undef> if
no distribution contains the extension with that status.
=head3 C<latest_distribution>
Returns a L<WWW::PGXN::Distribution> object describing the distribution in
which the latest version of the extension is found. Essentially a convenience
method for:
my $meth = $extension->latest . '_distribution';
my $dist = $extension->$meth;
=head3 C<distribution_for_version>
my $version_distribution = $extension->distribution_for_version($version);
Returns a L<WWW::PGXN::Distribution> object describing the distribution in
which the named version of the extension is found. Returns C<undef> if no such
version of the extension exists.
=head3 C<download_stable_to>
=head3 C<download_testing_to>
=head3 C<download_unstable_to>
my $stable_file = $extension->download_stable_to('/usr/src');
my $testing_file = $extension->download_testing_to('.');
my $unstable_file = $extension->download_unstable_to('mfile.zip');
Downloads the distribution containing the latest version of the extension with
the named release status. Pass the name of the file to save to, or the name of
a directory. If a directory is specified, the file will be written with the
same name as it has on PGXN, such as C<pgtap-0.24.0.zip>. Either way, the name
of the file written will be returned. Regardless of the file's name, it will
always be a zip archive.
=head3 C<download_latest_to>
my $file = $extension->download_latest_to($file);
Download the distribution containing the latest version of the distribution,
regardless of its release status. Essentially a convenience method for:
my $meth = 'download_' . $extension->latest . '_to';
my $file = $extension->$meth('.');
=head3 C<download_version_to>
my $file = $extension->download_version_to($version, $file);
Download the distribution containing the specified version of the extension.
=head1 See Also
=over
=item * L<WWW::PGXN>
The main class to communicate with a PGXN mirror or API server.
=back
=head1 Support
This module is stored in an open L<GitHub
repository|http://github.com/theory/www-pgxn/>. Feel free to fork and
contribute!
Please file bug reports via L<GitHub
Issues|http://github.com/theory/www-pgxn/issues/> or by sending mail to
L<bug-WWW-PGXN@rt.cpan.org|mailto:bug-WWW-PGXN@rt.cpan.org>.
=head1 Author
David E. Wheeler <david@justatheory.com>
=head1 Copyright and License
Copyright (c) 2011 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut