summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/Bus/Response.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/WebService/TFL/Bus/Response.pm')
-rw-r--r--lib/WebService/TFL/Bus/Response.pm72
1 files changed, 30 insertions, 42 deletions
diff --git a/lib/WebService/TFL/Bus/Response.pm b/lib/WebService/TFL/Bus/Response.pm
index e05d49d..59582be 100644
--- a/lib/WebService/TFL/Bus/Response.pm
+++ b/lib/WebService/TFL/Bus/Response.pm
@@ -1,55 +1,43 @@
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 _sort {
+ return sort {
+ $a->destinationName cmp $b->destinationName
+ ||
+ $a->expectedArrival <=> $b->expectedArrival
+ } @_;
}
-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);
+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->@*
+ ],
+ });
+}
- substr($json,0,$consumed)='';
- }
+sub new_merged {
+ my ($class, @responses) = @_;
- return $class->new(\%args);
+ return $class->new({
+ predictions => [
+ _sort map { $_->predictions->@* } @responses
+ ],
+ });
}
1;