File Coverage

blib/lib/WWW/Velib/Station.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             # Station.pm - WWW::Velib::Station
2             #
3             # Copyright (c) 2007 David Landgren
4             # All rights reserved
5              
6             package WWW::Velib::Station;
7 2     2   4327 use strict;
  2         5  
  2         100  
8              
9 2     2   875 use LWP::Simple;
  2         93580  
  2         21  
10 2     2   5424 use Math::Trig qw(deg2rad great_circle_distance);
  2         46202  
  2         265  
11 2     2   1311 use XML::Twig;
  0            
  0            
12              
13             use vars '$VERSION';
14             $VERSION = '0.01';
15              
16             use constant DETAILS => 'http://www.velib.paris.fr/service/stationdetails/';
17              
18             sub new {
19             my $class = shift;
20             my $self = bless {number => shift}, $class;
21             $self->refresh;
22             return $self;
23             }
24              
25             sub make {
26             my $class = shift;
27             my $self = {};
28             @{$self}{qw(number name address fullAddress lat lng open)} = @_;
29             @{$self}{qw(theta phi)} = _theta_phi(@{$self}{qw(lat lng)});
30             return bless $self, $class;
31             }
32              
33             sub _theta_phi { deg2rad($_[0]), deg2rad(90 - $_[1]) }
34              
35             sub load_v1 {
36             my $class = shift;
37             my $self = {};
38             @{$self}{qw(number open lat lng theta phi name address fullAddress)} = @_;
39             return bless $self, $class;
40             }
41              
42             sub coords {
43             my $self = shift;
44             return @{$self}{qw(theta phi)};
45             }
46              
47             sub distance_from {
48             my $self = shift;
49             my $there = shift;
50             my $scale = shift || 5;
51             $self->{dist} = $scale * sprintf( '%0.0f',
52             great_circle_distance( $there->coords, $self->coords, 6378249)
53             / $scale);
54             return $self->{dist};
55             }
56              
57             sub refresh {
58             my $self = shift;
59             if (my $content = get(DETAILS . $self->number)) {
60             $self->{_html} = $content;
61             my $twig = XML::Twig->new(
62             twig_handlers => {
63             available => sub {$self->{available} = $_[1]->text},
64             free => sub {$self->{free } = $_[1]->text},
65             total => sub {$self->{total } = $_[1]->text},
66             ticket => sub {$self->{ticket } = $_[1]->text},
67             },
68             );
69             $twig->parse($content);
70             }
71             else {
72             @{$self}{qw(available free total ticket)} = (-1) x 4;
73             };
74             return $self;
75             }
76              
77             sub number {return $_[0]->{number }}
78             sub open {return $_[0]->{open }}
79             sub name {return $_[0]->{name }}
80             sub latitude {return $_[0]->{lat }}
81             sub longitude {return $_[0]->{lng }}
82             sub address {return $_[0]->{address }}
83             sub full_address {return $_[0]->{fullAddress}}
84             sub available {return $_[0]->{available }}
85             sub free {return $_[0]->{free }}
86             sub total {return $_[0]->{total }}
87             sub disabled {
88             return $_[0]->total == -1
89             ? -1
90             : $_[0]->total - ($_[0]->free + $_[0]->available)
91             ;
92             }
93              
94             'The Lusty Decadent Delights of Imperial Pompeii';
95             __END__