| 1 | package Travel::Status::MOTIS::TripAtStopover; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.020; | ||
| 6 | |||
| 7 | use DateTime::Format::ISO8601; | ||
| 8 | |||
| 9 | use parent 'Class::Accessor'; | ||
| 10 | |||
| 11 | our $VERSION = '0.07'; | ||
| 12 | |||
| 13 | Travel::Status::MOTIS::TripAtStopover->mk_ro_accessors( | ||
| 14 | qw( | ||
| 15 | id | ||
| 16 | mode | ||
| 17 | agency | ||
| 18 | display_name | ||
| 19 | route_color | ||
| 20 | route_text_color | ||
| 21 | headsign | ||
| 22 | |||
| 23 | is_cancelled | ||
| 24 | is_realtime | ||
| 25 | |||
| 26 | stopover | ||
| 27 | ) | ||
| 28 | ); | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ( $obj, %opt ) = @_; | ||
| 32 | |||
| 33 | my $json = $opt{json}; | ||
| 34 | my $time_zone = $opt{time_zone}; | ||
| 35 | |||
| 36 | my $ref = { | ||
| 37 | id => $json->{tripId}, | ||
| 38 | mode => $json->{mode}, | ||
| 39 | agency => $json->{agencyName}, | ||
| 40 | display_name => $json->{displayName}, | ||
| 41 | route_color => $json->{routeColor}, | ||
| 42 | route_text_color => $json->{routeTextColor}, | ||
| 43 | headsign => $json->{headsign}, | ||
| 44 | |||
| 45 | is_cancelled => $json->{cancelled}, | ||
| 46 | is_realtime => $json->{realTime}, | ||
| 47 | |||
| 48 | stopover => Travel::Status::MOTIS::Stopover->new( | ||
| 49 | json => $json->{place}, | ||
| 50 | |||
| 51 | # NOTE: $json->{place}->{cancelled} isn't set, we just override this here. | ||
| 52 | cancelled => $json->{cancelled}, | ||
| 53 | realtime => $json->{realTime}, | ||
| 54 | |||
| 55 | time_zone => $time_zone, | ||
| 56 | ), | ||
| 57 | }; | ||
| 58 | |||
| 59 | bless( $ref, $obj ); | ||
| 60 | |||
| 61 | return $ref; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub TO_JSON { | ||
| 65 | my ($self) = @_; | ||
| 66 | |||
| 67 | my $ret = { %{$self} }; | ||
| 68 | |||
| 69 | for my $timestamp_key ( | ||
| 70 | qw( | ||
| 71 | scheduled_departure | ||
| 72 | realtime_departure | ||
| 73 | departure | ||
| 74 | scheduled_arrival | ||
| 75 | realtime_arrival | ||
| 76 | arrival | ||
| 77 | ) | ||
| 78 | ) | ||
| 79 | { | ||
| 80 | if ( $ret->{$timestamp_key} ) { | ||
| 81 | $ret->{$timestamp_key} = $ret->{$timestamp_key}->epoch; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | return $ret; | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; |