summaryrefslogtreecommitdiff
path: root/lib/WebService/TFL/Bus/Response.pm
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2013-05-27 17:01:20 +0100
committerdakkar <dakkar@thenautilus.net>2013-05-27 17:01:20 +0100
commit47d24b4cbea6d8364d0718e31cfd2fb92187b7ea (patch)
treeaf6fe6c112f4dd3a87d1606db6d03b48eeeccccb /lib/WebService/TFL/Bus/Response.pm
parentinclude busses in html (diff)
downloadHomePanel-47d24b4cbea6d8364d0718e31cfd2fb92187b7ea.tar.gz
HomePanel-47d24b4cbea6d8364d0718e31cfd2fb92187b7ea.tar.bz2
HomePanel-47d24b4cbea6d8364d0718e31cfd2fb92187b7ea.zip
factored TFL client
Diffstat (limited to 'lib/WebService/TFL/Bus/Response.pm')
-rw-r--r--lib/WebService/TFL/Bus/Response.pm54
1 files changed, 54 insertions, 0 deletions
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;