Code/Issue/Attachment.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 64.2% 18 / 28 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 82.9% 107 / 129 CC 18 SCAR 18.6
CC
18
Cov
85%
parse
12.1
parse_attachments
5.1
_load
3.1
CRAP 6.4
« Code/Issue/Base.pm ↑ Summary Code.pm »
1 package Net::Google::Code::Issue::Attachment;
2 10 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 with 'Net::Google::Code::Role::Fetchable', 'Net::Google::Code::Role::HTMLTree';
4 10 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use File::MMagic;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 has 'name' => ( isa => 'Str', is => 'rw' );
9 10 has 'url' => ( isa => 'Str', is => 'rw' );
10 10 has 'size' => ( isa => 'Str', is => 'rw' );
11 10 has 'id' => ( isa => 'Int', is => 'rw' );
12 has content => (
13 isa => 'Str',
14 is => 'rw',
15 lazy => 1,
16 1 ▶ default => sub { ($_[0]->_load)[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 10 );
18  
19 has content_type => (
20 isa => 'Str',
21 is => 'rw',
22 lazy => 1,
23 0 ▶ default => sub { ($_[0]->_load)[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 10 );
25  
26 5 ▶sub parse {
Subroutine parse: called CC=11 SCAR=24.9
Pod documented
27 5 my $self = shift;
28 5 my $tree = shift;
29 5 my $need_delete = not blessed $tree;
30 5 ▶ $tree = $self->html_tree( html => $tree ) unless blessed $tree;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($tree) 1 4 5
31 5 my $tr = $tree->find_by_tag_name('tr');
32  
33 5 my $b = $tr->find_by_tag_name('b'); # name lives here
34 5 ▶ if ($b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b) 5 0 5
35 5 my $name = $b->content_array_ref->[0];
36 5 $name =~ s/^\s+//;
37 5 $name =~ s/\s+$//;
38 5 $self->name($name);
39  
40 # google code doesn't parse download's content type at all, we need to
41 # figure it out by ourselves
42 5 my $content_type = $self->_mime_type;
43 5 ▶ if ( $content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type) 1 4 5
44 1 $self->content_type( $content_type );
45 }
46 }
47  
48 5 my @tds = $tr->find_by_tag_name('td');
49 5 ▶ if (@tds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tds) 5 0 5
50 5 $self->url( $tds[0]->find_by_tag_name('a')->attr('href') );
51 5 ▶ if ( $self->url =~ /aid=(-?\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url =~ /aid=(-?\d+)/) 5 0 5
52 5 my $id = $1;
53 5 $self->id( $id );
54 }
55  
56 5 ▶ if ( $tds[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tds[1]) 5 0 5
57 5 my $size = $tds[1]->content_array_ref->[2];
58 5 ▶ if ( $size && $size =~ /([\d.]+)\s*(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size and $size =~ /([\d.]+)\s*(\w+)/) { } 5 0 5
Condition33%1/3
$size and $size =~ /([\d.]+)\s*(\w+)/
!ll&&!rl&&r
005
MC/DC0%0/2
$size and $size =~ /([\d.]+)\s*(\w+)/
$size $size =~ /([\d.]+)\s*(\w+)/
Truth table33%1/3
A and B
A= $sizeB= $size =~ /([\d.]+)\s*(\w+)/
ABresult
0X0
100
111
59 5 $self->size("$1 $2");
60 }
61 else {
62 0 ▶ warn 'failed to parse size' unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
63 }
64 }
65 }
66  
67 5 ▶ $tree->delete if $need_delete;
Branch: 1 branch
BranchTrueFalseTotal
if $need_delete 1 4 5
68 5 return 1;
69 }
70  
71 2 ▶sub parse_attachments {
Subroutine parse_attachments: called CC=5 SCAR=16.2
Pod documented
72 2 my $self = shift;
73 2 my $element = shift;
74 2 my $need_delete = not blessed $element;
75 2 ▶ $element = $self->html_tree( html => $element ) unless blessed $element;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($element) 0 2 2
76  
77 2 my @attachments;
78  
79 2 my @items = $element->find_by_tag_name('tr');
80 2 ▶ while ( scalar @items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @items) 4 2 6
81 4 my $tr = shift @items;
82 4 my $a = Net::Google::Code::Issue::Attachment->new;
83  
84 4 ▶ if ( $a->parse( $tr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->parse($tr)) 4 0 4
85 4 push @attachments, $a;
86 }
87 }
88 2 ▶ $element->delete if $need_delete;
Branch: 1 branch
BranchTrueFalseTotal
if $need_delete 0 2 2
89 2 return @attachments;
90 }
91  
92 1 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.4
93 1 my $self = shift;
94 #XXX weird happens if the previous fetch is also an attachment,
95 # which will make the following fetch a Bad Request.
96 1 $self->fetch( 'http://code.google.com' );
97  
98 1 my $content = $self->fetch( $self->url );
99  
100 # in case MIME::Types failed to get, let File::MMagic rescue!
101 1 ▶ my $content_type =
Condition40%2/5
$self->_mime_type || 'File::MMagic'->new->checktype_contents($content) || 'application/octet-stream'
l!l
10
$self->_mime_type or "File::MMagic"->new->checktype_contents($content)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->_mime_type || 'File::MMagic'->new->checktype_contents($content) || 'application/octet-stream'
$self->_mime_type "File::MMagic"->new->checktype_contents($content)
Truth table33%1/3
A or B
A= $self->_mime_typeB= "File::MMagic"->new->checktype_contents($content)
ABresult
1X1
011
000
102 $self->_mime_type
103 || File::MMagic->new->checktype_contents($content)
104 || 'application/octet-stream';
105 1 $self->content( $content );
106 1 $self->content_type( $content_type );
107 1 return $content, $content_type;
108 }
109  
110 6 ▶sub _mime_type {
Subroutine _mime_type: called CC=2 SCAR=6.9
111 6 my $self = shift;
112 6 my $mime_type = MIME::Types->new->mimeTypeOf( $self->name );
113 6 ▶ return $mime_type ? $mime_type->type : undef;
Branch: 1 branch
BranchTrueFalseTotal
$mime_type ? : 1 5 6
114 }
115  
116 10 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 10 __PACKAGE__->meta->make_immutable;
118  
119 10 1;
120  
121 __END__
« Code/Issue/Base.pm ↑ Summary Code.pm »