| 1 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | package WebService::KVV::Live::Stop; | ||||||||||||||
| 4 | |||||||||||||||
| 5 | # ABSTRACT: Arrival times for Trams/Buses in the Karlsruhe metropolitan area | ||||||||||||||
| 6 | 4 | our $VERSION = '0.007'; # VERSION | |||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use Net::HTTP::Spore::Middleware::Format::JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use Net::HTTP::Spore 0.08; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 4 | ▶ | use Net::HTTP::Spore::Middleware::DefaultParams; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 4 | ▶ | use File::ShareDir 'dist_file'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | =pod | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =encoding utf8 | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 NAME | ||||||||||||||
| 20 | |||||||||||||||
| 21 | WebService::KVV::Live::Stop - Arrival times for Trams/Buses in the Karlsruhe metropolitan area | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||
| 25 | |||||||||||||||
| 26 | use WebService::KVV::Live::Stop; | ||||||||||||||
| 27 | use utf8; | ||||||||||||||
| 28 | use open qw( :encoding(UTF-8) :std ); | ||||||||||||||
| 29 | |||||||||||||||
| 30 | my $stop = WebService::KVV::Live::Stop->new("Siemensallee"); | ||||||||||||||
| 31 | print "Arrival time: $_->{time} $_->{route} $_->{destination}\n" for $stop->departures; | ||||||||||||||
| 32 | |||||||||||||||
| 33 | Print departure table: | ||||||||||||||
| 34 | |||||||||||||||
| 35 | use WebService::KVV::Live::Stop; | ||||||||||||||
| 36 | use utf8; | ||||||||||||||
| 37 | use open qw( :encoding(UTF-8) :std ); | ||||||||||||||
| 38 | |||||||||||||||
| 39 | use Data::Dumper::Table; | ||||||||||||||
| 40 | use Data::Visitor::Callback; | ||||||||||||||
| 41 | |||||||||||||||
| 42 | $stop = WebService::KVV::Live::Stop->new($ARGV[0] // 'Kronenplatz'); | ||||||||||||||
| 43 | my @entries = $stop->departures; | ||||||||||||||
| 44 | Data::Visitor::Callback->new('JSON::PP::Boolean' => sub { $_ = $_ ? 'true' : 0 })->visit(@entries); | ||||||||||||||
| 45 | my $departure_table = Tabulate \@entries; | ||||||||||||||
| 46 | $departure_table =~ s/^.*\n//; # remove object type | ||||||||||||||
| 47 | |||||||||||||||
| 48 | print $departure_tabletable; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | |||||||||||||||
| 51 | |||||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||||
| 53 | |||||||||||||||
| 54 | API for searching for bus/tram stops in the Karlsruhe Metropolitan Area (Karlsruhe Verkehrsvertriebe network to be exact) and for listing departure times at said stops. | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =cut | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 4 | my $client = Net::HTTP::Spore->new_from_spec(dist_file 'WebService-KVV-Live-Stop', 'kvvlive.json'); | |||||||||||||
| 59 | 4 | $client->enable('Format::JSON'); | |||||||||||||
| 60 | 4 | $client->enable('DefaultParams', default_params => { key => '377d840e54b59adbe53608ba1aad70e8' }); | |||||||||||||
| 61 | 4 | $client->enable('UserAgent', useragent => __PACKAGE__ ." $VERSION"); | |||||||||||||
| 62 | |||||||||||||||
| 63 | =head1 IMPLEMENTATION | ||||||||||||||
| 64 | |||||||||||||||
| 65 | Not really an API, just a client for L<http://live.kvv.de>. See L<kvvlive.json|https://github.com/athreef/WebService-KVV-Live-Stop/blob/master/share/kvvlive.json> for details. | ||||||||||||||
| 66 | |||||||||||||||
| 67 | The client is based on L<Net::HTTP::Spore> and has some workarounds: It overrides a method from C<Net::HTTP::Spore > that doesn't handle colons properly and throws a generic message on errors instead of the more specific HTTP error messages. | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =head1 METHODS AND ARGUMENTS | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =over 4 | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =item new($latitude, $langitude), new($name), new($id) | ||||||||||||||
| 74 | |||||||||||||||
| 75 | Search for matching local transport stops. C<$id> are identifiers starting with C<"de:">. C<$name> need not be an exact match. | ||||||||||||||
| 76 | |||||||||||||||
| 77 | Returns a list of C<WebService::KVV::Live::Stop>s in list context. In scalar context returns the best match. | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =cut | ||||||||||||||
| 80 | |||||||||||||||
| 81 | #FIXME: timeout | ||||||||||||||
| 82 | 3 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=8 SCAR=34.6
Pod
documented
| |||||||||||||||
| 83 | 3 | my $class = shift; | |||||||||||||
| 84 | |||||||||||||||
| 85 | 3 | my @self; | |||||||||||||
| 86 | 3 | ▶ | @_ or croak "No stop specified"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 3 | ▶ | my $response = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 88 | @_ == 2 ? $client->stop_by_latlon(LAT => shift, LON => shift) | ||||||||||||||
| 89 | : $_[0] =~ /^de:$/ ? $client->stop_by_id(ID => shift) | ||||||||||||||
| 90 | : $client->stop_by_name(NAME => shift) | ||||||||||||||
| 91 | ; | ||||||||||||||
| 92 | 0 | ▶ | @{$response->{body}{stops}} or croak "No stops match arguments"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | ▶ | $response->{body}{stops} = [$response->{body}{stops}[0]] unless wantarray; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 0 | for my $stop (@{$response->{body}{stops}}) { | |||||||||||||
| 95 | 0 | my $obj = $stop; | |||||||||||||
| 96 | 0 | bless $obj, $class; | |||||||||||||
| 97 | 0 | push @self, $obj; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | ▶ | return wantarray ? @self : $self[0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | |||||||||||||||
| 104 | =item departures([$route]) | ||||||||||||||
| 105 | |||||||||||||||
| 106 | Returns a list of departures for a WebService::KVV::Live::Stop. Results can be restricted to a particular route (Linie) by the optional argument. | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =cut | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 0 | ▶ | sub _departures { | ||||||||||||
|
Subroutine
_departures: not called CC=2 SCAR=17.9
| |||||||||||||||
| 111 | 0 | my $id = shift; | |||||||||||||
| 112 | 0 | my $route = shift; | |||||||||||||
| 113 | |||||||||||||||
| 114 | # ?maxInfos=:maxInfos | ||||||||||||||
| 115 | 0 | ▶ | return defined $route ? $client->departures_by_route(ID => $id, ROUTE => $route) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | : $client->departures_by_stop(ID => $id); | ||||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 0 | ▶ | sub departures { | ||||||||||||
|
Subroutine
departures: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 120 | 0 | my $self = shift; | |||||||||||||
| 121 | 0 | my $route = shift; | |||||||||||||
| 122 | |||||||||||||||
| 123 | 0 | my $id = $self->{id}; | |||||||||||||
| 124 | 0 | my $response; | |||||||||||||
| 125 | 0 | eval { | |||||||||||||
| 126 | 0 | $response = _departures $id, $route; | |||||||||||||
| 127 | }; | ||||||||||||||
| 128 | 0 | ▶ | defined $response or croak "Error during REST request (Ye, I know the error message sucks but it's acutally Net::HTTP::Spore throwing an exception without context)"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 0 | return @{$response->{body}->{departures}} | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | |||||||||||||||
| 133 | 4 | 1; | |||||||||||||
| 134 | __END__ | ||||||||||||||