diff options
author | dakkar <dakkar@thenautilus.net> | 2023-12-09 14:07:20 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2023-12-09 14:07:20 +0000 |
commit | 1a37c7e52d5433e8182d444d12a55fdd9b8a2770 (patch) | |
tree | 2f0ed93dd07e6c078a5fb03cb9539ec93772fe23 /lib/WebService/TFL/Bus/Response.pm | |
parent | bump perl (diff) | |
parent | sort buses (diff) | |
download | HomePanel-1a37c7e52d5433e8182d444d12a55fdd9b8a2770.tar.gz HomePanel-1a37c7e52d5433e8182d444d12a55fdd9b8a2770.tar.bz2 HomePanel-1a37c7e52d5433e8182d444d12a55fdd9b8a2770.zip |
Merge branch 'new-api'
Diffstat (limited to 'lib/WebService/TFL/Bus/Response.pm')
-rw-r--r-- | lib/WebService/TFL/Bus/Response.pm | 62 |
1 files changed, 16 insertions, 46 deletions
diff --git a/lib/WebService/TFL/Bus/Response.pm b/lib/WebService/TFL/Bus/Response.pm index e05d49d..0f811fc 100644 --- a/lib/WebService/TFL/Bus/Response.pm +++ b/lib/WebService/TFL/Bus/Response.pm @@ -1,55 +1,25 @@ package WebService::TFL::Bus::Response; use Moo; -use Class::Load 'load_class'; -use Type::Utils 'class_type'; use Types::Standard -all; -use JSON; -use namespace::autoclean; +use WebService::TFL::Bus::Prediction; +use namespace::clean; -sub line_class { - "WebService::TFL::Bus::Response::$_[0]"; -} - -my %line_map = ( - 0 => 'Stop', - 1 => 'Prediction', - 2 => 'FlexibleMessage', - 3 => 'BaseVersion', - 4 => 'URAVersion', +has predictions => ( + is => 'ro', + isa => ArrayRef[InstanceOf['WebService::TFL::Bus::Prediction']], ); -for my $field (values %line_map) { - my $class = line_class($field); - load_class($class); - has $field => ( - is => 'ro', - isa => ArrayRef[class_type { class => $class }], - ); -} - -sub new_from_json { - my ($class,$return_list,$json) = @_; - - my $parser = JSON->new->utf8; - - my %return_set;@return_set{@$return_list}=(); - unless (%return_set) { - @return_set{qw(StopPointName LineName EstimatedTime)}=(); - } - my %args; - - while ($json) { - my ($array,$consumed) = $parser->decode_prefix($json); - - my $array_type = $line_map{$array->[0]}; - my $line_class = line_class($array_type); - push @{$args{$array_type}}, - $line_class->new_from_array(\%return_set,$array); - - substr($json,0,$consumed)=''; - } - - return $class->new(\%args); +sub new_from_response { + my ($class,$response_data) = @_; + + return $class->new({ + predictions => [ + sort { $a->expectedArrival <=> $b->expectedArrival } + map { WebService::TFL::Bus::Prediction->new_from_response($_) } + grep { $_->{'$type'} =~ /\bPrediction\b/ } + $response_data->@* + ], + }); } 1; |