File Coverage

blib/lib/WWW/PTV/Route.pm
Criterion Covered Total %
statement 21 82 25.6
branch 0 28 0.0
condition 0 13 0.0
subroutine 7 14 50.0
pod 3 4 75.0
total 31 141 21.9


line stmt bran cond sub pod time code
1             package WWW::PTV::Route;
2              
3 2     2   10 use strict;
  2         3  
  2         47  
4 2     2   11 use warnings;
  2         4  
  2         49  
5              
6 2     2   1047 use WWW::PTV::TimeTable;
  2         4  
  2         54  
7 2     2   11 use HTML::TreeBuilder;
  2         4  
  2         12  
8 2     2   55 use Scalar::Util qw(weaken);
  2         3  
  2         97  
9 2     2   10 use Carp qw(croak);
  2         4  
  2         167  
10              
11             our $STOP = {};
12             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             foreach my $attr ( @ATTR ) {
16             {
17 2     2   10 no strict 'refs';
  2         3  
  2         2284  
18             *{ __PACKAGE__ .'::'. $attr } = sub {
19 0     0     my( $self, $val ) = @_;
20 0 0         $self->{$attr} = $val if $val;
21 0           return $self->{$attr}
22             }
23             }
24             }
25              
26             sub new {
27 0     0 0   my ( $class, %args ) = @_;
28              
29 0           my $self = bless {}, $class;
30 0 0         $args{id} or croak 'Constructor failed: mandatory id argument not supplied';
31              
32 0           foreach my $attr ( @ATTR ) { $self->{$attr} = $args{$attr} }
  0            
33              
34 0           $self->{uri} = $args{uri};
35 0 0         $args{ua} ? weaken( $self->{ua} = $args{ua} ) : croak 'Mandatory argument ua not supplied';
36              
37 0           return $self
38             }
39              
40 0     0 1   sub get_inbound_tt { $_[0]->__get_tt( 'in' ) }
41              
42 0     0 1   sub get_outbound_tt { $_[0]->__get_tt( 'out' ) }
43              
44             sub __get_tt {
45 0     0     my ( $self, $direction ) = @_;
46              
47 0 0         return unless $direction =~ /(in|out)/;
48              
49             my $tt = $self->__request( $direction eq 'out'
50             ? $self->{direction_out_link}
51             : $self->{direction_in_link}
52 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 0 0       if( ( defined $_->attr( 'http-equiv' ) )
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 0 0       if ( $tt && $tt->attr( 'onclick' ) && $tt->attr( 'onclick' ) =~ /TTB_REQUEST/ ) {
      0        
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;
  0            
81 0           my @stop_links = map { my ($r) = $_->look_down( _tag => 'a' )->attr( 'href' ) =~ /.*\/(\d*$)/ } @stops;
  0            
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 0 0       push @{ $s }, $h and next if $h !~ /\d/;
  0            
91 0           my $is_pm = $t->look_down( _tag => 'b' );
92              
93 0 0         push @{ $s }, ( $h == 12
  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
  0            
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             sub __request {
111 0     0     my ( $self, $uri ) = @_;
112              
113             my $res = ( $uri !~ /^http:/
114             ? $self->{ua}->get( $self->{uri} . $uri )
115 0 0         : $self->{ua}->get( $uri ) );
116              
117 0 0         return $res->content if $res->is_success;
118              
119 0           croak 'Unable to retrieve content: ' . $res->status_line
120             }
121              
122             sub get_stop_names_and_ids {
123 0     0 1   my ( $self, $direction ) = @_;
124              
125 0   0       $direction ||= 'out';
126 0 0         $self->{timetable}->{$direction} || $self->__get_tt($direction);
127              
128 0           return $self->{timetable}->{$direction}->stop_names_and_ids;
129             }
130              
131             1;
132              
133             __END__