summaryrefslogtreecommitdiff
path: root/lib/Travel/Status/MOTIS/TripAtStopover.pm
blob: 6d803817bc27e97e02b29d79c460a22ff0a37308 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
package Travel::Status::MOTIS::TripAtStopover;

use strict;
use warnings;
use 5.020;

use DateTime::Format::ISO8601;

use parent 'Class::Accessor';

our $VERSION = '0.03';

Travel::Status::MOTIS::TripAtStopover->mk_ro_accessors(
	qw(
	  id
	  mode
	  agency
	  route_name
	  route_color
	  route_text_color
	  headsign

	  is_cancelled
	  is_realtime

	  stopover
	)
);

sub new {
	my ( $obj, %opt ) = @_;

	my $json      = $opt{json};
	my $time_zone = $opt{time_zone};

	my $ref = {
		id               => $json->{tripId},
		mode             => $json->{mode},
		agency           => $json->{agencyName},
		route_name       => $json->{routeShortName},
		route_color      => $json->{routeColor},
		route_text_color => $json->{routeTextColor},
		headsign         => $json->{headsign},

		is_cancelled => $json->{cancelled},
		is_realtime  => $json->{realTime},

		stopover => Travel::Status::MOTIS::Stopover->new(
			json => $json->{place},

			# NOTE: $json->{place}->{cancelled} isn't set, we just override this here.
			cancelled => $json->{cancelled},
			realtime  => $json->{realTime},

			time_zone => $time_zone,
		),
	};

	bless( $ref, $obj );

	return $ref;
}

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

	my $ret = { %{$self} };

	for my $timestamp_key (
		qw(
		scheduled_departure
		realtime_departure
		departure
		scheduled_arrival
		realtime_arrival
		arrival
		)
	  )
	{
		if ( $ret->{$timestamp_key} ) {
			$ret->{$timestamp_key} = $ret->{$timestamp_key}->epoch;
		}
	}

	return $ret;
}

1;