Travel-Status-MOTIS

 view release on metacpan or  search on metacpan

lib/Travel/Status/MOTIS/Trip.pm  view on Meta::CPAN

package Travel::Status::MOTIS::Trip;

use strict;
use warnings;
use 5.020;

use parent 'Class::Accessor';

use DateTime::Format::ISO8601;

use Travel::Status::MOTIS::Stop;
use Travel::Status::MOTIS::Polyline qw(decode_polyline);

our $VERSION = '0.03';

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

	  is_realtime
	  is_cancelled

	  arrival
	  scheduled_arrival
	  realtime_arrival

	  departure
	  scheduled_departure
	  realtime_departure
	)
);

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

	my $json = $opt{json}{legs}[0];
	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},

		raw_stopovers =>
		  [ $json->{from}, @{ $json->{intermediateStops} }, $json->{to} ],
		raw_polyline => $json->{legGeometry},

		time_zone    => $time_zone,
	};

	$ref->{scheduled_departure} = DateTime::Format::ISO8601->parse_datetime(
		$json->{scheduledStartTime} );
	$ref->{scheduled_departure}->set_time_zone( $time_zone );

	if ( $json->{realTime} ) {
		$ref->{realtime_departure}
		  = DateTime::Format::ISO8601->parse_datetime( $json->{startTime} );
		$ref->{realtime_departure}->set_time_zone( $time_zone );
	}

	$ref->{departure} = $ref->{realtime_departure}
	  // $ref->{scheduled_departure};

	$ref->{scheduled_arrival}
	  = DateTime::Format::ISO8601->parse_datetime( $json->{scheduledEndTime} );
	$ref->{scheduled_arrival}->set_time_zone( $time_zone );

	if ( $json->{realTime} ) {
		$ref->{realtime_arrival}
		  = DateTime::Format::ISO8601->parse_datetime( $json->{endTime} );
		$ref->{realtime_arrival}->set_time_zone( $time_zone );
	}

	$ref->{arrival} = $ref->{realtime_arrival} // $ref->{scheduled_arrival};

	bless( $ref, $obj );

	return $ref;
}

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

	if ( not $self->{raw_polyline} ) {
		return;
	}

	if ( $self->{polyline} ) {
		return @{ $self->{polyline} };
	}

	my $polyline = [ decode_polyline( $self->{raw_polyline} ) ];

	my $gis_distance;

	eval {
		require GIS::Distance;
		$gis_distance = GIS::Distance->new;
	};



( run in 0.953 second using v1.01-cache-2.11-cpan-6de40a662fe )