diff options
Diffstat (limited to 'lib/WebService/TFL/TubeStatus.pm')
-rw-r--r-- | lib/WebService/TFL/TubeStatus.pm | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/lib/WebService/TFL/TubeStatus.pm b/lib/WebService/TFL/TubeStatus.pm index b0ba260..e4ec7b5 100644 --- a/lib/WebService/TFL/TubeStatus.pm +++ b/lib/WebService/TFL/TubeStatus.pm @@ -1,39 +1,44 @@ package WebService::TFL::TubeStatus; use Moo; -use Type::Utils 'duck_type'; use Types::URI 'Uri'; -use XML::LibXML; -use XML::LibXML::XPathContext; +use Types::Standard -types; +use Future::AsyncAwait; use WebService::TFL::TubeStatus::Response; -use namespace::autoclean; +use namespace::clean; has user_agent => ( - isa => duck_type(['get']), - is => 'lazy', + isa => HasMethods['do_request'], + is => 'ro', + required => 1, ); -sub _build_user_agent { - require LWP::UserAgent; - my $ua = LWP::UserAgent->new(); - $ua->env_proxy; - return $ua; -} 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,dlr,elizabeth-line,overground/Status', ); -sub request { +has parser => ( + is => 'lazy', + builder => sub { JSON->new->utf8 }, +); + +async 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/'); + my $http_response = await $self->user_agent->do_request(uri => $self->uri); + + if ($http_response->is_success) { + my $json = $http_response->content; - return WebService::TFL::TubeStatus::Response->new_from_xml($doc,$xpath); + return WebService::TFL::TubeStatus::Response->new_from_response( + $self->parser->decode($json) + ); + } + else { + die $http_response->status_line; + } } 1; |