diff options
Diffstat (limited to 'lib/Travel/Status/DE/HAFAS/StopFinder.pm')
-rw-r--r-- | lib/Travel/Status/DE/HAFAS/StopFinder.pm | 112 |
1 files changed, 97 insertions, 15 deletions
diff --git a/lib/Travel/Status/DE/HAFAS/StopFinder.pm b/lib/Travel/Status/DE/HAFAS/StopFinder.pm index ed575da..10f48da 100644 --- a/lib/Travel/Status/DE/HAFAS/StopFinder.pm +++ b/lib/Travel/Status/DE/HAFAS/StopFinder.pm @@ -5,22 +5,22 @@ use warnings; use 5.014; use utf8; -no if $] >= 5.018, warnings => 'experimental::smartmatch'; - -use Carp qw(confess); +use Carp qw(confess); use Encode qw(decode); use JSON; use LWP::UserAgent; -our $VERSION = '2.03'; +our $VERSION = '6.03'; + +# {{{ Constructors sub new { my ( $obj, %conf ) = @_; my $lang = $conf{language} // 'd'; - my $ua = $conf{ua}; + my $ua = $conf{ua}; - if ( not $ua ) { + if ( not $ua and not $conf{async} ) { my %lwp_options = %{ $conf{lwp_options} // { timeout => 10 } }; $ua = LWP::UserAgent->new(%lwp_options); $ua->env_proxy; @@ -46,6 +46,10 @@ sub new { bless( $ref, $obj ); + if ( $conf{async} ) { + return $ref; + } + my $url = $conf{url} . "/${lang}n"; $reply = $ua->post( $url, $ref->{post} ); @@ -69,6 +73,59 @@ sub new { return $ref; } +sub new_p { + my ( $obj, %conf ) = @_; + my $promise = $conf{promise}->new; + + if ( not $conf{input} ) { + return $promise->reject('You need to specify an input value'); + } + if ( not $conf{url} ) { + return $promise->reject('You need to specify a URL'); + } + + my $self = $obj->new( %conf, async => 1 ); + $self->{promise} = $conf{promise}; + + my $lang = $conf{language} // 'd'; + my $url = $conf{url} . "/${lang}n"; + $conf{user_agent}->post_p( $url, form => $self->{post} )->then( + sub { + my ($tx) = @_; + if ( my $err = $tx->error ) { + $promise->reject( + "POST $url returned HTTP $err->{code} $err->{message}"); + return; + } + my $content = $tx->res->body; + + $self->{raw_reply} = $content; + + $self->{raw_reply} =~ s{ ^ SLs [.] sls = }{}x; + $self->{raw_reply} =~ s{ ; SLs [.] showSuggestion [(] [)] ; $ }{}x; + + if ( $self->{developer_mode} ) { + say $self->{raw_reply}; + } + + $self->{json} = from_json( $self->{raw_reply} ); + + $promise->resolve( $self->results ); + return; + } + )->catch( + sub { + my ($err) = @_; + $promise->reject($err); + return; + } + )->wait; + + return $promise; +} + +# }}} + sub errstr { my ($self) = @_; @@ -109,7 +166,7 @@ finder services use Travel::Status::DE::HAFAS::StopFinder; my $sf = Travel::Status::DE::HAFAS::StopFinder->new( - url => 'http://reiseauskunft.bahn.de/bin/ajax-getstop.exe', + url => 'https://reiseauskunft.bahn.de/bin/ajax-getstop.exe', input => 'Borbeck', ); @@ -123,17 +180,21 @@ finder services =head1 VERSION -version 2.03 +version 6.03 =head1 DESCRIPTION Travel::Status::DE::HAFAS::StopFinder is an interface to the stop finder service of HAFAS based arrival/departure monitors, for instance the one -available at L<http://reiseauskunft.bahn.de/bin/ajax-getstop.exe/dn>. +available at L<https://reiseauskunft.bahn.de/bin/ajax-getstop.exe/dn>. It takes a string (usually a location or station name) and reports all stations and stops which are lexically similar to it. +StopFinder typically gives less coarse results than +Travel::Status::DE::HAFAS(3pm)'s locationSearch method. However, it is unclear +whether HAFAS instances will continue supporting it in the future. + =head1 METHODS =over @@ -172,18 +233,39 @@ you can use an empty hashref to override it. =back -=item $status->errstr +=item my $stopfinder_p = Travel::Status::DE::HAFAS::StopFinder->new_p(I<%opt>) + +Return a promise that resolves into a list of +Travel::Status::DE::HAFAS::StopFinder results ($stopfinder->results) on success +and rejects with an error message ($stopfinder->errstr) on failure. In addition +to the arguments of B<new>, the following mandatory arguments must be set. + +=over + +=item B<promise> => I<promises module> + +Promises implementation to use for internal promises as well as B<new_p> return +value. Recommended: Mojo::Promise(3pm). + +=item B<user_agent> => I<user agent> + +User agent instance to use for asynchronous requests. The object must implement +a B<post_p> function. Recommended: Mojo::UserAgent(3pm). + +=back + +=item $stopfinder->errstr In case of an error in the HTTP request, returns a string describing it. If no error occurred, returns undef. -=item $status->results +=item $stopfinder->results Returns a list of stop candidates. Each list element is a hash reference. The -hash keys are B<id> (IBNR / UIC station code) and B<name> (stop name). Both can -be used as input for the Travel::Status::DE::HAFAS(3pm) constructor. +hash keys are B<id> (IBNR / EVA / UIC station code) and B<name> (stop name). +Both can be used as input for the Travel::Status::DE::HAFAS(3pm) constructor. -If no matching results were found or the parser / http request failed, returns +If no matching results were found or the parser / HTTP request failed, returns the empty list. =back @@ -212,7 +294,7 @@ Travel::Status::DE::HAFAS(3pm). =head1 AUTHOR -Copyright (C) 2015-2017 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt> +Copyright (C) 2015-2023 by Birte Kristina Friesel E<lt>derf@finalrewind.orgE<gt> =head1 LICENSE |