File Coverage

blib/lib/Debian/Snapshot.pm
Criterion Covered Total %
statement 22 51 43.1
branch 0 6 0.0
condition n/a
subroutine 8 14 57.1
pod 5 5 100.0
total 35 76 46.0


line stmt bran cond sub pod time code
1             package Debian::Snapshot;
2             BEGIN {
3 2     2   58507 $Debian::Snapshot::VERSION = '0.003';
4             }
5             # ABSTRACT: interface to snapshot.debian.org
6              
7 2     2   1778 use Any::Moose;
  2         79603  
  2         16  
8              
9 2     2   2513 use Debian::Snapshot::Package;
  2         7  
  2         63  
10 2     2   3171 use JSON ();
  2         44183  
  2         63  
11 2     2   3626 use LWP::UserAgent;
  2         117649  
  2         1206  
12              
13             has 'user_agent' => (
14             is => 'rw',
15             isa => 'LWP::UserAgent',
16             lazy => 1,
17             builder => '_build_user_agent',
18             );
19              
20             has 'url' => (
21             is => 'rw',
22             isa => 'Str',
23             default => 'http://snapshot.debian.org',
24             );
25              
26             sub _build_user_agent {
27 1     1   1460 my $ua = LWP::UserAgent->new;
28 1         6004 $ua->agent("Debian-Snapshot/$Debian::Snapshot::VERSION ");
29 1         237 $ua->env_proxy;
30 1         27742 return $ua;
31             }
32              
33             sub _get {
34 0     0   0 my $self = shift;
35 0         0 my $url = shift;
36 0         0 $url = $self->url . $url;
37              
38 0         0 my $response = $self->user_agent->get($url, @_);
39              
40 0 0       0 die $response->status_line unless $response->is_success;
41 0         0 return $response->decoded_content;
42             }
43              
44             sub _get_json {
45 0     0   0 my $self = shift;
46 0         0 my $json = $self->_get(@_);
47 0         0 return JSON::decode_json($json);
48             }
49              
50             sub binaries {
51 0     0 1 0 my ($self, $name, $version) = @_;
52              
53 0         0 my $json = $self->_get_json("/mr/binary/$name/");
54              
55 0         0 my @binaries = map $self->package($_->{source}, $_->{version})
56             ->binary($_->{name}, $_->{binary_version}),
57 0         0 @{ $json->{result} };
58              
59 0 0       0 if (defined $version) {
60 0 0       0 $version = qr/^\Q$version\E$/ unless ref($version) eq 'Regexp';
61 0         0 @binaries = grep $_->binary_version =~ $version, @binaries;
62             }
63              
64 0         0 return \@binaries;
65             }
66              
67             sub file {
68 0     0 1 0 my ($self, $hash) = @_;
69 0         0 Debian::Snapshot::File->new(
70             hash => $hash,
71             _service => $self,
72             );
73             }
74              
75             sub package {
76 1     1 1 4 my ($self, $package, $version) = @_;
77 1         34 return Debian::Snapshot::Package->new(
78             _service => $self,
79             package => $package,
80             version => $version,
81             );
82             }
83              
84             sub packages {
85 0     0 1   my ($self) = @_;
86              
87 0           my $json = $self->_get_json("/mr/package/");
88 0           my @package = map $_->{package}, @{ $json->{result} };
  0            
89              
90 0           return \@package;
91             }
92              
93             sub package_versions {
94 0     0 1   my ($self, $package) = @_;
95              
96 0           my $json = $self->_get_json("/mr/package/$package/");
97 0           my @versions = map $_->{version}, @{ $json->{result} };
  0            
98 0           return \@versions;
99             }
100              
101 2     2   20 no Any::Moose;
  2         4  
  2         19  
102             1;
103              
104              
105              
106             =pod
107              
108             =head1 NAME
109              
110             Debian::Snapshot - interface to snapshot.debian.org
111              
112             =head1 VERSION
113              
114             version 0.003
115              
116             =head1 SYNOPSIS
117              
118             use Debian::Snapshot;
119             my $s = Debian::Snapshot->new;
120              
121             my $p = $s->package("package", "version"); # see Debian::Snapshot::Package
122             my @package_names = @{ $s->packages };
123             my @source_versions = @{ $s->package_versions("source-package") };
124              
125             my @bs = @{ $s->binaries("binary") }; # see Debian::Snapshot::Binary
126              
127             my $f = $s->file($sha1hash); # see Debian::Snapshot::File
128              
129             =head1 DESCRIPTION
130              
131             This module provides an interface to the snapshot.debian.org service.
132              
133             =head1 ATTRIBUTES
134              
135             =head2 url
136              
137             URL used to contact the snapshot service.
138             Defaults to C.
139              
140             =head2 user_agent
141              
142             The L object used to query the server.
143              
144             =head1 METHODS
145              
146             =head2 binaries($name, $version?)
147              
148             Returns an arrayref of L
149             objects for the binary package named C<$name>.
150              
151             If the optional parameter C<$version> is present, only return binaries whose
152             binary version matches C<$version> which might be either a string or a regular
153             expression.
154              
155             =head2 file($hash)
156              
157             Returns a L object for the file
158             with the given C<$hash>.
159              
160             =head2 package($package, $version)
161              
162             Returns a L object for the
163             source package C<$package> version C<$version>.
164              
165             =head2 packages
166              
167             Returns an arrayref of source package names.
168              
169             =head2 package_versions($package)
170              
171             Returns an arrayref of versions for source package C<$package>.
172              
173             =head1 SEE ALSO
174              
175             L
176              
177             =head1 AUTHOR
178              
179             Ansgar Burchardt
180              
181             =head1 COPYRIGHT AND LICENSE
182              
183             This software is copyright (c) 2010 by Ansgar Burchardt .
184              
185             This is free software; you can redistribute it and/or modify it under
186             the same terms as the Perl 5 programming language system itself.
187              
188             =cut
189              
190              
191             __END__