File Coverage

blib/lib/WWW/IRail/API/Vehicle.pm
Criterion Covered Total %
statement 21 23 91.3
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package WWW::IRail::API::Vehicle;
2             BEGIN {
3 1     1   2659 $WWW::IRail::API::Vehicle::AUTHORITY = 'cpan:ESSELENS';
4             }
5             BEGIN {
6 1     1   21 $WWW::IRail::API::Vehicle::VERSION = '0.003';
7             }
8 1     1   9 use strict;
  1         2  
  1         41  
9 1     1   4 use Carp qw/croak/;
  1         2  
  1         64  
10 1     1   6 use Date::Format;
  1         3  
  1         70  
11 1     1   6 use DateTime::Format::Natural;
  1         2  
  1         39  
12 1     1   16 use HTTP::Request::Common;
  1         2  
  1         176  
13 1     1   6 use JSON::XS;
  1         27  
  1         82  
14 1     1   507 use XML::Simple;
  0            
  0            
15             use YAML qw/freeze/;
16              
17              
18             sub make_request {
19             my %attr = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
20              
21             croak 'id (or vehicle) is a required argument' unless defined $attr{id} || $attr{vehicle};
22              
23             $attr{id} ||= $attr{vehicle};
24              
25             my $url = 'http://dev.api.irail.be/vehicle/?'.
26             join '&', map { $_.'='.$attr{$_} }
27             qw/id/;
28              
29             my $req = new HTTP::Request(GET => $url);
30              
31             return $req;
32             }
33              
34             sub parse_response {
35             my ($http_response, $dataType) = @_;
36              
37             my $obj = XMLin($http_response->content,
38             NoAttr => $dataType eq 'XML' ? 0 : 1,
39             SuppressEmpty => '',
40             NormaliseSpace => 2,
41             ForceArray => [ 'stop' ],
42             GroupTags => { stops => 'stop' },
43             KeyAttr => [],
44             );
45              
46             for ($dataType) {
47             /xml/i and return XMLout $obj, RootName=>'vehicleinformation', GroupTags => { stops => 'stop' };
48             /json/i and return JSON::XS->new->ascii->pretty->allow_nonref->encode($obj);
49             /yaml/i and return freeze $obj;
50             /perl/i and return $obj;
51             }
52              
53             return $obj; # default to perl
54              
55             }
56              
57             42;
58              
59              
60              
61             =pod
62              
63             =head1 VERSION
64              
65             version 0.003
66              
67             =head1 NAME
68              
69             WWW::IRail::API::Vehicle - HTTP::Request builder and HTTP::Response parser for the IRail API Vehicle data
70              
71             =head1 SYNOPSIS
72              
73             make_request( id => 'BE.NMBS.CR2089' );
74              
75             =head1 DESCRIPTION
76              
77             This module builds a L and has a parser for the
78             L. It's up to you to transmit it over the wire. If don't want
79             to do that yourself, don't use this module directly and use L
80             instead.
81              
82             =head1 METHODS
83              
84             =head2 make_request( I 'val'> | I<{ key => 'val' }> )
85              
86             C and C are the only arguments required, all time and date arguments default
87             to the current time and date on the iRail API side.
88              
89             make_request ( id => 'BE.NMBS.CR2089' );
90              
91             =head2 parse_response( I<$http_response>, I )
92              
93             parses the HTTP::Response you got back from the server, which if all went well contains XML.
94             That XML is then transformed into other data formats
95              
96             =over 4
97              
98             =item *
99              
100             xml
101              
102             =item *
103              
104             XML
105              
106             =item *
107              
108             YAML
109              
110             =item *
111              
112             JSON
113              
114             =item *
115              
116             perl (default)
117              
118             =back
119              
120             =head3 example of output when dataType = 'xml'
121              
122             =head1 METHODS
123              
124             =for xml
125            
126            
127            
128            
129            
130            
131            
132            
133            
134            
135            
136            
137            
138            
139            
140            
141            
142            
143              
144             =head3 example of output when dataType = 'XML'
145              
146             =for xml
147            
148            
149             AALST
150            
151            
152            
153             EREMBODEGEM
154            
155            
156            
157             DENDERLEEUW
158            
159            
160            
161             LIEDEKERKE
162            
163            
164            
165             ESSENE LOMBEEK
166            
167            
168            
169             TERNAT
170            
171            
172            
173             SINT MARTENS BODEGEM
174            
175            
176            
177             DILBEEK
178            
179            
180            
181             GROOT BIJGAARDEN
182            
183            
184            
185             BERCHEM SAINTE AGATHE
186            
187            
188            
189             JETTE
190            
191            
192            
193             BOCKSTAEL
194            
195            
196            
197             BRUSSELS NORD
198            
199            
200            
201             BRUSSELS CENTRAL
202            
203            
204            
205             BRUSSELS MIDI
206            
207            
208            
209             BE.NMBS.CR2089
210            
211              
212             =head3 example of output when dataType = 'JSON'
213              
214             =for json {
215             "stops" : [
216             { "station" : "AALST", "time" : "1291053960" },
217             { "station" : "EREMBODEGEM", "time" : "1291054200" },
218             { "station" : "DENDERLEEUW", "time" : "1291054500" },
219             { "station" : "LIEDEKERKE", "time" : "1291054740" },
220             { "station" : "ESSENE LOMBEEK", "time" : "1291054860" },
221             { "station" : "TERNAT", "time" : "1291055160" },
222             { "station" : "SINT MARTENS BODEGEM", "time" : "1291055340" },
223             { "station" : "DILBEEK", "time" : "1291055580" },
224             { "station" : "GROOT BIJGAARDEN", "time" : "1291055760" },
225             { "station" : "BERCHEM SAINTE AGATHE", "time" : "1291055880" },
226             { "station" : "JETTE", "time" : "1291056120" },
227             { "station" : "BOCKSTAEL", "time" : "1291056300" },
228             { "station" : "BRUSSELS NORD", "time" : "1291056720" },
229             { "station" : "BRUSSELS CENTRAL", "time" : "1291056960" },
230             { "station" : "BRUSSELS MIDI", "time" : "1291057140" }
231             ],
232             "vehicle" : "BE.NMBS.CR2089"
233             }
234              
235             =head3 example of output when dataType = 'YAML'
236              
237             =for YAML ---
238             stops:
239             - station: AALST
240             time: 1291053960
241             - station: EREMBODEGEM
242             time: 1291054200
243             - station: DENDERLEEUW
244             time: 1291054500
245             - station: LIEDEKERKE
246             time: 1291054740
247             - station: ESSENE LOMBEEK
248             time: 1291054860
249             - station: TERNAT
250             time: 1291055160
251             - station: SINT MARTENS BODEGEM
252             time: 1291055340
253             - station: DILBEEK
254             time: 1291055580
255             - station: GROOT BIJGAARDEN
256             time: 1291055760
257             - station: BERCHEM SAINTE AGATHE
258             time: 1291055880
259             - station: JETTE
260             time: 1291056120
261             - station: BOCKSTAEL
262             time: 1291056300
263             - station: BRUSSELS NORD
264             time: 1291056720
265             - station: BRUSSELS CENTRAL
266             time: 1291056960
267             - station: BRUSSELS MIDI
268             time: 1291057140
269             vehicle: BE.NMBS.CR2089
270              
271             =head3 example of output when dataType="perl" (default)
272              
273             =for perl $VAR1 = {
274             'stops' => [
275             {
276             'station' => 'AALST',
277             'time' => '1291053960'
278             },
279             {
280             'station' => 'EREMBODEGEM',
281             'time' => '1291054200'
282             },
283             {
284             'station' => 'DENDERLEEUW',
285             'time' => '1291054500'
286             },
287             {
288             'station' => 'LIEDEKERKE',
289             'time' => '1291054740'
290             },
291             {
292             'station' => 'ESSENE LOMBEEK',
293             'time' => '1291054860'
294             },
295             {
296             'station' => 'TERNAT',
297             'time' => '1291055160'
298             },
299             {
300             'station' => 'SINT MARTENS BODEGEM',
301             'time' => '1291055340'
302             },
303             {
304             'station' => 'DILBEEK',
305             'time' => '1291055580'
306             },
307             {
308             'station' => 'GROOT BIJGAARDEN',
309             'time' => '1291055760'
310             },
311             {
312             'station' => 'BERCHEM SAINTE AGATHE',
313             'time' => '1291055880'
314             },
315             {
316             'station' => 'JETTE',
317             'time' => '1291056120'
318             },
319             {
320             'station' => 'BOCKSTAEL',
321             'time' => '1291056300'
322             },
323             {
324             'station' => 'BRUSSELS NORD',
325             'time' => '1291056720'
326             },
327             {
328             'station' => 'BRUSSELS CENTRAL',
329             'time' => '1291056960'
330             },
331             {
332             'station' => 'BRUSSELS MIDI',
333             'time' => '1291057140'
334             }
335             ],
336             'vehicle' => 'BE.NMBS.CR2089'
337             };
338              
339             =head1 INSTALLATION
340              
341             See perlmodinstall for information and options on installing Perl modules.
342              
343             =head1 BUGS AND LIMITATIONS
344              
345             No bugs have been reported.
346              
347             Please report any bugs or feature requests through the web interface at
348             L.
349              
350             =head1 AUTHOR
351              
352             Tim Esselens
353              
354             =head1 COPYRIGHT AND LICENSE
355              
356             This software is copyright (c) 2010 by Tim Esselens.
357              
358             This is free software; you can redistribute it and/or modify it under
359             the same terms as the Perl 5 programming language system itself.
360              
361             =cut
362              
363              
364             __END__