| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::IRail::API::Liveboard; | 
| 2 |  |  |  |  |  |  | BEGIN { | 
| 3 | 1 |  |  | 1 |  | 2564 | $WWW::IRail::API::Liveboard::AUTHORITY = 'cpan:ESSELENS'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | BEGIN { | 
| 6 | 1 |  |  | 1 |  | 18 | $WWW::IRail::API::Liveboard::VERSION = '0.003'; | 
| 7 |  |  |  |  |  |  | } | 
| 8 | 1 |  |  | 1 |  | 8 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 9 | 1 |  |  | 1 |  | 5 | use Carp qw/croak/; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 10 | 1 |  |  | 1 |  | 6 | use HTTP::Request::Common; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 70 |  | 
| 11 | 1 |  |  | 1 |  | 5 | use JSON::XS; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 12 | 1 |  |  | 1 |  | 542 | use XML::Simple; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | use YAML qw/freeze/; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub make_request { | 
| 16 |  |  |  |  |  |  | my %attr = ref $_[0] eq 'HASH' ? %{$_[0]} : @_; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | $attr{station} ||= $attr{from} || $attr{to}; | 
| 19 |  |  |  |  |  |  | $attr{direction} ||= $attr{dir} || $attr{from} ? 'DEP' : $attr{to} ? 'ARR' : 'DEP'; | 
| 20 |  |  |  |  |  |  | $attr{direction} =~ s/^(arr|dep).*/\U$1/i; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | croak 'too few arguments' unless $attr{station}; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | my %urimap = ( direction => 'arrdep' ); | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | my $url = 'http://dev.api.irail.be/liveboard/?'. | 
| 27 |  |  |  |  |  |  | join '&', map { ($urimap{$_} || $_) .'='.$attr{$_} } | 
| 28 |  |  |  |  |  |  | qw/station direction/; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | my $req = new HTTP::Request(GET => $url); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | return $req; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub parse_response { | 
| 36 |  |  |  |  |  |  | my ($http_response, $dataType) = @_; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | my $obj = XMLin($http_response->content, | 
| 39 |  |  |  |  |  |  | NoAttr => $dataType eq 'XML' ? 0 : 1, | 
| 40 |  |  |  |  |  |  | SuppressEmpty => 1, | 
| 41 |  |  |  |  |  |  | NormaliseSpace => 2, | 
| 42 |  |  |  |  |  |  | ForceArray => [ 'departure', 'arrival' ], | 
| 43 |  |  |  |  |  |  | KeyAttr => [], | 
| 44 |  |  |  |  |  |  | GroupTags => { departures => 'departure', arrivals => 'arrival' }, | 
| 45 |  |  |  |  |  |  | ); | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | ($obj->{timestamp}) = ($http_response->content =~ m/timestamp="(\d+)"/); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | for ($dataType) { | 
| 50 |  |  |  |  |  |  | /xml/i and return XMLout $obj, RootName => 'liveboard', GroupTags => { departures => 'departure', arrivals => 'arrival' }; | 
| 51 |  |  |  |  |  |  | /json/i and return JSON::XS->new->ascii->pretty->allow_nonref->encode($obj); | 
| 52 |  |  |  |  |  |  | /yaml/i and return freeze $obj; | 
| 53 |  |  |  |  |  |  | /perl/i and return $obj; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | return $obj; # default to perl | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | 42; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =pod | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =head1 VERSION | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | version 0.003 | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =head1 NAME | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | WWW::IRail::API::Liveboard - HTTP::Request builder and HTTP::Response parser for the IRail API Liveboard data | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | make_request ( station => 'oostende' );     # departures by default | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | make_request ( to => 'oostende' );          # arrivals coming into oostende | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | This module builds a L and has a parser for the | 
| 83 |  |  |  |  |  |  | L. It's up to you to transmit it over the wire. If don't want | 
| 84 |  |  |  |  |  |  | to do that yourself, don't use this module directly and use L | 
| 85 |  |  |  |  |  |  | instead. | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =head1 METHODS | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head2 make_request( I 'val'> | I<{ key => 'val' }> ) | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Only the C argument is required, but if you either use C or C it will be seen as | 
| 92 |  |  |  |  |  |  | the station name with the direction set accordingly. Memento: trains leaving B => 'oostende' and | 
| 93 |  |  |  |  |  |  | trains arriving inB => 'oostende'. If the direction could not be deduced it defaults to C<'departures'> | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | make_request ( from => 'oostende' ); | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | make_request ( station => 'oostende', direction => 'departures' ); | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | make_request ( { station => 'oostende', dir => 'arr' } ); | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | the API direction parameter is extracted from your input using C | 
| 102 |  |  |  |  |  |  | so you can choose between less typing or better readability. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head2 parse_response( I<$http_response>, I ) | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | rses the HTTP::Response you got back from the server, which if all went well contains XML. | 
| 107 |  |  |  |  |  |  | That XML is then transformed into other data formats | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =over 4 | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =item * | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | xml | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =item * | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | XML | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =item * | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | YAML | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =item * | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | JSON | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =item * | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | perl (default) | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | =back | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head3 example of output when dataType = 'xml' | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head3 example of output when dataType = 'XML' | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | 1 | 
| 150 |  |  |  |  |  |  | DENDERMONDE | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | 1 | 
| 155 |  |  |  |  |  |  | DENDERMONDE | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | 2 | 
| 160 |  |  |  |  |  |  | GERAARDSBERGEN | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | 1 | 
| 165 |  |  |  |  |  |  | DENDERMONDE | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | MOLLEM | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =head3 example of output when dataType = 'YAML' | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | --- | 
| 175 |  |  |  |  |  |  | departures: | 
| 176 |  |  |  |  |  |  | - platform: 1 | 
| 177 |  |  |  |  |  |  | station: DENDERMONDE | 
| 178 |  |  |  |  |  |  | time: 1291043100 | 
| 179 |  |  |  |  |  |  | vehicle: BE.NMBS.CR1564 | 
| 180 |  |  |  |  |  |  | - platform: 1 | 
| 181 |  |  |  |  |  |  | station: DENDERMONDE | 
| 182 |  |  |  |  |  |  | time: 1291044480 | 
| 183 |  |  |  |  |  |  | vehicle: BE.NMBS.CR5316 | 
| 184 |  |  |  |  |  |  | - platform: 2 | 
| 185 |  |  |  |  |  |  | station: GERAARDSBERGEN | 
| 186 |  |  |  |  |  |  | time: 1291046100 | 
| 187 |  |  |  |  |  |  | vehicle: BE.NMBS.CR1588 | 
| 188 |  |  |  |  |  |  | - platform: 1 | 
| 189 |  |  |  |  |  |  | station: DENDERMONDE | 
| 190 |  |  |  |  |  |  | time: 1291046700 | 
| 191 |  |  |  |  |  |  | vehicle: BE.NMBS.CR1565 | 
| 192 |  |  |  |  |  |  | station: MOLLEM | 
| 193 |  |  |  |  |  |  | timestamp: 1291044267 | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =head3 example of output when dataType = 'JSON' | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | { | 
| 198 |  |  |  |  |  |  | "departures" : [ | 
| 199 |  |  |  |  |  |  | { | 
| 200 |  |  |  |  |  |  | "station" : "DENDERMONDE", | 
| 201 |  |  |  |  |  |  | "time" : "1291043100", | 
| 202 |  |  |  |  |  |  | "vehicle" : "BE.NMBS.CR1564", | 
| 203 |  |  |  |  |  |  | "platform" : "1" | 
| 204 |  |  |  |  |  |  | }, | 
| 205 |  |  |  |  |  |  | { | 
| 206 |  |  |  |  |  |  | "station" : "DENDERMONDE", | 
| 207 |  |  |  |  |  |  | "time" : "1291044480", | 
| 208 |  |  |  |  |  |  | "vehicle" : "BE.NMBS.CR5316", | 
| 209 |  |  |  |  |  |  | "platform" : "1" | 
| 210 |  |  |  |  |  |  | }, | 
| 211 |  |  |  |  |  |  | { | 
| 212 |  |  |  |  |  |  | "station" : "GERAARDSBERGEN", | 
| 213 |  |  |  |  |  |  | "time" : "1291046100", | 
| 214 |  |  |  |  |  |  | "vehicle" : "BE.NMBS.CR1588", | 
| 215 |  |  |  |  |  |  | "platform" : "2" | 
| 216 |  |  |  |  |  |  | }, | 
| 217 |  |  |  |  |  |  | { | 
| 218 |  |  |  |  |  |  | "station" : "DENDERMONDE", | 
| 219 |  |  |  |  |  |  | "time" : "1291046700", | 
| 220 |  |  |  |  |  |  | "vehicle" : "BE.NMBS.CR1565", | 
| 221 |  |  |  |  |  |  | "platform" : "1" | 
| 222 |  |  |  |  |  |  | } | 
| 223 |  |  |  |  |  |  | ], | 
| 224 |  |  |  |  |  |  | "station" : "MOLLEM", | 
| 225 |  |  |  |  |  |  | "timestamp" : "1291044295" | 
| 226 |  |  |  |  |  |  | } | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | =head3 example of output when dataType = 'perl' | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | { | 
| 231 |  |  |  |  |  |  | 'departures' => [ | 
| 232 |  |  |  |  |  |  | { | 
| 233 |  |  |  |  |  |  | 'station' => 'DENDERMONDE', | 
| 234 |  |  |  |  |  |  | 'time' => '1291043100', | 
| 235 |  |  |  |  |  |  | 'vehicle' => 'BE.NMBS.CR1564', | 
| 236 |  |  |  |  |  |  | 'platform' => '1' | 
| 237 |  |  |  |  |  |  | }, | 
| 238 |  |  |  |  |  |  | { | 
| 239 |  |  |  |  |  |  | 'station' => 'DENDERMONDE', | 
| 240 |  |  |  |  |  |  | 'time' => '1291044480', | 
| 241 |  |  |  |  |  |  | 'vehicle' => 'BE.NMBS.CR5316', | 
| 242 |  |  |  |  |  |  | 'platform' => '1' | 
| 243 |  |  |  |  |  |  | }, | 
| 244 |  |  |  |  |  |  | { | 
| 245 |  |  |  |  |  |  | 'station' => 'GERAARDSBERGEN', | 
| 246 |  |  |  |  |  |  | 'time' => '1291046100', | 
| 247 |  |  |  |  |  |  | 'vehicle' => 'BE.NMBS.CR1588', | 
| 248 |  |  |  |  |  |  | 'platform' => '2' | 
| 249 |  |  |  |  |  |  | }, | 
| 250 |  |  |  |  |  |  | { | 
| 251 |  |  |  |  |  |  | 'station' => 'DENDERMONDE', | 
| 252 |  |  |  |  |  |  | 'time' => '1291046700', | 
| 253 |  |  |  |  |  |  | 'vehicle' => 'BE.NMBS.CR1565', | 
| 254 |  |  |  |  |  |  | 'platform' => '1' | 
| 255 |  |  |  |  |  |  | } | 
| 256 |  |  |  |  |  |  | ], | 
| 257 |  |  |  |  |  |  | 'station' => 'MOLLEM', | 
| 258 |  |  |  |  |  |  | 'timestamp' => '1291044344' | 
| 259 |  |  |  |  |  |  | }; | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | =head1 METHODS | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | =head1 INSTALLATION | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | See perlmodinstall for information and options on installing Perl modules. | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | =head1 BUGS AND LIMITATIONS | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | No bugs have been reported. | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | Please report any bugs or feature requests through the web interface at | 
| 272 |  |  |  |  |  |  | L. | 
| 273 |  |  |  |  |  |  |  | 
| 274 |  |  |  |  |  |  | =head1 AUTHOR | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  | Tim Esselens | 
| 277 |  |  |  |  |  |  |  | 
| 278 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 279 |  |  |  |  |  |  |  | 
| 280 |  |  |  |  |  |  | This software is copyright (c) 2010 by Tim Esselens. | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 283 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 284 |  |  |  |  |  |  |  | 
| 285 |  |  |  |  |  |  | =cut | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  |  | 
| 288 |  |  |  |  |  |  | __END__ |