summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/Bus/Response.pm
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2023-12-09 13:10:17 +0000
committerdakkar <dakkar@thenautilus.net>2023-12-09 13:10:17 +0000
commit8bd30555a18dcab02dc95ea8b3701cdc7adbabb6 (patch)
tree4a2351d6111c8231fb04db72391ea2da5d60dbf3 /lib/WebService/TFL/Bus/Response.pm
parentwe can use the new weather api (diff)
downloadHomePanel-8bd30555a18dcab02dc95ea8b3701cdc7adbabb6.tar.gz
HomePanel-8bd30555a18dcab02dc95ea8b3701cdc7adbabb6.tar.bz2
HomePanel-8bd30555a18dcab02dc95ea8b3701cdc7adbabb6.zip
new bus api
Diffstat (limited to 'lib/WebService/TFL/Bus/Response.pm')
-rw-r--r--lib/WebService/TFL/Bus/Response.pm59
1 files changed, 14 insertions, 45 deletions
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;