From 5c48b7da4780bcdac4757ad5c23e74e3f6744c42 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 9 Dec 2023 13:19:26 +0000 Subject: new tube api --- lib/WebService/TFL/TubeStatus.pm | 25 +++++++++++++++------- lib/WebService/TFL/TubeStatus/Response.pm | 25 +++++++++------------- lib/WebService/TFL/TubeStatus/Response/Line.pm | 23 ++++++++++---------- .../TFL/TubeStatus/Response/LineStatus.pm | 22 ++++++------------- lib/WebService/TFL/TubeStatus/Types.pm | 11 ---------- 5 files changed, 44 insertions(+), 62 deletions(-) delete mode 100644 lib/WebService/TFL/TubeStatus/Types.pm (limited to 'lib') diff --git a/lib/WebService/TFL/TubeStatus.pm b/lib/WebService/TFL/TubeStatus.pm index b0ba260..4166a79 100644 --- a/lib/WebService/TFL/TubeStatus.pm +++ b/lib/WebService/TFL/TubeStatus.pm @@ -2,8 +2,6 @@ package WebService::TFL::TubeStatus; use Moo; use Type::Utils 'duck_type'; use Types::URI 'Uri'; -use XML::LibXML; -use XML::LibXML::XPathContext; use WebService::TFL::TubeStatus::Response; use namespace::autoclean; @@ -22,18 +20,29 @@ has uri => ( isa => Uri, is => 'ro', coerce => Uri->coercion, - default => 'http://cloud.tfl.gov.uk/TrackerNet/LineStatus', + default => 'https://api.tfl.gov.uk/Line/Mode/tube/Status', +); + +has parser => ( + is => 'lazy', + builder => sub { JSON->new->utf8 }, ); sub request { my ($self) = @_; - my $doc = XML::LibXML->load_xml(location => $self->uri) - or die "Couldn't fetch tube status"; - my $xpath=XML::LibXML::XPathContext->new($doc); - $xpath->registerNs('ws','http://webservices.lul.co.uk/'); - return WebService::TFL::TubeStatus::Response->new_from_xml($doc,$xpath); + my $http_response = $self->user_agent->get($self->uri); + if ($http_response->is_success) { + my $json = $http_response->content; + + return WebService::TFL::TubeStatus::Response->new_from_response( + $self->parser->decode($json) + ); + } + else { + die $http_response->status_line; + } } 1; diff --git a/lib/WebService/TFL/TubeStatus/Response.pm b/lib/WebService/TFL/TubeStatus/Response.pm index 191abe7..4fe6524 100644 --- a/lib/WebService/TFL/TubeStatus/Response.pm +++ b/lib/WebService/TFL/TubeStatus/Response.pm @@ -1,30 +1,25 @@ package WebService::TFL::TubeStatus::Response; use Moo; -use WebService::TFL::TubeStatus::Types -all; use WebService::TFL::TubeStatus::Response::Line; use Types::Standard -all; use namespace::autoclean; has lines => ( is => 'ro', - isa => ArrayRef[LineT], + isa => ArrayRef[InstanceOf['WebService::TFL::TubeStatus::Response::Line']], required => 1, ); -sub new_from_xml { - my ($class,$doc,$xpath) = @_; +sub new_from_response { + my ($class,$response_data) = @_; - my @lines; - - for my $ls ($xpath->findnodes(q{/ws:ArrayOfLineStatus/ws:LineStatus},$doc)) { - my ($line)=$xpath->findnodes(q{ws:Line},$ls); - - my $line_object = WebService::TFL::TubeStatus::Response::Line->new_from_xml($line,$ls,$xpath); - - push @lines,$line_object; - } - - return $class->new({lines=>\@lines}); + return $class->new({ + lines=> [ + map { WebService::TFL::TubeStatus::Response::Line->new_from_response($_) } + grep { $_->{'$type'} =~ /\bLine\b/ } + $response_data->@* + ], + }); } 1; diff --git a/lib/WebService/TFL/TubeStatus/Response/Line.pm b/lib/WebService/TFL/TubeStatus/Response/Line.pm index 9779443..e8163a9 100644 --- a/lib/WebService/TFL/TubeStatus/Response/Line.pm +++ b/lib/WebService/TFL/TubeStatus/Response/Line.pm @@ -1,13 +1,12 @@ package WebService::TFL::TubeStatus::Response::Line; use Moo; use Types::Standard -all; -use WebService::TFL::TubeStatus::Types -all; use WebService::TFL::TubeStatus::Response::LineStatus; use namespace::autoclean; has id => ( is => 'ro', - isa => Num, + isa => Str, required => 1, ); @@ -19,20 +18,20 @@ has name => ( has status => ( is => 'ro', - isa => LineStatusT, + isa => InstanceOf['WebService::TFL::TubeStatus::Response::LineStatus'], required => 1, ); -sub new_from_xml { - my ($class,$line,$status,$xpath) = @_; - - my %init_arg; - - $init_arg{id} = $line->findvalue(q{@ID}); - $init_arg{name} = $line->findvalue(q{@Name}); - $init_arg{status} = WebService::TFL::TubeStatus::Response::LineStatus->new_from_xml($status,$xpath); +sub new_from_response { + my ($class,$response_data) = @_; - return $class->new(\%init_arg); + return $class->new({ + id => $response_data->{id}, + name => $response_data->{name}, + status => WebService::TFL::TubeStatus::Response::LineStatus->new_from_response( + $response_data->{lineStatuses}[0], + ), + }); } 1; diff --git a/lib/WebService/TFL/TubeStatus/Response/LineStatus.pm b/lib/WebService/TFL/TubeStatus/Response/LineStatus.pm index 6caa7d6..87eb52f 100644 --- a/lib/WebService/TFL/TubeStatus/Response/LineStatus.pm +++ b/lib/WebService/TFL/TubeStatus/Response/LineStatus.pm @@ -3,31 +3,21 @@ use Moo; use Types::Standard -all; use namespace::autoclean; -has is_active => ( +has [qw(statusSeverity statusSeverityDescription)] => ( is => 'ro', - isa => Bool, + isa => Str, required => 1, ); -has [qw(code class description details)] => ( +has reason => ( is => 'ro', isa => Str, - required => 1, ); -sub new_from_xml { - my ($class,$ls,$xpath) = @_; - - my %init_arg; - - my ($status) = $xpath->findnodes(q{ws:Status},$ls); - $init_arg{code} = $status->findvalue(q{@ID}); - $init_arg{is_active} = $status->findvalue(q{@IsActive}) eq 'true'; - $init_arg{class} = $status->findvalue(q{@CssClass}); - $init_arg{description} = $status->findvalue(q{@Description}); - $init_arg{details} = $ls->findvalue(q{@StatusDetails}); +sub new_from_response { + my ($class,$response_data) = @_; - return $class->new(\%init_arg); + return $class->new($response_data); } 1; diff --git a/lib/WebService/TFL/TubeStatus/Types.pm b/lib/WebService/TFL/TubeStatus/Types.pm deleted file mode 100644 index d1cb141..0000000 --- a/lib/WebService/TFL/TubeStatus/Types.pm +++ /dev/null @@ -1,11 +0,0 @@ -package WebService::TFL::TubeStatus::Types; -use strict; -use warnings; -use Type::Library -base, -declare => qw(LineT LineStatusT); -use Type::Utils -all; -use namespace::autoclean; - -class_type LineT, { class => 'WebService::TFL::TubeStatus::Response::Line' }; -class_type LineStatusT, { class => 'WebService::TFL::TubeStatus::Response::LineStatus' }; - -1; -- cgit v1.2.3