API-DeutscheBahn-Fahrplan

 view release on metacpan or  search on metacpan

lib/API/DeutscheBahn/Fahrplan.pm  view on Meta::CPAN

    $data = $fahrplan->location( name => 'Berlin' );
    $data = $fahrplan->arrival_board( id => 8503000, date => '2018-09-24T11:00:00' );
    $data = $fahrplan->departure_board( id => 8503000, date => '2018-09-24T11:00:00' );
    $data = $fahrplan->journey_details( id => '87510%2F49419%2F965692%2F453678%2F80%3fstation_evaId%3D850300' );

=head1 DESCRIPTION

API::DeutscheBahn::Fahrplan provides a simple interface to the Deutsche Bahn Fahrplan
API. See L<https://developer.deutschebahn.com/> for further information.

=head1 ATTRIBUTES

=over

=item fahrplan_free_url

URL endpoint for DB Fahrplan free version. Defaults to I<https://api.deutschebahn.com/freeplan/v1>.

=item fahrplan_plus_url

URL endpoint for DB Fahrplan subscribed version. Defaults to I<https://api.deutschebahn.com/fahrplan-plus/v1>.

=item access_token

Access token to sign requests. If provided the client will use the C<fahrplan_plus_url> endpoint.

=back

=cut

has 'fahrplan_free_url' => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://api.deutschebahn.com/freeplan/v1',
);

has 'fahrplan_plus_url' => (
    is      => 'ro',
    isa     => 'Str',
    default => 'https://api.deutschebahn.com/fahrplan-plus/v1',
);

has 'access_token' => (
    is  => 'ro',
    isa => 'Str',
);

has '_client' => (
    is      => 'ro',
    lazy    => 1,
    builder => '_build_client',
);


=head1 METHODS

=head2 location

    $fahrplan->location( name => 'Berlin' );

Fetch information about locations matching the given name or name fragment.

=cut

sub location {
    return shift->_request( 'location', @_ );
}

=head2 arrival_board

    $fahrplan->arrival_board( id => 8503000, date => '2018-09-24T11:00:00' );

Fetch the arrival board at a given location at a given date and time. The date
parameter should be in the ISO-8601 format.

=cut

sub arrival_board {
    return shift->_request( 'arrival_board', @_ );
}

=head2 departure_board

    $fahrplan->departure_board( id => 8503000, date => '2018-09-24T11:00:00' );

Fetch the departure board at a given location at a given date and time. The date
parameter should be in the ISO-8601 format.

=cut

sub departure_board {
    return shift->_request( 'departure_board', @_ );
}

=head2 journey_details

    $fahrplan->journey_details( id => '87510%2F49419%2F965692%2F453678%2F80%3fstation_evaId%3D850300' );

Retrieve details of a journey for a given id.

=cut

sub journey_details {
    my ( $self, %args ) = @_;
    return $self->_request( 'journey_details',
        # id needs to be uri encoded
        id => uri_encode( $args{id} ) );
}


# PRIVATE METHODS


sub _request {
    my ( $self, $name, %args ) = @_;
    my ( $method, $uri ) = $self->_create_uri( $name, %args );
    my $response = $self->_client->$method($uri);
    return JSON::XS::decode_json $response->{content};
}




( run in 1.546 second using v1.01-cache-2.11-cpan-b16cb0d3907 )