-
Birte Kristina Friesel authoredBirte Kristina Friesel authored
StopFinder.pm 4.35 KiB
package Travel::Status::DE::HAFAS::StopFinder;
use strict;
use warnings;
use 5.014;
use utf8;
no if $] >= 5.018, warnings => 'experimental::smartmatch';
use Carp qw(confess);
use Encode qw(decode);
use JSON;
use LWP::UserAgent;
our $VERSION = '3.00';
sub new {
my ( $obj, %conf ) = @_;
my $lang = $conf{language} // 'd';
my $ua = $conf{ua};
if ( not $ua ) {
my %lwp_options = %{ $conf{lwp_options} // { timeout => 10 } };
$ua = LWP::UserAgent->new(%lwp_options);
$ua->env_proxy;
}
my $reply;
if ( not $conf{input} ) {
confess('You need to specify an input value');
}
if ( not $conf{url} ) {
confess('You need to specify a URL');
}
my $ref = {
developer_mode => $conf{developer_mode},
post => {
getstop => 1,
REQ0JourneyStopsS0A => 255,
REQ0JourneyStopsS0G => $conf{input},
},
};
bless( $ref, $obj );
my $url = $conf{url} . "/${lang}n";
$reply = $ua->post( $url, $ref->{post} );
if ( $reply->is_error ) {
$ref->{errstr} = $reply->status_line;
return $ref;
}
$ref->{raw_reply} = $reply->decoded_content;
$ref->{raw_reply} =~ s{ ^ SLs [.] sls = }{}x;
$ref->{raw_reply} =~ s{ ; SLs [.] showSuggestion [(] [)] ; $ }{}x;
if ( $ref->{developer_mode} ) {
say $ref->{raw_reply};
}
$ref->{json} = from_json( $ref->{raw_reply} );
return $ref;
}