From 346aa5f857d852591d5e2ef5c0ad4dba49fbae0f Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 2 Feb 2023 20:28:40 +0100 Subject: StopFinder: add new_p constructor --- lib/Travel/Status/DE/HAFAS/StopFinder.pm | 91 ++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/Travel/Status/DE/HAFAS/StopFinder.pm b/lib/Travel/Status/DE/HAFAS/StopFinder.pm index 0ae3800..9b3d232 100644 --- a/lib/Travel/Status/DE/HAFAS/StopFinder.pm +++ b/lib/Travel/Status/DE/HAFAS/StopFinder.pm @@ -14,13 +14,15 @@ use LWP::UserAgent; our $VERSION = '4.04'; +# {{{ Constructors + sub new { my ( $obj, %conf ) = @_; my $lang = $conf{language} // 'd'; 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 +48,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 +75,62 @@ 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 { + say "then"; + my ($tx) = @_; + if ( my $err = $tx->error ) { + $promise->reject( + "POST $url returned HTTP $err->{code} $err->{message}"); + return; + } + my $content = $tx->res->body; + + say $content; + $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); + return; + } + )->catch( + sub { + say "catch"; + my ($err) = @_; + $promise->reject($err); + return; + } + )->wait; + + return $promise; +} + +# }}} + sub errstr { my ($self) = @_; @@ -172,18 +234,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 Travel::Status::DE::HAFAS::StopFinder instance +($stopfinder) on success and rejects with an error message ($stopfinder->errstr) on +failure. In addition to the arguments of B, the following mandatory +arguments must be set. + +=over + +=item B => I + +Promises implementation to use for internal promises as well as B return +value. Recommended: Mojo::Promise(3pm). + +=item B => I + +User agent instance to use for asynchronous requests. The object must implement +a B 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 (IBNR / UIC station code) and B (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 -- cgit v1.2.3