From 8bd30555a18dcab02dc95ea8b3701cdc7adbabb6 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 9 Dec 2023 13:10:17 +0000 Subject: new bus api --- lib/WebService/TFL/Bus/Response.pm | 59 +++++++++----------------------------- 1 file changed, 14 insertions(+), 45 deletions(-) (limited to 'lib/WebService/TFL/Bus/Response.pm') diff --git a/lib/WebService/TFL/Bus/Response.pm b/lib/WebService/TFL/Bus/Response.pm index e05d49d..6cba172 100644 --- a/lib/WebService/TFL/Bus/Response.pm +++ b/lib/WebService/TFL/Bus/Response.pm @@ -1,55 +1,24 @@ 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)=''; - } +sub new_from_response { + my ($class,$response_data) = @_; - return $class->new(\%args); + return $class->new({ + predictions => [ + map { WebService::TFL::Bus::Prediction->new_from_response($_) } + grep { $_->{'$type'} =~ /\bPrediction\b/ } + $response_data->@* + ], + }); } 1; -- cgit v1.2.3