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

Expose operator names (if available), fall back to admin IDs otherwise

See #10
parent 5f42920c
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -4,3 +4,5 @@
/cover_db
/MANIFEST*
/MYMETA.*

/lib/Travel/Status/DE/DBRIS/Operators.pm
+2 −2
Original line number Diff line number Diff line
@@ -394,8 +394,8 @@ elsif ( $opt{journey} and not( $raw_json_output or $json_output ) ) {
		$trip->train_no // $trip->number,
		$trip->day->strftime('%d.%m.%Y') );

	if ( $trip->admin_ids ) {
		printf( "Betrieb: %s\n", join( q{, }, $trip->admin_ids ) );
	if ( $trip->operators ) {
		printf( "Betrieb: %s\n", join( q{, }, $trip->operators ) );
	}
	say q{};

+33 −1
Original line number Diff line number Diff line
@@ -7,13 +7,15 @@ use 5.020;
use parent 'Class::Accessor';

use Travel::Status::DE::DBRIS::Location;
use Travel::Status::DE::DBRIS::Operators;

our $VERSION = '0.16';

# ->number is deprecated
# TODO: Rename ->train, ->train_no to ->trip, ->trip_no
Travel::Status::DE::DBRIS::Journey->mk_ro_accessors(
	qw(admin_id day id train train_no line_no type number is_cancelled));
	qw(admin_id day id train train_no line_no type number operator is_cancelled)
);

sub new {
	my ( $obj, %opt ) = @_;
@@ -52,10 +54,24 @@ sub new {
			if ( defined( my $admin_id = $admin_id_ml{ $admin_id_argmax[0] } ) )
			{
				$ref->{admin_id} = $admin_id;
				if (
					my $op
					= Travel::Status::DE::DBRIS::Operators::get_operator_name(
						$admin_id)
				  )
				{
					$ref->{operator} = $admin_id;
				}
			}

			# return most frequent admin ID first
			$ref->{admin_ids} = \@admin_id_argmax;
			$ref->{operators} = [
				map {
					Travel::Status::DE::DBRIS::Operators::get_operator_name($_)
					  // $_
				} @admin_id_argmax
			];
		}

		if (%trip_no_ml) {
@@ -202,6 +218,12 @@ sub admin_ids {
	return @{ $self->{admin_ids} // [] };
}

sub operators {
	my ($self) = @_;

	return @{ $self->{operators} // [] };
}

sub TO_JSON {
	my ($self) = @_;

@@ -274,6 +296,16 @@ majority of stops.
List of strings indirectly identifying the operators of the journey, in
descending order of the number of stops they are responsible for.

=item $journey->operator

String naming the operator of the journey.  In case there are mulitple
operators, returns the one responsible for the majority of stops.

=item $journey->operators

List of strings naming the operators of the journey, in descending order of the
number of stops they are responsible for.

=item $journey->train

Textual description of the departure, typically consisting of type identifier
+66 −0
Original line number Diff line number Diff line
#!/usr/bin/env perl

use strict;
use warnings;
use 5.020;
use utf8;
use File::Slurp qw(read_file write_file);
use JSON;

my $json_str  = read_file('share/admin-id-to-operator.json');
my $adm_to_op = JSON->new->utf8->decode($json_str);

my $buf = <<'EOF';
package Travel::Status::DE::DBRIS::Operators;

# vim:readonly
# This module has been automatically generated from share/admin-id-to-operator.json
# by lib/Travel/Status/DE/DBRIS/Operators.pm.PL.
# Do not edit, changes will be lost.

use strict;
use warnings;
use 5.020;
use utf8;

our $VERSION = '0.16';

# Automatically generated, see share/stations.json
my %admin_id_to_operator = (
EOF

while ( my ( $k, $v ) = each %{$adm_to_op} ) {
	if ( $k =~ m{'} or $v->[0] =~ m{'} or $v->[1] =~ m{'} ) {
		die("Unsupported entry: $k");
	}
	$buf .= sprintf( "'%s' => ['%s', '%s'],\n", $k, @{$v} );
}

$buf .= <<'EOF';
);

sub get_operator {
	my ($id) = @_;
	return $admin_id_to_operator{$id};
}

sub get_operator_abbr {
	my ($id) = @_;
	if (my $op = $admin_id_to_operator{$id}) {
		return $op->[0];
	}
	return;
}

sub get_operator_name {
	my ($id) = @_;
	if (my $op = $admin_id_to_operator{$id}) {
		return $op->[1];
	}
	return;
}

1;
EOF

write_file( $ARGV[0], { binmode => ':utf8' }, $buf );
+0 −0

File moved.