From fa21ea4bce78edbf3649d656b98d96f88b0b0aaf Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 11 Jan 2021 22:33:54 +0100 Subject: Wagonorder: refactor dbdb requests --- lib/DBInfoscreen/Helper/Wagonorder.pm | 52 ++++++++--------------------------- 1 file changed, 12 insertions(+), 40 deletions(-) (limited to 'lib/DBInfoscreen/Helper') diff --git a/lib/DBInfoscreen/Helper/Wagonorder.pm b/lib/DBInfoscreen/Helper/Wagonorder.pm index 5904d86..d303d18 100644 --- a/lib/DBInfoscreen/Helper/Wagonorder.pm +++ b/lib/DBInfoscreen/Helper/Wagonorder.pm @@ -57,12 +57,11 @@ sub is_available_p { return $promise; } -sub has_umlauf_p { - my ( $self, $train_no ) = @_; +sub head_dbdb_p { + my ( $self, $url ) = @_; my $promise = Mojo::Promise->new; - my $url = "https://lib.finalrewind.org/dbdb/db_umlauf/${train_no}.png"; my $cache = $self->{main_cache}; if ( my $content = $cache->get($url) ) { @@ -98,46 +97,19 @@ sub has_umlauf_p { return $promise; } -sub check_wagonorder_p { - my ( $self, $train_no, $wr_link ) = @_; - - my $promise = Mojo::Promise->new; +sub has_cycle_p { + my ( $self, $train_no ) = @_; - my $url - = "https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${wr_link}"; - my $cache = $self->{main_cache}; + return $self->head_dbdb_p( + "https://lib.finalrewind.org/dbdb/db_umlauf/${train_no}.png"); +} - if ( my $content = $cache->get($url) ) { - if ( $content eq 'y' ) { - return $promise->resolve; - } - else { - return $promise->reject; - } - } +sub check_wagonorder_p { + my ( $self, $train_no, $wr_link ) = @_; - $self->{user_agent}->request_timeout(5)->head_p( $url => $self->{header} ) - ->then( - sub { - my ($tx) = @_; - if ( $tx->result->is_success ) { - $cache->set( $url, 'y' ); - $promise->resolve; - } - else { - $cache->set( $url, 'n' ); - $promise->reject; - } - return; - } - )->catch( - sub { - $cache->set( $url, 'n' ); - $promise->reject; - return; - } - )->wait; - return $promise; + return $self->head_dbdb_p( + "https://lib.finalrewind.org/dbdb/has_wagonorder/${train_no}/${wr_link}" + ); } sub get_p { -- cgit v1.2.3