PTV.pm

filter: Statementstmt 17.5% 30 / 171 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 36.0% 9 / 25 Podpod 100% 13 / 13 totaltotal 19.9% 52 / 261 SCAR 58.9
CC
23
Cov
14%
get_area_by_id
56.0
get_stop_by_id
42.0
get_route_by_id
30.0
CRAP 360.0
↑ Summary PTV/Route.pm »
1 package WWW::PTV;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use WWW::PTV::Area;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use WWW::PTV::Stop;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use WWW::PTV::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.06';
15 2 our $CACHE;
16  
17 sub __request {
18 0 ▶ my($self,$uri) = @_;
Subroutine __request: not called CC=3 SCAR=24.8
19 my $res = ( $uri !~ /^http:/
20 ? $self->{ua}->get( $self->{uri} . $uri )
21 0 ▶ : $self->{ua}->get( $uri ) );
Branch: 1 branch
BranchTrueFalseTotal
!($uri =~ /^http:/) ? : 0 0 0
22 0 ▶ $res->is_success and return $res->content;
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success and return $res->content 0 0 0
23 0 croak 'Unable to retrieve content: ' . $res->status_line
24 }
25  
26 sub __tl_request {
27 0 ▶ my ($self, $tag_id)= @_;
Subroutine __tl_request: not called CC=4 SCAR=30.0
28 my $r = ( $self->{cache} and $CACHE->{timetables}->{master}
29 ? $CACHE->{timetables}->{master}
30 0 ▶ : $self->__request( 'http://ptv.vic.gov.au/timetables' ) );
Branch: 1 branch
BranchTrueFalseTotal
$WWW::PTV::CACHE->{'timetables'}{'master'} ? : 0 0 0
Condition0%0/3
$self->{'cache'} && ($WWW::PTV::CACHE->{'timetables'}{'master'} ? $WWW::PTV::CACHE->{'timetables'}{'master'} : $self->__request('http://ptv.vic.gov.au/timetables'))
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'cache'} && ($WWW::PTV::CACHE->{'timetables'}{'master'} ? $WWW::PTV::CACHE->{'timetables'}{'master'} : $self->__request('http://ptv.vic.gov.au/timetables'))
$self->{'cache'} ($WWW::PTV::CACHE->{'timetables'}{'master'} ? $WWW::PTV::CACHE->{'timetables'}{'master'} : $self->__request('http://ptv.vic.gov.au/timetables'))
Truth table0%0/3
A && B
A= $self->{'cache'}B= ($WWW::PTV::CACHE->{'timetables'}{'master'} ? $WWW::PTV::CACHE->{'timetables'}{'master'} : $self->__request('http://ptv.vic.gov.au/timetables'))
ABresult
0X0
100
111
31 0 ▶ $CACHE->{timetables}->{master} = $r if $self->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 0 0 0
32 0 my $t = HTML::TreeBuilder->new_from_content( $r );
33 0 $t = $t->look_down( _tag => 'select', id => $tag_id );
34 0 my @routes = $t->look_down( _tag => 'option' );
35 0 return my %routes = map { $_->attr( 'value' ) => $_->as_text } grep { $_->attr( 'value' ) ne '' } @routes
36 }
37  
38 sub new {
39 0 ▶ my($class,%args)= @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
40 0 my $self = bless {}, $class;
41 0 ▶ $self->{uri} = 'http://' . ( defined $args{uri} ? $args{uri} : 'ptv.vic.gov.au' ) . '/';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'uri'} ? : 0 0 0
42 0 $self->{ua} = LWP::UserAgent->new;
43 0 $self->{tree} = HTML::TreeBuilder->new;
44 0 ▶ $self->{cache}++ if $args{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"cache"} 0 0 0
45 0 return $self
46 }
47  
48 0 ▶sub cache { $_[0]->{cache}++ }
Subroutine cache: not called CC=1 SCAR=6.9
Pod documented
49  
50 0 ▶sub nocache { $_[0]->{cache} = 0 }
Subroutine nocache: not called CC=1 SCAR=6.9
Pod documented
51  
52 sub get_metropolitan_bus_routes {
53 0 ▶ return $_[0]->__tl_request( 'RouteForm1_RouteUrl' );
Subroutine get_metropolitan_bus_routes: not called CC=1 SCAR=6.9
Pod documented
54 }
55  
56 sub get_metropolitan_train_routes {
57 0 ▶ return $_[0]->__tl_request( 'RouteForm2_RouteUrl' )
Subroutine get_metropolitan_train_routes: not called CC=1 SCAR=6.9
Pod documented
58 }
59  
60 sub get_metropolitan_tram_routes {
61 0 ▶ return $_[0]->__tl_request( 'RouteForm3_RouteUrl' );
Subroutine get_metropolitan_tram_routes: not called CC=1 SCAR=6.9
Pod documented
62 }
63  
64 sub get_vline_bus_routes {
65 0 ▶ return $_[0]->__tl_request( 'RouteForm4_RouteUrl' )
Subroutine get_vline_bus_routes: not called CC=1 SCAR=6.9
Pod documented
66 }
67  
68 sub get_vline_train_routes {
69 0 ▶ return $_[0]->__tl_request( 'RouteForm5_RouteUrl' )
Subroutine get_vline_train_routes: not called CC=1 SCAR=6.9
Pod documented
70 }
71  
72 sub get_regional_bus_routes {
73 0 ▶ return $_[0]->__tl_request( 'RouteForm6_RouteUrl' )
Subroutine get_regional_bus_routes: not called CC=1 SCAR=6.9
Pod documented
74 }
75  
76 sub get_route_by_id {
77 0 ▶ my( $self, $id )= @_;
Subroutine get_route_by_id: not called CC=5 SCAR=34.0
Pod documented
78 0 ▶ $id or return "Mandatory parameter id not given";
Branch: 1 branch
BranchTrueFalseTotal
$id or return "Mandatory parameter id not given" 0 0 0
79  
80 0 ▶ return $CACHE->{ROUTE}->{$id} if ( $self->{cache} and $CACHE->{ROUTE}->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} and $WWW::PTV::CACHE->{"ROUTE"}{$id} 0 0 0
Condition0%0/3
$self->{"cache"} and $WWW::PTV::CACHE->{"ROUTE"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cache"} and $WWW::PTV::CACHE->{"ROUTE"}{$id}
$self->{"cache"} $WWW::PTV::CACHE->{"ROUTE"}{$id}
Truth table0%0/3
A and B
A= $self->{"cache"}B= $WWW::PTV::CACHE->{"ROUTE"}{$id}
ABresult
0X0
100
111
81  
82 0 my $r = $self->__request( "/route/view/$id" );
83 0 my $t = HTML::TreeBuilder->new_from_content( $r );
84 #my $t = HTML::TreeBuilder->new_from_file( './metro_train_route_1' );
85 #my $t = HTML::TreeBuilder->new_from_file( './regional_bus_route_geelong_19' );
86 0 my %route = (id => $id);
87 0 my $r_link = $t->look_down( _tag => 'div', id => 'content' );
88 0 $route{name} = $t->look_down( _tag => 'h1' )->as_text;
89 ( $route{direction_in}, $route{direction_out} )
90 0 = $r_link->look_down( _tag => 'a' );
91  
92 ( $route{direction_in_link}, $route{direction_out_link} )
93 0 = map { $_->attr( 'href' ) } $r_link->look_down( _tag => 'a' );
94  
95 ( $route{direction_in}, $route{direction_out} )
96 0 = map { $_->as_text } ( $route{direction_in}, $route{direction_out} );
97  
98 ( $route{description_in}, $route{description_out} )
99 0 = map { $_->as_text } $r_link->look_down( _tag => 'p' );
100 0 my $operator = $t->look_down( _tag => 'div', class => 'operator' )->as_text;
101 0 $operator =~ s/(Contact|Website|:)/,/g;
102 0 $operator =~ s/\s//g;
103 0 ( $route{operator} ,$route{operator_ph} ) = ( split /,/, $operator )[0,2];
104 0 $route{ua} = $self->{ua};
105 0 $route{uri} = $self->{uri};
106 0 my $route = WWW::PTV::Route->new( %route );
107 0 ▶ $CACHE->{ROUTE}->{$id} = $route if ( $self->{cache} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 0 0 0
108 0 return $route
109 }
110  
111  
112  
113 sub get_stop_by_id {
114 0 ▶ my( $self, $id )= @_;
Subroutine get_stop_by_id: not called CC=6 SCAR=37.4
Pod documented
115 0 ▶ $id or return "Mandatory parameter id not given";
Branch: 1 branch
BranchTrueFalseTotal
$id or return "Mandatory parameter id not given" 0 0 0
116 0 ▶ return $CACHE->{STOP}->{$id} if ( $self->{cache} and $CACHE->{STOP}->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} and $WWW::PTV::CACHE->{"STOP"}{$id} 0 0 0
Condition0%0/3
$self->{"cache"} and $WWW::PTV::CACHE->{"STOP"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cache"} and $WWW::PTV::CACHE->{"STOP"}{$id}
$self->{"cache"} $WWW::PTV::CACHE->{"STOP"}{$id}
Truth table0%0/3
A and B
A= $self->{"cache"}B= $WWW::PTV::CACHE->{"STOP"}{$id}
ABresult
0X0
100
111
117 0 my $r = $self->__request( "/stop/view/$id" );
118 0 my $t = HTML::TreeBuilder->new_from_content( $r );
119 #my $t = HTML::TreeBuilder->new_from_file( './45470' );
120 0 my %stop = (id => $id );
121 0 my $s_type = $t->look_down( _tag => 'img', class => 'stopModeImage' );
122 0 $stop{address} = $t->look_down( _tag => 'div', id => 'container' )
123 ->look_down( _tag => 'p' )
124 ->as_text;
125  
126 0 $stop{transport_type} = $s_type->attr('src');
127 0 $stop{transport_type} =~ s|themes/transport-site/images/jp/icons/icon||;
128 0 $stop{transport_type} =~ s|\.png||;
129 0 ( $stop{street}, $stop{locality} ) = ( split /,/, $stop{address} );
130 0 ( $stop{postcode} ) = $stop{locality} =~ /\b(\d{4})\b/;
131 0 $stop{municipiality} = $t->look_down( _tag => 'table', class => 'stationSummary' )
132 ->look_down( _tag => 'a' )
133 ->as_text;
134  
135 0 ( $stop{municipiality_id} ) = $t->look_down( _tag => 'table', class => 'stationSummary' )
136 ->look_down( _tag => 'a' )
137 ->attr( 'href' );
138 0 ( $stop{municipiality_id} ) = $stop{municipiality_id} =~ /\/(\d.*)$/;
139  
140 0 $stop{zone} = ( $t->look_down( _tag => 'table', class => 'stationSummary' )
141 ->look_down( _tag => 'td' )
142 )[2]->as_text;
143  
144 0 $stop{map_ref} = $t->look_down( _tag => 'div', class => 'aside' )
145 ->look_down( _tag => 'a' )
146 ->attr( 'href' );
147  
148 0 ( $stop{latitude} ) = $stop{map_ref} =~ /=(-?\d.*),/;
149  
150 0 ( $stop{longitude} ) = $stop{map_ref} =~ /,(-?\d.*)$/;
151  
152 ( $stop{phone_feedback}, $stop{phone_station} )
153 0 = map { $_->as_text } $t->look_down( _tag => 'div', class => 'expander phone-numbers' )
154 ->look_down( _tag => 'dd' );
155  
156 0 $stop{staff_hours} = $t->look_down( _tag => 'div', 'data-cookie' => 'stop-staff' )
157 ->look_down( _tag => 'dd' )->as_text;
158  
159 ( $stop{myki_machines}, $stop{myki_checks}, $stop{vline_bookings} )
160 0 = map { $_->as_text } $t->look_down( _tag => 'div', 'data-cookie' => 'stop-ticketing' )
161 ->look_down( _tag => 'dd' );
162  
163 ( $stop{car_parking}, $stop{_bicycles}, $stop{taxi_rank} )
164 0 = map { $_->as_text } $t->look_down( _tag => 'div', 'data-cookie' => 'stop-other-transport-links' )
165 ->look_down( _tag => 'dd' );
166  
167 ( $stop{bicycle_racks}, $stop{bicycle_lockers}, $stop{bicycle_cage} )
168 0 = map { s/.*://; $_ } split /,/, $stop{_bicycles};
169  
170 ( $stop{wheelchair_accessible}, $stop{stairs}, $stop{escalator}, $stop{lift}, $stop{tactile_paths}, $stop{hearing_loop} )
171 0 = map { $_->as_text } $t->look_down( _tag => 'div', 'data-cookie' => 'stop-accessibility' )
172 ->look_down( _tag => 'dd' );
173  
174 ( $stop{seating}, $stop{lighting}, $stop{lockers}, $stop{public_phone}, $stop{public_toilet}, $stop{_waiting_area} )
175 0 = map { $_->as_text } $t->look_down( _tag => 'div', 'data-cookie' => 'stop-general-facilities' )
176 ->look_down( _tag => 'dd' );
177  
178 ( $stop{waiting_area_indoor}, $stop{waiting_area_sheltered} )
179 0 = map { s/.*://; $_ } split /,/, $stop{_waiting_area};
180  
181 0 foreach my $line ( $t->look_down( _tag => 'div', 'data-cookie' => 'stop-line-timetables' )
182 ->look_down( _tag => 'div', class => 'timetable-row' ) ) {
183 0 my $ref = { id => $line->look_down( _tag => 'a' )->attr( 'href' ) =~ /^.*\/(.*)/ };
184 0 $ref->{ name } = $line->look_down( _tag => 'a' )->as_text;
185 0 $ref->{ type } = _get_line_type( $line->look_down( _tag => 'img' )->attr( 'src' ) );
186 0 push @{ $stop{routes} }, $ref
187 }
188  
189 0 $stop{ua} = $self->{ua};
190  
191 0 my $stop = WWW::PTV::Stop->new( %stop );
192 0 ▶ $CACHE->{STOP}->{$id} = $stop if ( $self->{cache} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 0 0 0
193  
194 0 return $stop
195 }
196  
197 sub get_area_by_id {
198 0 ▶ my( $self, $id )= @_;
Subroutine get_area_by_id: not called CC=7 SCAR=40.3
Pod documented
199 0 ▶ $id or return "Mandatory parameter id not given";
Branch: 1 branch
BranchTrueFalseTotal
$id or return "Mandatory parameter id not given" 0 0 0
200 0 ▶ return $CACHE->{AREA}->{$id} if ( $self->{cache} and $CACHE->{AREA}->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} and $WWW::PTV::CACHE->{"AREA"}{$id} 0 0 0
Condition0%0/3
$self->{"cache"} and $WWW::PTV::CACHE->{"AREA"}{$id}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"cache"} and $WWW::PTV::CACHE->{"AREA"}{$id}
$self->{"cache"} $WWW::PTV::CACHE->{"AREA"}{$id}
Truth table0%0/3
A and B
A= $self->{"cache"}B= $WWW::PTV::CACHE->{"AREA"}{$id}
ABresult
0X0
100
111
201 0 my $r = $self->__request( "/location/view/$id" );
202 0 my $t = HTML::TreeBuilder->new_from_content( $r );
203 #my $t = HTML::TreeBuilder->new_from_file( './local_area_19' );
204 #my $t = HTML::TreeBuilder->new_from_file( './area_30' );
205 0 my %area = ( id => $id );
206 0 $area{name} = $t->look_down( _tag => 'h1' )->as_text;
207 0 @{ $area{suburbs}} = split /, /, $t->look_down( _tag => 'p' )->as_text;
208 0 my $t_type;
209  
210 0 foreach my $service ( $t->look_down( _tag => 'div', id => 'content' )->look_down( _tag => 'div' ) ) {
211 0 my $type = $service->look_down( _tag => 'h3' );
212  
213 0 ▶ if ( $type->as_text ne '' ) { $t_type = $type->as_text }
Branch: 1 branch
BranchTrueFalseTotal
if ($type->as_text ne "") 0 0 0
214  
215 0 @{ $area{service}{names}{$t_type} }
216 0 = map { $_->as_text } $service->look_down( _tag => 'li' );
217  
218 0 @{ $area{service}{links}{$t_type} }
219 0 = map { $_->attr( 'href' ) } $service->look_down( _tag => 'a' );
220 }
221  
222 0 my $area = WWW::PTV::Area->new( %area );
223 0 ▶ $CACHE->{AREA}->{$id} = $area if ( $self->{cache} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cache"} 0 0 0
224  
225 0 return $area
226 }
227  
228 sub get_local_areas {
229 0 ▶ my $self = shift;
Subroutine get_local_areas: not called CC=1 SCAR=6.9
Pod documented
230 0 my $r = $self->__request( '/getting-around/local-areas/' );
231 0 my $t = HTML::TreeBuilder->new_from_content( $r );
232 #my $t = HTML::TreeBuilder->new_from_file( './local_areas_list' );
233 0 $t = $t->look_down( _tag => 'div', id => 'content' );
234  
235 0 @{ $self->{local_areas}{names} }
236 0 = map { $_->as_text } $t->look_down( _tag => 'li' );
237  
238 0 @{ $self->{local_areas}{links} }
239 0 = map { $_->attr( 'href' ) } $t->look_down( _tag => 'a' );
240  
241 0 my %res;
242 0 @res{ @{ $self->{local_areas}{names} } } = @{ $self->{local_areas}{links} };
243 0 return %res
244 }
245  
246 sub _get_line_type {
247 0 ▶ my $obj = shift;
Subroutine _get_line_type: not called CC=1 SCAR=6.9
248 0 $obj =~ s/^.*\/icon//;
249 0 $obj =~ s/\..*$//;
250 0 return lc $obj
251 }
252  
253 2 1;
254  
255 __END__
↑ Summary PTV/Route.pm »