API-DeutscheBahn-Fahrplan

 view release on metacpan or  search on metacpan

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

    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;

t/01_main.t  view on Meta::CPAN

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',
    'User-Agent'  => $user_agent,
    Authorization => 'Bearer 123',
    },
    'set correct headers for Fahrplan plus';


note 'testing uri generation';


my @uri_tests = (

    # Successful

    {    #

t/01_main.t  view on Meta::CPAN

    my ( $method, %params ) = ( $_->{method}, %{ $_->{params} } );

    if ( $_->{throws} ) {
        throws_ok { $fahrplan_free->_create_uri( $method, %params ) }
        $_->{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 0.511 second using v1.01-cache-2.11-cpan-49f99fa48dc )