| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::TubeUpdates; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 29792 | use Moose; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | use MooseX::Params::Validate; | 
| 5 |  |  |  |  |  |  | use Moose::Util::TypeConstraints; | 
| 6 |  |  |  |  |  |  | use namespace::clean; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | use Carp; | 
| 9 |  |  |  |  |  |  | use Data::Dumper; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use Readonly; | 
| 12 |  |  |  |  |  |  | use HTTP::Request; | 
| 13 |  |  |  |  |  |  | use LWP::UserAgent; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 NAME | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | WWW::TubeUpdates - Interface to Tube Updates API. | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 VERSION | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | Version 0.02 | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =cut | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | our $VERSION = '0.02'; | 
| 26 |  |  |  |  |  |  | Readonly my $BASE_URL   => 'http://api.tubeupdates.com/?method=get.status'; | 
| 27 |  |  |  |  |  |  | Readonly my $TUBE_LINES => | 
| 28 |  |  |  |  |  |  | { | 
| 29 |  |  |  |  |  |  | 'all'             => 'Wildcard for all lines', | 
| 30 |  |  |  |  |  |  | 'bakerloo'        => 'Bakerloo Line', | 
| 31 |  |  |  |  |  |  | 'central'         => 'Central Line', | 
| 32 |  |  |  |  |  |  | 'circle'          => 'Circle Line', | 
| 33 |  |  |  |  |  |  | 'district'        => 'District Line', | 
| 34 |  |  |  |  |  |  | 'docklands'       => 'Docklands', | 
| 35 |  |  |  |  |  |  | 'hammersmithcity' => 'Hammersmith & City Line', | 
| 36 |  |  |  |  |  |  | 'jubilee'         => 'Jubilee Line', | 
| 37 |  |  |  |  |  |  | 'metropolitan'    => 'Metropolitan Line', | 
| 38 |  |  |  |  |  |  | 'northern'        => 'Northern Line', | 
| 39 |  |  |  |  |  |  | 'piccadilly'      => 'Piccadilly Line', | 
| 40 |  |  |  |  |  |  | 'overground'      => 'London Overground system', | 
| 41 |  |  |  |  |  |  | 'tube'            => 'Wildcard for all tube lines (excludes DLR and Overground)', | 
| 42 |  |  |  |  |  |  | 'victoria'        => 'Victoria Line', | 
| 43 |  |  |  |  |  |  | 'waterloocity'    => 'Waterloo & City Line' | 
| 44 |  |  |  |  |  |  | }; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | A very lightweight wrapper for the Tube Updates REST API provided by tubeupdates.com. | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =cut | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | subtype 'TubeLine' | 
| 53 |  |  |  |  |  |  | => as 'Str' | 
| 54 |  |  |  |  |  |  | => where { _validateTubeLine($_) }; | 
| 55 |  |  |  |  |  |  | subtype 'ArrayRefOfTubeLine' | 
| 56 |  |  |  |  |  |  | => as 'ArrayRef[TubeLine]'; | 
| 57 |  |  |  |  |  |  | coerce 'ArrayRefOfTubeLine' | 
| 58 |  |  |  |  |  |  | => from 'TubeLine' | 
| 59 |  |  |  |  |  |  | => via { [ $_ ] } | 
| 60 |  |  |  |  |  |  | => from 'ArrayRef[Str]' | 
| 61 |  |  |  |  |  |  | => via { [ map { _coerceStrToTubeLine($_) } @$_ ] }; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | type 'Format'  => where { defined($_) && (/\bxml\b|\bjson\b/i) }; | 
| 64 |  |  |  |  |  |  | has  'format'  => (is => 'ro', isa => 'Format', default => 'json'); | 
| 65 |  |  |  |  |  |  | has  'browser' => (is => 'rw', isa => 'LWP::UserAgent', default => sub { return LWP::UserAgent->new(agent => 'Mozilla/5.0'); }); | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | around BUILDARGS => sub | 
| 68 |  |  |  |  |  |  | { | 
| 69 |  |  |  |  |  |  | my $orig  = shift; | 
| 70 |  |  |  |  |  |  | my $class = shift; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | if (@_ == 1 && ! ref $_[0]) | 
| 73 |  |  |  |  |  |  | { | 
| 74 |  |  |  |  |  |  | return $class->$orig(format => $_[0]); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | else | 
| 77 |  |  |  |  |  |  | { | 
| 78 |  |  |  |  |  |  | return $class->$orig(@_); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | }; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | =head1 CONSTRUCTOR | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | The constructor requires optionally format type. It can be either XML / JSON. The default type | 
| 85 |  |  |  |  |  |  | is JSON. | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | use strict; use warnings; | 
| 88 |  |  |  |  |  |  | use WWW::TubeUpdates; | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | my ($tube); | 
| 91 |  |  |  |  |  |  | $tube = WWW::TubeUpdates->new('xml'); | 
| 92 |  |  |  |  |  |  | # or | 
| 93 |  |  |  |  |  |  | $tube = WWW::TubeUpdates->new(format => 'xml'); | 
| 94 |  |  |  |  |  |  | # or | 
| 95 |  |  |  |  |  |  | $tube = WWW::TubeUpdates->new({format => 'xml'}); | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head1 METHODS | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head2 getStatus() | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | Returns the status of the given tube lines. Tube lines can be passed in as list or reference | 
| 102 |  |  |  |  |  |  | to a list. Following is the list of valid tube lines: | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | +-----------------+-----------------------------------------------------------+ | 
| 105 |  |  |  |  |  |  | | Id              | Description                                               | | 
| 106 |  |  |  |  |  |  | +-----------------+-----------------------------------------------------------+ | 
| 107 |  |  |  |  |  |  | | all             | Wildcard for all lines                                    | | 
| 108 |  |  |  |  |  |  | | bakerloo        | Bakerloo Line                                             | | 
| 109 |  |  |  |  |  |  | | central         | Central Line                                              | | 
| 110 |  |  |  |  |  |  | | circle          | Circle Line                                               | | 
| 111 |  |  |  |  |  |  | | district        | District Line                                             | | 
| 112 |  |  |  |  |  |  | | docklands       | Docklands                                                 | | 
| 113 |  |  |  |  |  |  | | hammersmithcity | Hammersmith & City Line                                   | | 
| 114 |  |  |  |  |  |  | | jubilee         | Jubilee Line                                              | | 
| 115 |  |  |  |  |  |  | | metropolitan    | Metropolitan Line                                         | | 
| 116 |  |  |  |  |  |  | | northern        | Northern Line                                             | | 
| 117 |  |  |  |  |  |  | | piccadilly      | Piccadilly Line                                           | | 
| 118 |  |  |  |  |  |  | | overground      | London Overground system                                  | | 
| 119 |  |  |  |  |  |  | | tube            | Wildcard for all tube lines (excludes DLR and Overground) | | 
| 120 |  |  |  |  |  |  | | victoria        | Victoria Line                                             | | 
| 121 |  |  |  |  |  |  | | waterloocity    | Waterloo & City Line                                      | | 
| 122 |  |  |  |  |  |  | +-----------------+-----------------------------------------------------------+ | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | use strict; use warnings; | 
| 125 |  |  |  |  |  |  | use WWW::TubeUpdates; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | my $tube   = WWW::TubeUpdates->new('xml'); | 
| 128 |  |  |  |  |  |  | print $tube->getStatus('circle') . "\n"; | 
| 129 |  |  |  |  |  |  | # or | 
| 130 |  |  |  |  |  |  | print $tube->getStatus(['circle']) . "\n"; | 
| 131 |  |  |  |  |  |  | # or | 
| 132 |  |  |  |  |  |  | print $tube->getStatus('circle', 'bakerloo') . "\n"; | 
| 133 |  |  |  |  |  |  | # or | 
| 134 |  |  |  |  |  |  | print $tube->getStatus(['circle', 'bakerloo']) . "\n"; | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =cut | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | around 'getStatus' => sub | 
| 139 |  |  |  |  |  |  | { | 
| 140 |  |  |  |  |  |  | my $orig  = shift; | 
| 141 |  |  |  |  |  |  | my $class = shift; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | if (@_ > 1 && !ref $_[0]) | 
| 144 |  |  |  |  |  |  | { | 
| 145 |  |  |  |  |  |  | return $class->$orig([@_]); | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  | else | 
| 148 |  |  |  |  |  |  | { | 
| 149 |  |  |  |  |  |  | return $class->$orig(@_); | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  | }; | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | sub getStatus | 
| 154 |  |  |  |  |  |  | { | 
| 155 |  |  |  |  |  |  | my $self    = shift; | 
| 156 |  |  |  |  |  |  | my ($lines) = pos_validated_list(\@_, | 
| 157 |  |  |  |  |  |  | { isa => 'ArrayRefOfTubeLine', coerce => 1, required => 1 }, | 
| 158 |  |  |  |  |  |  | MX_PARAMS_VALIDATE_NO_CACHE => 1); | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | my ($browser, $url, $request, $response, $content); | 
| 161 |  |  |  |  |  |  | $browser = $self->browser; | 
| 162 |  |  |  |  |  |  | $browser->env_proxy; | 
| 163 |  |  |  |  |  |  | $url = sprintf("%s&lines=%s", $BASE_URL, join(",",@$lines)); | 
| 164 |  |  |  |  |  |  | $url.= sprintf("&format=%s", $self->format); | 
| 165 |  |  |  |  |  |  | $request  = HTTP::Request->new(GET => $url); | 
| 166 |  |  |  |  |  |  | $response = $browser->request($request); | 
| 167 |  |  |  |  |  |  | croak("ERROR: Couldn't fetch data [$url]:[".$response->status_line."]\n") | 
| 168 |  |  |  |  |  |  | unless $response->is_success; | 
| 169 |  |  |  |  |  |  | $content  = $response->content; | 
| 170 |  |  |  |  |  |  | croak("ERROR: No data found.\n") unless defined $content; | 
| 171 |  |  |  |  |  |  | return $content; | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | sub _validateTubeLine | 
| 175 |  |  |  |  |  |  | { | 
| 176 |  |  |  |  |  |  | my $data = shift; | 
| 177 |  |  |  |  |  |  | return 1 if (defined($data) && exists($TUBE_LINES->{lc($data)})); | 
| 178 |  |  |  |  |  |  | return (); | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | sub _coerceStrToTubeLine | 
| 182 |  |  |  |  |  |  | { | 
| 183 |  |  |  |  |  |  | my $str = shift; | 
| 184 |  |  |  |  |  |  | return $str if _validateTubeLine($str); | 
| 185 |  |  |  |  |  |  | warn("ERROR: Invalid tube line [$str]."); | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =head1 AUTHOR | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >> | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | =head1 BUGS | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | Please report any bugs or feature requests to C<bug-www-tubeupdates at rt.cpan.org> or through | 
| 195 |  |  |  |  |  |  | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-TubeUpdates>. I will | 
| 196 |  |  |  |  |  |  | be notified & then you'll automatically be notified of progress on your bug as I make changes. | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | =head1 SUPPORT | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | perldoc WWW::TubeUpdates | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | You can also look for information at: | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =over 4 | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =item * RT: CPAN's request tracker | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-TubeUpdates> | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | L<http://annocpan.org/dist/WWW-TubeUpdates> | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | L<http://cpanratings.perl.org/d/WWW-TubeUpdates> | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =item * Search CPAN | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | L<http://search.cpan.org/dist/WWW-TubeUpdates/> | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =back | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENT | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | Ben Dodson (author of TubeUpdates REST API). | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | Copyright 2011 Mohammad S Anwar. | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | This  program  is  free  software; you can redistribute it and/or modify it under the terms of | 
| 235 |  |  |  |  |  |  | either:  the  GNU  General Public License as published by the Free Software Foundation; or the | 
| 236 |  |  |  |  |  |  | Artistic License. | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | See http://dev.perl.org/licenses/ for more information. | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | =head1 DISCLAIMER | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | This  program  is  distributed in the hope that it will be useful,  but  WITHOUT ANY WARRANTY; | 
| 243 |  |  |  |  |  |  | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | =cut | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 248 |  |  |  |  |  |  | no Moose; # Keywords are removed from the WWW::TubeUpdates package | 
| 249 |  |  |  |  |  |  | no Moose::Util::TypeConstraints; | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | 1; # End of WWW::TubeUpdates |