Finance-MtGox
view release on metacpan or search on metacpan
lib/Finance/MtGox.pm view on Meta::CPAN
package Finance::MtGox;
use warnings;
use strict;
use Carp qw( croak );
use JSON::Any;
use WWW::Mechanize;
use URI;
use Time::HiRes qw( gettimeofday );
use Digest::SHA qw( hmac_sha512 );
use MIME::Base64;
=head1 NAME
Finance::MtGox - trade Bitcoin with the MtGox API
=head1 VERSION
Version 0.50
=cut
our $VERSION = '0.50';
=head1 SYNOPSIS
use Finance::MtGox;
my $mtgox = Finance::MtGox->new({
key => 'api key',
secret => 'api secret'
});
# unauthenticated API calls
my $depth = $mtgox->call('getDepth');
# authenticated API calls
my $funds = $mtgox->call_auth('generic/info');
# convenience methods built on the core API
my ( $btcs, $usds ) = $mtgox->balances;
my $rate = $mtgox->clearing_rate( 'asks', 200, 'BTC' );
$rate = $mtgox->clearing_rate( 'bids', 42, 'USD' );
=head1 BASIC METHODS
=head2 new
Create a new C<Finance::MtGox> object with your MtGox credentials provided
in the C<key> and C<secret> arguments.
=cut
sub new {
my ( $class, $args ) = @_;
$args->{key} && $args->{secret}
or croak "You must provide 'key' and 'secret' credentials.";
$args->{json} = JSON::Any->new;
$args->{mech} = WWW::Mechanize->new(stack_depth => 0);
return bless $args, $class;
}
=head2 call( $name )
Run the API call named C<$name>. Returns a Perl data structure
representing the JSON returned from MtGox.
=cut
sub call {
my ( $self, $name ) = @_;
croak "You must provide an API method" if not $name;
my $version = $self->_version_from_name($name);
my $req = $self->_build_api_method_request( 'GET',
$version,
$name,
$version == 0 ? 'data': '' );
$self->_mech->request($req);
return $self->_decode;
}
=head2 call_auth( $name, $args )
Run the API call named C<$name> with arguments provided by the hashref
C<$args>. Returns a Perl data structure representing the JSON returned
from MtGox
=cut
sub call_auth {
my ( $self, $name, $args ) = @_;
croak "You must provide an API method" if not $name;
my $version = $self->_version_from_name($name);
$args ||= {};
my $req = $self->_build_api_method_request( 'POST', $version, $name, '', $args );
$self->_mech->request($req);
return $self->_decode;
}
=head1 CONVENIENCE METHODS
=head2 balances
Returns a list with current BTC and C<$currency> account balances,
respectively. If C<$currency> is not specified it defaults to USD.
=cut
sub balances {
my ($self, $currency) = @_;
$currency ||= 'USD';
my $result = $self->call_auth('info');
( run in 1.862 second using v1.01-cache-2.11-cpan-22024b96cdf )