PTV/Route.pm

filter: Statementstmt 31.4% 28 / 89 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 7 / 14 Podpod 75.0% 3 / 4 totaltotal 23.9% 38 / 159 CC 24 SCAR 48.1
CC
24
Cov
21%
__get_tt
272.0
new
20.0
__request
12.0
CRAP 122.3
↑ Summary PTV/TimeTable/Schedule.pm »
1 package WWW::PTV::Route;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use WWW::PTV::TimeTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $STOP = {};
12 2 our @ATTR = qw( id direction_out direction_in direction_out_link direction_in_link
13 description_out description_in name operator operator_ph );
14  
15 2 foreach my $attr ( @ATTR ) {
16 {
17 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶ *{ __PACKAGE__ .'::'. $attr } = sub {
Subroutine __ANON__: not called CC=2 SCAR=14.6
19 0 my( $self, $val ) = @_;
20 0 ▶ $self->{$attr} = $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
21 0 return $self->{$attr}
22 }
23 20 }
24 }
25  
26 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
27 0 my ( $class, %args ) = @_;
28  
29 0 my $self = bless {}, $class;
30 0 ▶ $args{id} or croak 'Constructor failed: mandatory id argument not supplied';
Branch: 1 branch
BranchTrueFalseTotal
$args{"id"} or croak("Constructor failed: mandatory id argument not supplied") 0 0 0
31  
32 0 foreach my $attr ( @ATTR ) { $self->{$attr} = $args{$attr} }
33  
34 0 $self->{uri} = $args{uri};
35 0 ▶ $args{ua} ? weaken( $self->{ua} = $args{ua} ) : croak 'Mandatory argument ua not supplied';
Branch: 1 branch
BranchTrueFalseTotal
$args{'ua'} ? : 0 0 0
36  
37 0 return $self
38 }
39  
40 0 ▶sub get_inbound_tt { $_[0]->__get_tt( 'in' ) }
Subroutine get_inbound_tt: not called CC=1 SCAR=6.9
Pod documented
41  
42 0 ▶sub get_outbound_tt { $_[0]->__get_tt( 'out' ) }
Subroutine get_outbound_tt: not called CC=1 SCAR=6.9
Pod documented
43  
44 0 ▶sub __get_tt {
Subroutine __get_tt: not called CC=16 SCAR=56.1
45 0 my ( $self, $direction ) = @_;
46  
47 0 ▶ return unless $direction =~ /(in|out)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $direction =~ /(in|out)/ 0 0 0
48  
49 my $tt = $self->__request( $direction eq 'out'
50 ? $self->{direction_out_link}
51 : $self->{direction_in_link}
52 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$direction eq 'out' ? : 0 0 0
53  
54 0 my $t = HTML::TreeBuilder->new_from_content( $tt );
55  
56 0 for ( $t->look_down( _tag => 'meta' ) ) {
57  
58 0 ▶ if( ( defined $_->attr( 'http-equiv' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->attr("http-equiv") and $_->attr("http-equiv") eq "refresh") 0 0 0
Condition0%0/3
defined $_->attr("http-equiv") and $_->attr("http-equiv") eq "refresh"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->attr("http-equiv") and $_->attr("http-equiv") eq "refresh"
defined $_->attr("http-equiv") $_->attr("http-equiv") eq "refresh"
Truth table0%0/3
A and B
A= defined $_->attr("http-equiv")B= $_->attr("http-equiv") eq "refresh"
ABresult
0X0
100
111
59 and ( $_->attr( 'http-equiv' ) eq 'refresh' )
60 ) {
61 0 ( my $url = $_->attr( 'content' ) ) =~ s/^.*url=//;
62 0 $url .= '&itdLPxx_scrollOffset=118';
63  
64 0 $t = HTML::TreeBuilder->new_from_content( $self->__request( $self->{uri}.'/tt/'.$url ) );
65  
66 last
67 0 }
68 }
69  
70 0 $tt = $t->look_down( _tag => 'img', title => 'Expand' );
71  
72 0 ▶ if ( $tt && $tt->attr( 'onclick' ) && $tt->attr( 'onclick' ) =~ /TTB_REQUEST/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt and $tt->attr("onclick") and $tt->attr("onclick") =~ /TTB_REQUEST/) 0 0 0
Condition0%0/6
$tt and $tt->attr("onclick") and $tt->attr("onclick") =~ /TTB_REQUEST/
!ll&&!rl&&r
000
$tt and $tt->attr("onclick")
!ll&&!rl&&r
000
MC/DC0%0/3
$tt and $tt->attr("onclick") and $tt->attr("onclick") =~ /TTB_REQUEST/
$tt $tt->attr("onclick") $tt->attr("onclick") =~ /TTB_REQUEST/
Truth table0%0/4
A and B and C
A= $ttB= $tt->attr("onclick")C= $tt->attr("onclick") =~ /TTB_REQUEST/
ABCresult
0XX0
10X0
1100
1111
73 0 ( $tt = $tt->attr( 'onclick' ) ) =~ s/^.*\('//;
74 0 $tt =~ s/'.*$//;
75 0 $t = HTML::TreeBuilder->new_from_content( $self->__request( "http://tt.ptv.vic.gov.au/tt/$tt" ) )
76 }
77  
78 0 $t = $t->look_down( _tag => 'div', id => qr/tt(Bus|Tram|Train|Regional)/ );
79 0 my @stops = $t->look_down( _tag => 'div', class => qr/^ma_stop/ );
80 0 my @stop_names = map { $_->as_text } @stops;
81 0 my @stop_links = map { my ($r) = $_->look_down( _tag => 'a' )->attr( 'href' ) =~ /.*\/(\d*$)/ } @stops;
82 0 my $stop_times;
83 0 my $c = 0;
84  
85 0 foreach my $t ( $t->look_down( _tag => 'div', class => qr/^ttBodyN?TP$/ ) ) {
86 0 my $s;
87  
88 0 foreach my $t ( $t->look_down( _tag => 'span' ) ) {
89 0 my ( $h, $m ) = split /:/, $t->as_text;
90 0 ▶ push @{ $s }, $h and next if $h !~ /\d/;
Branch: 1 branch
BranchTrueFalseTotal
unless $h =~ /\d/ 0 0 0
Condition0%0/5
$h =~ /\d/ || push(@{$s;}, $h) && (next)
l!l&&r!l&&!r
000
push @{$s;}, $h and next
!ll
00
MC/DC0%0/2
$h =~ /\d/ || push(@{$s;}, $h) && (next)
$h =~ /\d/ push @{$s;}, $h
Truth table0%0/3
A || B
A= $h =~ /\d/B= push @{$s;}, $h
ABresult
1X1
011
000
91 0 my $is_pm = $t->look_down( _tag => 'b' );
92  
93 0 ▶ push @{ $s }, ( $h == 12
Branch: 3 branches
BranchTrueFalseTotal
$h == 12 ? : 0 0 0
$is_pm ? : 0 0 0
$is_pm ? : 0 0 0
94 ? ( $is_pm ? "$h:$m" : "00:$m" )
95 : ( $is_pm ? $h + 12 .":$m" : "$h:$m" )
96 );
97 }
98  
99 0 $self->{ STOP }->{ $stop_links[$c] }->{ $direction } = $s;
100 0 $self->{ STOP }->{ $stop_links[$c] }->{ name } = $stop_names[$c];
101 0 $c++;
102 0 push @{ $stop_times }, $s
103 }
104  
105 0 my $ret = WWW::PTV::TimeTable->new( \@stop_names, \@stop_links, $stop_times );
106 0 $self->{timetable}->{$direction} = $ret;
107 0 return $ret;
108 }
109  
110 0 ▶sub __request {
Subroutine __request: not called CC=3 SCAR=24.8
111 0 my ( $self, $uri ) = @_;
112  
113 my $res = ( $uri !~ /^http:/
114 ? $self->{ua}->get( $self->{uri} . $uri )
115 0 ▶ : $self->{ua}->get( $uri ) );
Branch: 1 branch
BranchTrueFalseTotal
!($uri =~ /^http:/) ? : 0 0 0
116  
117 0 ▶ return $res->content if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
118  
119 0 croak 'Unable to retrieve content: ' . $res->status_line
120 }
121  
122 0 ▶sub get_stop_names_and_ids {
Subroutine get_stop_names_and_ids: not called CC=3 SCAR=24.8
Pod documented
123 0 my ( $self, $direction ) = @_;
124  
125 0 ▶ $direction ||= 'out';
Condition0%0/2
$direction ||= "out"
l!l
00
MC/DC0%0/1
$direction ||= "out"
$direction
Truth table0%0/2
A
A= $direction
Aresult
11
00
126 0 ▶ $self->{timetable}->{$direction} || $self->__get_tt($direction);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"timetable"}{$direction} or $self->__get_tt($direction) 0 0 0
127  
128 0 return $self->{timetable}->{$direction}->stop_names_and_ids;
129 }
130  
131 2 1;
132  
133 __END__
↑ Summary PTV/TimeTable/Schedule.pm »