Unverified Commit 0c5496f0 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

fine-grained specification of passengers and discounts

parent f1e494c9
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ use Travel::Routing::DE::DBRIS;

my ( $date, $time, $from, $to, $language );
my $mots;
my ( $first_class, $discounts );
my ( $first_class, $passengers );
my $developer_mode;
my ( $json_output, $raw_json_output );
my $use_cache = 1;
@@ -36,12 +36,12 @@ my $output_reset = -t STDOUT ? "\033[0m" : q{};

GetOptions(
	'd|date=s'             => \$date,
	'D|discounts=s'        => \$discounts,
	'h|help'               => sub { show_help(0) },
	'f|full-route'         => \$show_full_route,
	'first-class!'         => \$first_class,
	'm|modes-of-transit=s' => \$mots,
	'l|language=s'         => \$language,
	'p|passengers=s'       => \$passengers,
	't|time=s'             => \$time,
	'V|version'            => \&show_version,
	'cache!'               => \$use_cache,
@@ -177,8 +177,18 @@ if ($mots) {
	$opt{modes_of_transit} = [ grep { $known_mot{$_} } @mots ];
}

if ($discounts) {
	$opt{discounts} = [ split( qr{, *}, $discounts ) ];
if ($passengers) {
	for my $passenger ( split( qr{; *}, $passengers ) ) {
		my ( $type, $discounts ) = split( qr{ *: *}, $passenger );
		$discounts = $discounts ? [ split( qr{, *}, $discounts ) ] : [];
		push(
			@{ $opt{passengers} },
			{
				type      => $type,
				discounts => $discounts,
			}
		);
	}
}

sub show_help {
@@ -293,7 +303,7 @@ for my $connection ( $ris->connections ) {
		$connection->duration->in_units( 'hours', 'minutes' ),
		$connection->arr ? $connection->arr->strftime('%H:%M') : q{??:??},
		format_occupancy($connection),
		defined $connection->price
		( defined $passengers and defined $connection->price )
		? sprintf( '  %.2f %s', $connection->price, $connection->price_unit )
		: q{},
		$header,
+51 −21
Original line number Diff line number Diff line
@@ -21,6 +21,12 @@ our $VERSION = '0.01';

Travel::Routing::DE::DBRIS->mk_ro_accessors(qw(earlier later));

my %passenger_type_map = (
	adult  => 'ERWACHSENER',
	junior => 'JUGENDLICHER',
	senior => 'SENIOR',
);

# {{{ Constructors

sub new {
@@ -94,10 +100,20 @@ sub new {
		push( @{ $req->{zwischenhalte} }, $via_stop );
	}

	if ( @{ $conf{discounts} // [] } ) {
		$req->{reisende}[0]{ermaessigungen} = [];
	if ( @{ $conf{passengers} // [] } ) {
		$req->{reisende} = [];
	}

	for my $passenger ( @{ $conf{passengers} // [] } ) {
		if ( not $passenger_type_map{ $passenger->{type} } ) {
			die("Unknown passenger type: '$passenger->{type}'");
		}
	for my $discount ( @{ $conf{discounts} // [] } ) {
		my $entry = {
			typ    => $passenger_type_map{ $passenger->{type} },
			alter  => [],
			anzahl => 1
		};
		for my $discount ( @{ $passenger->{discounts} // [] } ) {
			my ( $type, $class );
			for my $num (qw(25 50 100)) {
				if ( $discount eq "bc${num}" ) {
@@ -111,7 +127,7 @@ sub new {
			}
			if ($type) {
				push(
				@{ $req->{reisende}[0]{ermaessigungen} },
					@{ $entry->{ermaessigungen} },
					{
						art    => $type,
						klasse => $class,
@@ -119,6 +135,16 @@ sub new {
				);
			}
		}
		if ( not @{ $entry->{ermaessigungen} // [] } ) {
			$entry->{ermaessigungen} = [
				{
					art    => 'KEINE_ERMAESSIGUNG',
					klasse => 'KLASSENLOS'
				}
			];
		}
		push( @{ $req->{reisende} }, $entry );
	}

	$self->{strptime_obj} //= DateTime::Format::Strptime->new(
		pattern   => '%Y-%m-%dT%H:%M:%S',
@@ -381,11 +407,15 @@ Default: de.
Only request connections using the modes of transit specified in I<arrayref>.
Default: ICE, EC_IC, IR, REGIONAL, SBAHN, BUS, SCHIFF, UBAHN, TRAM, ANRUFPFLICHTIG.

=item B<discounts> => I<arrayref>
=item B<passengers> => I<arrayref>

Use passengers as defined by I<arrayref> when determining offer prices.  Each
entry describes a single person with a B<type> (string) and B<discounts>
(arrayref). B<type> must be B<adult> (27 to 64 years old), B<junior> (15 to 26
years old), or B<senior> (65 years or older). Supported discounts are: bc25,
bc25-first, bc50, bc50-first, bc100, bc100-first.

Consider discounts specified in I<arrayref> when determining offer prices.
Supported items: bc25, bc25-first, bc50, bc50-first, bc100, bc100-first.
Default: none.
Default: single adult, no discounts.

=item B<user_agent> => I<user agent>