File Coverage

blib/lib/Net/Google/Code/Download.pm
Criterion Covered Total %
statement 64 66 96.9
branch 17 26 65.3
condition 3 9 33.3
subroutine 7 7 100.0
pod 3 3 100.0
total 94 111 84.6


line stmt bran cond sub pod time code
1             package Net::Google::Code::Download;
2              
3 3     3   3686 use Any::Moose;
  3         15  
  3         26  
4 3     3   1502 use Params::Validate qw(:all);
  3         7  
  3         682  
5 3     3   19 use Scalar::Util qw/blessed/;
  3         6  
  3         2797  
6              
7             with 'Net::Google::Code::TypicalRoles';
8              
9             has 'project' => (
10             isa => 'Str',
11             is => 'rw',
12             );
13              
14             has 'name' => (
15             isa => 'Str',
16             is => 'rw',
17             );
18              
19             has 'size' => (
20             isa => 'Str',
21             is => 'rw',
22             );
23              
24             has 'download_url' => (
25             isa => 'Str',
26             is => 'rw',
27             );
28              
29             has 'count' => (
30             isa => 'Int',
31             is => 'rw',
32             );
33              
34             has 'labels' => (
35             isa => 'ArrayRef[Str]',
36             is => 'rw',
37             );
38              
39             has 'checksum' => (
40             isa => 'Str',
41             is => 'rw',
42             );
43              
44             has 'uploaded_by' => (
45             isa => 'Str',
46             is => 'rw',
47             );
48              
49             has 'uploaded' => (
50             isa => 'Str',
51             is => 'rw',
52             );
53              
54             sub load {
55 3     3 1 12 my $self = shift;
56 3   33     30 my $name = shift || $self->name;
57 3 50       9 die "current object doesn't have name and load() is not passed a name either"
58             unless $name;
59            
60             # http://code.google.com/p/net-google-code/downloads/detail?name=Net-Google-Code-0.01.tar.gz
61            
62 3         35 my $content =
63             $self->fetch( $self->base_url . "downloads/detail?name=$name" );
64            
65 3 50 33     512 $self->name( $name ) unless $self->name && $self->name eq $name;
66 3         13 return $self->parse( $content );
67             }
68              
69             sub parse {
70 3     3 1 7 my $self = shift;
71 3         6 my $tree = shift;
72 3         12 my $need_delete = not blessed $tree;
73 3 50       28 $tree = $self->html_tree( html => $tree ) unless blessed $tree;
74              
75 3         8 my $entry;
76 3         21 my $uploaded = $tree->look_down(class => 'date')->attr('title');
77 3 50       2289 $self->uploaded( $uploaded ) if $uploaded;
78              
79 3         13 my @labels_tag = $tree->look_down( class => 'label' );
80 3         3126 my @labels;
81 3         6 for my $tag ( @labels_tag ) {
82 6         88 push @labels, $tag->as_text;
83             }
84 3         76 $self->labels( \@labels );
85              
86             # parse uploaded_by and download count.
87             # uploaded and labels are kind of special, so they're handleed above
88 3         12 my ($meta) = $tree->look_down( id => 'issuemeta' );
89 3         2780 my @meta = $meta->find_by_tag_name('tr');
90 3         435 for my $meta (@meta) {
91              
92 15         19 my ( $key, $value );
93 15         38 $key = $meta->find_by_tag_name('th');
94 15 100       390 next unless $key;
95 9         26 $key = $key->as_text;
96              
97 9         159 my $td = $meta->find_by_tag_name('td');
98 9 50       247 next unless $td;
99 9         26 $value = $td->as_text;
100 9 100       229 if ( $key =~ /Uploaded.*?by/ ) {
    100          
101 3         99 $self->uploaded_by($value);
102             }
103             elsif ( $key =~ /Downloads/ ) {
104 3         26 $self->count($value);
105             }
106             }
107              
108             # download_url and size
109 3         14 my $desc = $tree->look_down( class => 'vt issuedescription' );
110 3         2476 my $box_inner = $desc->look_down( class => 'box-inner' );
111 3         263 $self->download_url( $box_inner->content_array_ref->[0]->attr('href') );
112              
113 3         61 my $size = $box_inner->content_array_ref->[3];
114 3         27 $size =~ s/^\D+//;
115 3         23 $size =~ s/\s+$//;
116 3 50       22 $self->size( $size ) if $size;
117              
118             # checksum
119 3         12 my $span = $desc->find_by_tag_name('span');
120 3         282 my $checksum = $span->content_array_ref->[0];
121 3 50       28 if ( $checksum =~ /^SHA1 Checksum:\s+(\w+)/ ) {
122 3         31 $self->checksum( $1 );
123             }
124 3 50       24 $tree->delete if $need_delete;
125             }
126              
127             sub BUILDARGS {
128 4     4 1 404 my $class = shift;
129 4         10 my %args;
130 4 50 33     32 if ( @_ % 2 && ref $_[0] eq 'HASH' ) {
131 0         0 %args = %{$_[0]};
  0         0  
132             }
133             else {
134 4         25 %args = @_;
135             }
136              
137 4         17 my %translations = ( filename => 'name', 'downloadcount' => 'count' );
138 4         14 for my $key ( keys %translations ) {
139 8 100       24 if ( exists $args{$key} ) {
140 4         13 $args{ $translations{$key} } = $args{$key};
141             }
142             }
143 4         143 return $class->SUPER::BUILDARGS(%args);
144             }
145              
146 3     3   20 no Any::Moose;
  3         8  
  3         32  
147             __PACKAGE__->meta->make_immutable;
148              
149             1;
150             __END__