summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2023-02-02 20:28:40 +0100
committerDaniel Friesel <derf@finalrewind.org>2023-02-02 20:28:40 +0100
commit346aa5f857d852591d5e2ef5c0ad4dba49fbae0f (patch)
treecd5d219849344ac9f44a9666bd9a2e89a1143a1b
parentf6ed08a133487645fda472a3e0ade57e536c9f3e (diff)
StopFinder: add new_p constructor
-rw-r--r--lib/Travel/Status/DE/HAFAS/StopFinder.pm91
1 files changed, 87 insertions, 4 deletions
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<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.
-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