line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::IRail::API::Stations; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
2
|
|
|
2
|
|
49
|
$WWW::IRail::API::Stations::AUTHORITY = 'cpan:ESSELENS'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
2
|
|
|
2
|
|
31
|
$WWW::IRail::API::Stations::VERSION = '0.003'; |
7
|
|
|
|
|
|
|
} |
8
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
53
|
|
9
|
2
|
|
|
2
|
|
11
|
use Carp qw/croak/; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
93
|
|
10
|
2
|
|
|
2
|
|
16971
|
use Date::Format; |
|
2
|
|
|
|
|
37679
|
|
|
2
|
|
|
|
|
130
|
|
11
|
2
|
|
|
2
|
|
2143
|
use DateTime::Format::Natural; |
|
2
|
|
|
|
|
655459
|
|
|
2
|
|
|
|
|
164
|
|
12
|
2
|
|
|
2
|
|
2839
|
use HTTP::Request::Common; |
|
2
|
|
|
|
|
71870
|
|
|
2
|
|
|
|
|
170
|
|
13
|
2
|
|
|
2
|
|
1077
|
use JSON::XS; |
|
2
|
|
|
|
|
13212
|
|
|
2
|
|
|
|
|
144
|
|
14
|
2
|
|
|
2
|
|
2505
|
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
|
|
|
|
|
|
|
$attr{lang} ||= 'en'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
croak "lang must match qr/(en | nl | fr | de)/x" unless $attr{lang} =~ m/en | nl | fr | de/x; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $url = 'http://dev.api.irail.be/stations/?'. |
26
|
|
|
|
|
|
|
join '&', map { $_.'='.$attr{$_} } |
27
|
|
|
|
|
|
|
qw/lang/; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $req = new HTTP::Request(GET => $url); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
return $req; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub parse_response { |
36
|
|
|
|
|
|
|
my ($http_response, $dataType, $filter) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $obj = XMLin($http_response->decoded_content, |
39
|
|
|
|
|
|
|
NoAttr => $dataType eq 'XML' ? 0 : 1, |
40
|
|
|
|
|
|
|
SuppressEmpty => '', |
41
|
|
|
|
|
|
|
NormaliseSpace => 2, |
42
|
|
|
|
|
|
|
ForceArray => [ 'station' ], |
43
|
|
|
|
|
|
|
GroupTags => { stations => 'station'}, |
44
|
|
|
|
|
|
|
KeyAttr => [], |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$obj->{station} = [grep { $filter->(lc $_) } @{$obj->{station}}] if ref $filter eq "CODE"; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
for ($dataType) { |
50
|
|
|
|
|
|
|
/xml/i and return XMLout $obj, RootName=>'stations', GroupTags => { stations => 'station' }; |
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->{station}; |
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::Stations - HTTP::Request builder and HTTP::Response parser for the IRail API Station data |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 SYNOPSIS |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
use WWW::IRail::API::Stations; |
77
|
|
|
|
|
|
|
use LWP::UserAgent(); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $ua = new LWP::UserAgent(); |
80
|
|
|
|
|
|
|
$ua->timeout(20); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $station_req = WWW::IRail::API::Stations::make_request(); |
83
|
|
|
|
|
|
|
my $http_resp = $ua->request($station_req); |
84
|
|
|
|
|
|
|
my $result = WWW::IRail::API::Stations::parse_response($http_resp,'perl'); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 DESCRIPTION |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
This module builds a L and has a parser for the |
89
|
|
|
|
|
|
|
L. It's up to you to transmit it over the wire. If don't want |
90
|
|
|
|
|
|
|
to do that yourself, don't use this module directly and use L |
91
|
|
|
|
|
|
|
instead. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 METHODS |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 make_request() |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Has no arguments, requests the whole list of stations from the API |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 parse_response( I<{$http_response}>, I<"dataType">, I ) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
parses the HTTP::Response you got back from the server, which if all went well contains XML. |
102
|
|
|
|
|
|
|
That XML is then transformed into other data formats. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Note that the perl data format returns the data unnested for easier access. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=over 4 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item * |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
xml |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item * |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
XML |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item * |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
YAML |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item * |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
JSON |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
perl (default) |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=back |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head3 example of output when dataType = 'xml' |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
\'S GRAVENBRAKEL |
134
|
|
|
|
|
|
|
AALST |
135
|
|
|
|
|
|
|
AALST KERREBROEK |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head3 example of output when dataType = 'XML' |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
\'S GRAVENBRAKEL |
145
|
|
|
|
|
|
|
AALST |
146
|
|
|
|
|
|
|
AALST KERREBROEK |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head3 example of output when dataType = 'JSON' |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
{ |
155
|
|
|
|
|
|
|
"station" : [ |
156
|
|
|
|
|
|
|
"\'S GRAVENBRAKEL", |
157
|
|
|
|
|
|
|
"AALST", |
158
|
|
|
|
|
|
|
"AALST KERREBROEK", |
159
|
|
|
|
|
|
|
"AALTER", |
160
|
|
|
|
|
|
|
// ... |
161
|
|
|
|
|
|
|
] |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head3 example of output when dataType = 'YAML' |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
station: |
167
|
|
|
|
|
|
|
- "\'S GRAVENBRAKEL" |
168
|
|
|
|
|
|
|
- AALST |
169
|
|
|
|
|
|
|
- AALST KERREBROEK |
170
|
|
|
|
|
|
|
- AALTER |
171
|
|
|
|
|
|
|
... |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head3 example of output when dataType="perl" (default) |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
$VAR1 = [ |
176
|
|
|
|
|
|
|
'\'S GRAVENBRAKEL', |
177
|
|
|
|
|
|
|
'AALST', |
178
|
|
|
|
|
|
|
'AALST KERREBROEK', |
179
|
|
|
|
|
|
|
'AALTER', |
180
|
|
|
|
|
|
|
'AARLEN', |
181
|
|
|
|
|
|
|
'AARSCHOT', |
182
|
|
|
|
|
|
|
# ... |
183
|
|
|
|
|
|
|
] |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 METHODS |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 SEE ALSO |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=over 4 |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item * |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
L |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=back |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 INSTALLATION |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
See perlmodinstall for information and options on installing Perl modules. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head1 BUGS AND LIMITATIONS |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
No bugs have been reported. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Please report any bugs or feature requests through the web interface at |
206
|
|
|
|
|
|
|
L. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 AUTHOR |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Tim Esselens |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Tim Esselens. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
217
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
__END__ |