| 1 | package API::DeutscheBahn::Fahrplan; | ||
| 2 | |||
| 3 | 2 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | |||
| 6 | # VERSION | ||
| 7 | our $VERSION = '0.01'; | ||
| 8 | |||
| 9 | # IMPORTS | ||
| 10 | use Carp; | ||
| 11 | use HTTP::Tiny (); | ||
| 12 | use JSON::XS (); | ||
| 13 | use URI (); | ||
| 14 | use URI::Encode qw(uri_encode); | ||
| 15 | use URI::QueryParam (); | ||
| 16 | |||
| 17 | =encoding utf-8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | API::DeutscheBahn::Fahrplan - Deutsche Bahn Fahrplan API Client | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | |||
| 26 | my $fahrplan_free = API::DeutscheBahn::Fahrplan->new; | ||
| 27 | my $fahrplan_plus = API::DeutscheBahn::Fahrplan->new( access_token => $access_token ); | ||
| 28 | |||
| 29 | $data = $fahrplan->location( name => 'Berlin' ); | ||
| 30 | $data = $fahrplan->arrival_board( id => 8503000, date => '2018-09-24T11:00:00' ); | ||
| 31 | $data = $fahrplan->departure_board( id => 8503000, date => '2018-09-24T11:00:00' ); | ||
| 32 | $data = $fahrplan->journey_details( id => '87510%2F49419%2F965692%2F453678%2F80%3fstation_evaId%3D850300' ); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | API::DeutscheBahn::Fahrplan provides a simple interface to the Deutsche Bahn Fahrplan | ||
| 37 | API. See L<https://developer.deutschebahn.com/> for further information. | ||
| 38 | |||
| 39 | =head1 ATTRIBUTES | ||
| 40 | |||
| 41 | =over | ||
| 42 | |||
| 43 | =item fahrplan_free_url | ||
| 44 | |||
| 45 | URL endpoint for DB Fahrplan free version. Defaults to I<https://api.deutschebahn.com/freeplan/v1>. | ||
| 46 | |||
| 47 | =item fahrplan_plus_url | ||
| 48 | |||
| 49 | URL endpoint for DB Fahrplan subscribed version. Defaults to I<https://api.deutschebahn.com/fahrplan-plus/v1>. | ||
| 50 | |||
| 51 | =item access_token | ||
| 52 | |||
| 53 | Access token to sign requests. If provided the client will use the C<fahrplan_plus_url> endpoint. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | has 'fahrplan_free_url' => ( | ||
| 60 | is => 'ro', | ||
| 61 | isa => 'Str', | ||
| 62 | default => 'https://api.deutschebahn.com/freeplan/v1', | ||
| 63 | ); | ||
| 64 | |||
| 65 | has 'fahrplan_plus_url' => ( | ||
| 66 | is => 'ro', | ||
| 67 | isa => 'Str', | ||
| 68 | default => 'https://api.deutschebahn.com/fahrplan-plus/v1', | ||
| 69 | ); | ||
| 70 | |||
| 71 | has 'access_token' => ( | ||
| 72 | is => 'ro', | ||
| 73 | isa => 'Str', | ||
| 74 | ); | ||
| 75 | |||
| 76 | has '_client' => ( | ||
| 77 | is => 'ro', | ||
| 78 | lazy => 1, | ||
| 79 | builder => '_build_client', | ||
| 80 | ); | ||
| 81 | |||
| 82 | |||
| 83 | =head1 METHODS | ||
| 84 | |||
| 85 | =head2 location | ||
| 86 | |||
| 87 | $fahrplan->location( name => 'Berlin' ); | ||
| 88 | |||
| 89 | Fetch information about locations matching the given name or name fragment. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub location { | ||
| 94 | return shift->_request( 'location', @_ ); | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 arrival_board | ||
| 98 | |||
| 99 | $fahrplan->arrival_board( id => 8503000, date => '2018-09-24T11:00:00' ); | ||
| 100 | |||
| 101 | Fetch the arrival board at a given location at a given date and time. The date | ||
| 102 | parameter should be in the ISO-8601 format. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub arrival_board { | ||
| 107 | return shift->_request( 'arrival_board', @_ ); | ||
| 108 | } | ||
| 109 | |||
| 110 | =head2 departure_board | ||
| 111 | |||
| 112 | $fahrplan->departure_board( id => 8503000, date => '2018-09-24T11:00:00' ); | ||
| 113 | |||
| 114 | Fetch the departure board at a given location at a given date and time. The date | ||
| 115 | parameter should be in the ISO-8601 format. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub departure_board { | ||
| 120 | return shift->_request( 'departure_board', @_ ); | ||
| 121 | } | ||
| 122 | |||
| 123 | =head2 journey_details | ||
| 124 | |||
| 125 | $fahrplan->journey_details( id => '87510%2F49419%2F965692%2F453678%2F80%3fstation_evaId%3D850300' ); | ||
| 126 | |||
| 127 | Retrieve details of a journey for a given id. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub journey_details { | ||
| 132 | my ( $self, %args ) = @_; | ||
| 133 | return $self->_request( 'journey_details', | ||
| 134 | # id needs to be uri encoded | ||
| 135 | id => uri_encode( $args{id} ) ); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | # PRIVATE METHODS | ||
| 140 | |||
| 141 | |||
| 142 | sub _request { | ||
| 143 | my ( $self, $name, %args ) = @_; | ||
| 144 | my ( $method, $uri ) = $self->_create_uri( $name, %args ); | ||
| 145 | my $response = $self->_client->$method($uri); | ||
| 146 | return JSON::XS::decode_json $response->{content}; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub _create_uri { | ||
| 151 | my ( $self, $name, %args ) = @_; | ||
| 152 | |||
| 153 | my $uri = $self->_base_uri; | ||
| 154 | my $definition = $self->_api->{$name}; | ||
| 155 | my ( $method, $path ) = @{$definition}{qw(method path)}; | ||
| 156 | |||
| 157 | # add path parameters | ||
| 158 | for ( @{ $definition->{path_parameters} } ) { | ||
| 159 | my $value = $args{$_}; | ||
| 160 | croak sprintf 'Missing path parameter: %s', $_ unless $value; | ||
| 161 | $path .= "/${value}"; | ||
| 162 | } | ||
| 163 | |||
| 164 | # set the uri path including the path set in the base url | ||
| 165 | $uri->path( $uri->path . $path ); | ||
| 166 | |||
| 167 | # add query parameters | ||
| 168 | for my $param ( keys %{ $definition->{query_parameters} } ) { | ||
| 169 | if ( my $value = $args{$param} ) { | ||
| 170 | $uri->query_param( $param => $value ); | ||
| 171 | } | ||
| 172 | # check if param is required | ||
| 173 | elsif ( $definition->{query_parameters}->{$param} ) { | ||
| 174 | croak sprintf 'Missing query parameter: %s', $param; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | return ( lc $method, $uri ); | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | sub _base_uri { | ||
| 184 | return URI->new( | ||
| 185 | $_[0]->access_token | ||
| 186 | ? $_[0]->fahrplan_plus_url | ||
| 187 | : $_[0]->fahrplan_free_url | ||
| 188 | ); | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub _api { | ||
| 193 | return { | ||
| 194 | location => { | ||
| 195 | method => 'GET', | ||
| 196 | path => '/location', | ||
| 197 | path_parameters => ['name'], | ||
| 198 | }, | ||
| 199 | arrival_board => { | ||
| 200 | method => 'GET', | ||
| 201 | path => '/arrivalBoard', | ||
| 202 | path_parameters => ['id'], | ||
| 203 | query_parameters => { date => 1 }, | ||
| 204 | }, | ||
| 205 | departure_board => { | ||
| 206 | method => 'GET', | ||
| 207 | path => '/departureBoard', | ||
| 208 | path_parameters => ['id'], | ||
| 209 | query_parameters => { date => 1 }, | ||
| 210 | }, | ||
| 211 | journey_details => { | ||
| 212 | method => 'GET', | ||
| 213 | path => '/journeyDetails', | ||
| 214 | path_parameters => ['id'], | ||
| 215 | }, | ||
| 216 | }; | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | # BUILDERS | ||
| 221 | |||
| 222 | |||
| 223 | sub _build_client { | ||
| 224 | my $self = $_[0]; | ||
| 225 | my @args; | ||
| 226 | |||
| 227 | push @args, 'Authorization' => sprintf( 'Bearer %s', $self->access_token ) | ||
| 228 | if $self->access_token; | ||
| 229 | |||
| 230 | return HTTP::Tiny->new( | ||
| 231 | default_headers => { | ||
| 232 | 'Accept' => 'application/json', | ||
| 233 | 'User-Agent' => sprintf( 'Perl-%s::%s', __PACKAGE__, $VERSION ), | ||
| 234 | @args, | ||
| 235 | }, | ||
| 236 | ); | ||
| 237 | } | ||
| 238 | |||
| 239 | 1; | ||
| 240 | |||
| 241 | =head1 LICENSE | ||
| 242 | |||
| 243 | Copyright (C) Edward Francis. | ||
| 244 | |||
| 245 | This library is free software; you can redistribute it and/or modify | ||
| 246 | it under the same terms as Perl itself. | ||
| 247 | |||
| 248 | =head1 AUTHOR | ||
| 249 | |||
| 250 | Edward Francis E<lt>edwardafrancis@gmail.comE<gt> | ||
| 251 | |||
| 252 | =cut | ||