Finance-Dogecoin

 view release on metacpan or  search on metacpan

lib/Finance/Dogecoin/API.pm  view on Meta::CPAN

    # manually setting the 'a' parameter avoids a weird behavior in LWP::UA
    # which uppercases 'a'--not what the API expects or wants
    my $uri = URI->new( $self->endpoint );
    $uri->query_param( a => $method );

    while (my ($key, $value) = each %params) {
        $uri->query_param( $key => $value );
    }

    my $response = $self->ua->get( $uri );
    my $result   = $self->json->decode( $response->decoded_content );

    Carp::croak( "Bad API call from $method() call" ) if $result eq 'Bad Query';
    Carp::croak( "Invalid API key '" . $self->api_key . "'" )
        if $result eq 'Invalid API Key';

    return $result;
}

BEGIN {
    for my $non_api (qw( get_difficulty get_current_block get_current_price )) {

t/api_errors.t  view on Meta::CPAN

        'get_address_by_label() should throw error without address_label';
}

sub test_bad_query_exception {
    my $api = Finance::Dogecoin::API->new(
        ua      => $Mock_ua,
        api_key => 'hello, world',
    );

    $Mock_ua->mock(                   get => sub { $Mock_response } );
    $Mock_response->mock( decoded_content => sub { '"Bad Query"'  } );

    my @errors;
    local *Carp::croak;
    *Carp::croak = sub { push @errors, $_[0]; };

    $api->get_address_by_label;
    like $errors[-1], qr/Bad API call from get_address_by_label\(\) call/,
        'API calls should throw exception when remote API fails';

    $Mock_response->mock( decoded_content => sub { '"Invalid API Key"'  } );

    $api->get_balance;
    like $errors[-1], qr/Invalid API key 'hello, world'/,
        'API calls should throw exception with invalid API key';
}

t/mocked_api.t  view on Meta::CPAN

    test_get_address_received( $api );
    test_get_address_by_label( $api );

    done_testing;
    return 0;
}

sub make_json_response {
    state $json = do { my $j = JSON->new; $j->allow_nonref; $j };
    my $datum   = shift;
    $Mock_response->mock( decoded_content => sub { $json->encode( $datum ) } );
}

sub test_get_difficulty {
    my $api = shift;

    make_json_response( 1281.08277983 );
    is $api->get_difficulty, 1281.08277983,
        'get_difficulty() should return JSON decoded result';
}

sub test_get_current_block {
    my $api = shift;

    make_json_response( 75268 );
    is $api->get_current_block, 75268,
        'get_current_block() should return JSON decoded result';
}

sub test_get_current_price {
    my $api = shift;

    make_json_response( 0.0014510097 );
    is $api->get_current_price, 0.0014510097,
        'get_current_price() should return JSON decoded result';
}

sub test_get_balance {
    my $api = shift;

    make_json_response( 100.98 );
    is $api->get_balance, 100.98,
        'get_balance() should return JSON decoded result';
}

sub test_get_my_addresses {
    my $api = shift;

    make_json_response( [] );
    is_deeply $api->get_my_addresses, [],
        'get_my_addresses() should return empty arrayref with no addresses';

    make_json_response( [qw( addyone twoaddy addthress ) ] );

t/mocked_api.t  view on Meta::CPAN

    is $api->withdraw( payment_address => 'addthreess', amount => 404 ),
        'success',
        'withdraw() should return success on success';
}

sub test_get_new_address {
    my $api = shift;

    make_json_response( 'newaddress1' );
    is $api->get_new_address, 'newaddress1',
        'get_new_address() should return new address decoded from JSON';
}

sub test_get_address_received {
    my $api = shift;

    make_json_response( 888.77 );
    is $api->get_address_received( address_label => 'newaddress1' ), 888.77,
        'get_address_received() should return value decoded from JSON';

    make_json_response( 777.88 );
    is $api->get_address_received( payment_address => '1234abcd' ), 777.88,
        'get_address_received() should return value decoded from JSON';
}

sub test_get_address_by_label {
    my $api = shift;

    make_json_response( '1234abcd' );
    is $api->get_address_by_label( address_label => 'newaddress1' ),
        '1234abcd',
        'get_address_by_label() should return address decoded from JSON';
}



( run in 0.242 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )