| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | 5 | $self->url( $tds[0]->find_by_tag_name('a')->attr('href') ); | |||||||||||||||||||||||||||
| 51 | 5 | ▶ | if ( $self->url =~ /aid=(-?\d+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 5 | my $id = $1; | |||||||||||||||||||||||||||
| 53 | 5 | $self->id( $id ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | if ( $tds[1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 5 | my $size = $tds[1]->content_array_ref->[2]; | |||||||||||||||||||||||||||
| 58 | 5 | ▶ | if ( $size && $size =~ /([\d.]+)\s*(\w+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$size and $size =~ /([\d.]+)\s*(\w+)/
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+)/
| |||||||||||||||||||||||||||||
| 59 | 5 | $self->size("$1 $2"); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | warn 'failed to parse size' unless $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 5 | ▶ | $tree->delete if $need_delete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | my @attachments; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | my @items = $element->find_by_tag_name('tr'); | |||||||||||||||||||||||||||
| 80 | 2 | ▶ | while ( scalar @items ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 85 | 4 | push @attachments, $a; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 2 | ▶ | $element->delete if $need_delete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'
$self->_mime_type or "File::MMagic"->new->checktype_contents($content)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||