summaryrefslogtreecommitdiff
path: root/lib/Travel/Status/DE/DBRIS/Operators.pm.PL
blob: d345eadba1c7824ef24ee8cfd0dbce00195d3df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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.19';

# 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) = @_;
	if (not defined $id) {
		return;
	}
	return $admin_id_to_operator{$id};
}

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

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

1;
EOF

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