| 1 | package Travel::Status::DE::HAFAS::StopFinder; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | our $VERSION = '6.25'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # {{{ Constructors | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 0 | my ( $obj, %conf ) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $lang = $conf{language} // 'd'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$conf{"language"} // "d"
MC/DC0%0/1
$conf{"language"} // "d"
$conf{"language"}
Truth table0%0/2
A
A= $conf{"language"}
| |||||||||||||||||||||||||||||
| 21 | 0 | my $ua = $conf{ua}; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ( not $ua and not $conf{async} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ua or $conf{"async"}
MC/DC0%0/2
$ua or $conf{"async"}
$ua
$conf{"async"}
Truth table0%0/3
A or B
A= $uaB= $conf{"async"}
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | my %lwp_options = %{ $conf{lwp_options} // { timeout => 10 } }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$conf{"lwp_options"} // {"timeout", 10}
MC/DC0%0/1
$conf{"lwp_options"} // {"timeout", 10}
$conf{"lwp_options"}
Truth table0%0/2
A
A= $conf{"lwp_options"}
| |||||||||||||||||||||||||||||
| 25 | 0 | $ua = LWP::UserAgent->new(%lwp_options); | |||||||||||||||||||||||||||
| 26 | 0 | $ua->env_proxy; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | my $reply; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( not $conf{input} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | confess('You need to specify an input value'); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( not $conf{url} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | confess('You need to specify a URL'); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $ref = { | ||||||||||||||||||||||||||||
| 39 | developer_mode => $conf{developer_mode}, | ||||||||||||||||||||||||||||
| 40 | post => { | ||||||||||||||||||||||||||||
| 41 | getstop => 1, | ||||||||||||||||||||||||||||
| 42 | REQ0JourneyStopsS0A => 255, | ||||||||||||||||||||||||||||
| 43 | REQ0JourneyStopsS0G => $conf{input}, | ||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | 0 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | bless( $ref, $obj ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( $conf{async} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | return $ref; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $url = $conf{url} . "/${lang}n"; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | $reply = $ua->post( $url, $ref->{post} ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( $reply->is_error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $ref->{errstr} = $reply->status_line; | |||||||||||||||||||||||||||
| 59 | 0 | return $ref; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | $ref->{raw_reply} = $reply->decoded_content; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | $ref->{raw_reply} =~ s{ ^ SLs [.] sls = }{}x; | |||||||||||||||||||||||||||
| 65 | 0 | $ref->{raw_reply} =~ s{ ; SLs [.] showSuggestion [(] [)] ; $ }{}x; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $ref->{developer_mode} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | say $ref->{raw_reply}; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | $ref->{json} = from_json( $ref->{raw_reply} ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return $ref; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub new_p { | ||||||||||||||||||||||||||
|
Subroutine
new_p: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | my ( $obj, %conf ) = @_; | |||||||||||||||||||||||||||
| 78 | 0 | my $promise = $conf{promise}->new; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( not $conf{input} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | return $promise->reject('You need to specify an input value'); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( not $conf{url} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | return $promise->reject('You need to specify a URL'); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $self = $obj->new( %conf, async => 1 ); | |||||||||||||||||||||||||||
| 88 | 0 | $self->{promise} = $conf{promise}; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $lang = $conf{language} // 'd'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$conf{"language"} // "d"
MC/DC0%0/1
$conf{"language"} // "d"
$conf{"language"}
Truth table0%0/2
A
A= $conf{"language"}
| |||||||||||||||||||||||||||||
| 91 | 0 | my $url = $conf{url} . "/${lang}n"; | |||||||||||||||||||||||||||
| 92 | $conf{user_agent}->post_p( $url, form => $self->{post} )->then( | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 94 | 0 | my ($tx) = @_; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( my $err = $tx->error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $promise->reject( | |||||||||||||||||||||||||||
| 97 | "POST $url returned HTTP $err->{code} $err->{message}"); | ||||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | my $content = $tx->res->body; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $self->{raw_reply} = $content; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | $self->{raw_reply} =~ s{ ^ SLs [.] sls = }{}x; | |||||||||||||||||||||||||||
| 105 | 0 | $self->{raw_reply} =~ s{ ; SLs [.] showSuggestion [(] [)] ; $ }{}x; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( $self->{developer_mode} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | say $self->{raw_reply}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | $self->{json} = from_json( $self->{raw_reply} ); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | $promise->resolve( $self->results ); | |||||||||||||||||||||||||||
| 114 | 0 | return; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | )->catch( | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($err) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | $promise->reject($err); | |||||||||||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 0 | )->wait; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | return $promise; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # }}} | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub errstr { | ||||||||||||||||||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | return $self->{errstr}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub results { | ||||||||||||||||||||||||||
|
Subroutine
results: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | $self->{results} = []; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | for my $result ( @{ $self->{json}->{suggestions} } ) { | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( $result->{typeStr} eq '[Bhf/Hst]' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | push( | ||||||||||||||||||||||||||||
| 143 | 0 | @{ $self->{results} }, | |||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | name => decode( 'iso-8859-15', $result->{value} ), | ||||||||||||||||||||||||||||
| 146 | id => $result->{extId} | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | ); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | return @{ $self->{results} }; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 5 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||