summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/TubeStatus.pm
blob: e4ec7b51e0a280acc32e6a1d892dd8ae65333bfb (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
package WebService::TFL::TubeStatus; 
use Moo;
use Types::URI 'Uri';
use Types::Standard -types;
use Future::AsyncAwait;
use WebService::TFL::TubeStatus::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/Line/Mode/tube,dlr,elizabeth-line,overground/Status',
);
 
has parser => (
    is => 'lazy',
    builder => sub { JSON->new->utf8 },
);
 
async sub request {
    my ($self) = @_;
 
    my $http_response = await $self->user_agent->do_request(uri => $self->uri);
 
    if ($http_response->is_success) {
        my $json = $http_response->content;
 
        return WebService::TFL::TubeStatus::Response->new_from_response(
            $self->parser->decode($json)
        );
    }
    else {
        die $http_response->status_line;
    }
}
 
1;