summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/Bus.pm
blob: 8f43c0f4f5b73d5b697b2cbd79a76bc253873923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package WebService::TFL::Bus; 
use Moo;
use Types::URI 'Uri';
use Types::Standard -types;
use Future::AsyncAwait;
use WebService::TFL::Bus::Response;
use namespace::clean;
 
has user_agent => (
    isa => HasMethods['do_request'],
    is => 'ro',
    required => 1,
);
 
has uri => (
    isa => Uri,
    is => 'ro',
    coerce => Uri->coercion,
    default => 'https://api.tfl.gov.uk/StopPoint/__/Arrivals',
);
 
has parser => (
    is => 'lazy',
    builder => sub { JSON->new->utf8 },
);
 
async sub request {
    my ($self,$stop_id) = @_;
 
    my $uri = $self->uri->clone;
    $uri->path_segments(
        map { $_ eq '__' ? $stop_id : $_ } $uri->path_segments
    );
 
    my $http_response = await $self->user_agent->do_request(uri => $uri);
 
    if ($http_response->is_success) {
        my $json = $http_response->content;
 
        return WebService::TFL::Bus::Response->new_from_response(
            $self->parser->decode($json)
        );
    }
    else {
        die $http_response->status_line;
    }
}
 
1;