summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/Bus/Response.pm
blob: 59582be0b4c6ce7ecaeb85d6a305171cdf9f1821 (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
package WebService::TFL::Bus::Response; 
use Moo;
use Types::Standard -all;
use WebService::TFL::Bus::Prediction;
use namespace::clean;
 
has predictions => (
    is => 'ro',
    isa => ArrayRef[InstanceOf['WebService::TFL::Bus::Prediction']],
);
 
sub _sort {
    return sort {
        $a->destinationName cmp $b->destinationName
            ||
            $a->expectedArrival <=> $b->expectedArrival
            @_;
}
 
sub new_from_response {
    my ($class,$response_data) = @_;
 
    return $class->new({
        predictions => [
            _sort
            map { WebService::TFL::Bus::Prediction->new_from_response($_) }
            grep { $_->{'$type'} =~ /\bPrediction\b/ }
            $response_data->@*
        ],
    });
}
 
sub new_merged {
    my ($class@responses) = @_;
 
    return $class->new({
        predictions => [
            _sort map { $_->predictions->@* } @responses
        ],
    });
}
 
1;