From 47d24b4cbea6d8364d0718e31cfd2fb92187b7ea Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 27 May 2013 17:01:20 +0100 Subject: factored TFL client --- lib/WebService/TFL/Bus/Response.pm | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/WebService/TFL/Bus/Response.pm (limited to 'lib/WebService/TFL/Bus/Response.pm') diff --git a/lib/WebService/TFL/Bus/Response.pm b/lib/WebService/TFL/Bus/Response.pm new file mode 100644 index 0000000..a4aa056 --- /dev/null +++ b/lib/WebService/TFL/Bus/Response.pm @@ -0,0 +1,54 @@ +package WebService::TFL::Bus::Response; +use Moose; +use Class::Load 'load_class'; +use JSON; +use namespace::autoclean; + +sub line_class { + "WebService::TFL::Bus::Response::$_[0]"; +} + +my %line_map = ( + 0 => 'Stop', + 1 => 'Prediction', + 2 => 'FlexibleMessage', + 3 => 'BaseVersion', + 4 => 'URAVersion', +); + +for my $field (values %line_map) { + my $class = line_class($field); + load_class($class); + has $field => ( + is => 'ro', + isa => "ArrayRef[$class]", + traits => [ 'Array' ], + ); +} + +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); +} + +1; -- cgit v1.2.3