API-DeutscheBahn-Fahrplan
view release on metacpan or search on metacpan
b) cause the whole of any work that you distribute or publish, that
in whole or in part contains the Program or any part thereof, either
with or without modifications, to be licensed at no charge to all
third parties under the terms of this General Public License (except
that you may choose to grant warranty protection to some or all
third parties, at your option).
c) If the modified program normally reads commands interactively when
run, you must cause it, when started running for such interactive use
in the simplest and most usual way, to print or display an
announcement including an appropriate copyright notice and a notice
that there is no warranty (or else, saying that you provide a
warranty) and that users may redistribute the program under these
conditions, and telling the user how to view a copy of this General
Public License.
d) You may charge a fee for the physical act of transferring a
copy, and you may at your option offer warranty protection in
exchange for a fee.
lib/API/DeutscheBahn/Fahrplan.pm view on Meta::CPAN
sub _create_uri {
my ( $self, $name, %args ) = @_;
my $uri = $self->_base_uri;
my $definition = $self->_api->{$name};
my ( $method, $path ) = @{$definition}{qw(method path)};
# add path parameters
for ( @{ $definition->{path_parameters} } ) {
my $value = $args{$_};
croak sprintf 'Missing path parameter: %s', $_ unless $value;
$path .= "/${value}";
}
# set the uri path including the path set in the base url
$uri->path( $uri->path . $path );
# add query parameters
for my $param ( keys %{ $definition->{query_parameters} } ) {
if ( my $value = $args{$param} ) {
$uri->query_param( $param => $value );
}
# check if param is required
elsif ( $definition->{query_parameters}->{$param} ) {
croak sprintf 'Missing query parameter: %s', $param;
}
}
return ( lc $method, $uri );
}
sub _base_uri {
return URI->new(
lib/API/DeutscheBahn/Fahrplan.pm view on Meta::CPAN
}
# BUILDERS
sub _build_client {
my $self = $_[0];
my @args;
push @args, 'Authorization' => sprintf( 'Bearer %s', $self->access_token )
if $self->access_token;
return HTTP::Tiny->new(
default_headers => {
'Accept' => 'application/json',
'User-Agent' => sprintf( 'Perl-%s::%s', __PACKAGE__, $VERSION ),
@args,
},
);
}
1;
=head1 LICENSE
Copyright (C) Edward Francis.
t/01_main.t view on Meta::CPAN
note 'basic object tests';
my $fahrplan_free = API::DeutscheBahn::Fahrplan->new;
isa_ok $fahrplan_free, 'API::DeutscheBahn::Fahrplan';
my $fahrplan_plus = API::DeutscheBahn::Fahrplan->new( access_token => '123' );
isa_ok $fahrplan_plus, 'API::DeutscheBahn::Fahrplan';
# construct the user agent string
my $user_agent = sprintf 'Perl-API::DeutscheBahn::Fahrplan::%s',
$API::DeutscheBahn::Fahrplan::VERSION;
cmp_deeply $fahrplan_free->_client->default_headers,
{ Accept => 'application/json', 'User-Agent' => $user_agent },
'set correct headers for Fahrplan free';
cmp_deeply $fahrplan_plus->_client->default_headers,
{
Accept => 'application/json',
t/01_main.t view on Meta::CPAN
$_->{throws_regex}, 'error thrown successfully'
and next;
}
my ( undef, $uri ) = $fahrplan_free->_create_uri( $method, %params );
is $uri->scheme, 'https', 'uri scheme set to https';
is $uri->host, 'api.deutschebahn.com', 'uri host set';
like $uri->path_query, $_->{regex},
sprintf 'successfully created uri for %s', $method;
}
done_testing;
( run in 1.222 second using v1.01-cache-2.11-cpan-de7293f3b23 )