blib/lib/Finance/Bitcoin/Yacuna.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 199 CC 23 SCAR 37.1
CC
23
Cov
0%
call
132.0
new
20.0
prepareAuth
12.0
CRAP 40.7
1 package Finance::Bitcoin::Yacuna;
2  
3 #
4 # $Id: Yacuna.pm 2 2014-07-01 11:07:37Z martchouk $
5 #
6 # Yacuna API connector
7 # author, (c): Andrei Martchouk <andrei at yacuna dot com>
8 #
9  
10 use strict;
11 no strict 'subs';
12 use warnings;
13  
14 use WWW::Mechanize;
15 use HTTP::Request;
16 use MIME::Base64;
17 use Digest::SHA qw(sha512_hex);
18 use Data::Dump qw(dump);
19  
20 require Exporter;
21 our @ISA = qw(Exporter);
22 our $VERSION = "0.1";
23  
24 my $conf = {
25 prod => {
26 host => 'https://yacuna.com',
27 basePath => '/api/',
28 apiVersion => '1'
29 },
30 sandbox => {
31 host => 'https://sandbox.yacuna.com',
32 basePath => '/api/',
33 apiVersion => '1'
34 },
35 debug => 0
36 };
37  
38  
39 sub new{
40 my $type = shift;
41 my %params = @_;
42 my $httpClient = new WWW::Mechanize;
43 if($params{'skipSSL'} && (int$params{'skipSSL'})>0){
44 $httpClient->{'ssl_opts'} = {
45 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
46 verify_hostname => 0, # this key is likely going to be removed in future LWP >6.04
47 };
48 }
49  
50 my $config = {};
51  
52 $config = $params{'sandbox'} && (int$params{'sandbox'})>0 ? $conf->{'sandbox'} : $conf->{'prod'};
53  
54 $config->{debug} = $params{'debug'} && (int$params{'debug'})>0 ? $params{'debug'} : 0;
55  
56 bless{
57 'uri' => $config->{'host'},
58 'basePath' => $config->{'basePath'},
59 'apiVersion' => $params{'apiVersion'} || $config->{'apiVersion'},
60 'tokenId' => $params{'tokenId'},
61 'secret'=> $params{'secret'},
62 'SKIP_SSL'=>$params{'skipSSL'},
63 'httpClient'=> $httpClient,
64 'debug' => $config->{debug}
65 },
66 $type;
67 }
68  
69 sub uri {
70 return $_[0]->{'uri'} unless $_[1];
71 $_[0]->{'uri'} = $_[1]
72 }
73  
74 sub basePath {
75 return $_[0]->{'basePath'} unless $_[1];
76 $_[0]->{'basePath'} = $_[1]
77 }
78  
79 sub apiVersion {
80 return $_[0]->{'apiVersion'} unless $_[1];
81 $_[0]->{'apiVersion'} = $_[1]
82 }
83  
84 sub tokenId {
85 return $_[0]->{'tokenId'} unless $_[1];
86 $_[0]->{'tokenId'} = $_[1]
87 }
88  
89 sub secret {
90 return $_[0]->{'secret'} unless $_[1];
91 $_[0]->{'secret'} = $_[1]
92 }
93  
94 sub debug {
95 return $_[0]->{'debug'} unless $_[1];
96 $_[0]->{'debug'} = $_[1]
97 }
98  
99 sub SKIP_SSL {
100 return $_[0]->{'SKIP_SSL'} unless $_[1];
101 $_[0]->{'SKIP_SSL'} = $_[1]
102 }
103  
104 sub call {
105 my $self = shift;
106 my $httpMethod = $_[0];
107 my $restPath = $_[1];
108  
109 eval{
110 my $qry = defined $_[2] && scalar @{$_[2]}>0 ? (join "&", sort @{$_[2]}):undef;
111 my $body = '';
112 if('GET' eq $httpMethod){
113 $restPath .= "?$qry" if defined $qry && '' ne $qry;
114 print "\n$httpMethod ". $self->{'uri'}.$self->{'basePath'}.$self->{'apiVersion'}.'/'.$restPath if $self->{'debug'} > 0;
115 }
116 elsif('POST' eq $httpMethod){
117 $body = $qry;
118 print "\n$httpMethod ". $self->{'uri'}.$self->{'basePath'}.$self->{'apiVersion'}.'/'.$restPath."\n".$body if $self->{'debug'} > 0;
119 }
120
121 # authentication not needed for some public calls
122 if(defined $self->{'secret'} && defined $self->{'tokenId'}){
123 my $apiToken = &prepareAuth($self->{'basePath'}.$self->{'apiVersion'}.'/'.$restPath, $body, $httpMethod, $self->{'secret'}, $self->{'debug'});
124 $self->{'httpClient'}->add_header( 'Api-Token-Id' => $self->{'tokenId'}, 'Api-Token' => $apiToken, 'Api-Token-OTP'=>'');
125 }
126  
127 if('GET' eq $httpMethod){
128 $self->{'httpClient'}->get($self->{'uri'} . $self->{'basePath'} . $self->{'apiVersion'}.'/'. $restPath);
129 }
130 elsif('POST' eq $httpMethod){
131 my $req = new HTTP::Request('POST', $self->{'uri'} . $self->{'basePath'} . $self->{'apiVersion'}.'/'.$restPath);
132 $req->content_type('application/x-www-form-urlencoded');
133 $req->content($body);
134 my $res = $self->{'httpClient'}->request($req);
135 return $res->decoded_content;
136 }
137  
138 };
139 return $self->{'httpClient'}->response->decoded_content if $self->{'httpClient'}->response;
140 }
141  
142 sub prepareAuth(){
143 my ($path, $body, $httpMethod, $apiSecret, $debug) = @_;
144 my $tokenSalt = ''.time*1000;
145 my $hashInput = $tokenSalt.'@'.$apiSecret.'@'.$httpMethod.'@'.$path;
146 $hashInput .= '@'.$body if '' ne $body;
147 my $apiToken = $tokenSalt.'T'.(sha512_hex($hashInput));
148
149 if(defined $debug && $debug > 0){
150 print "\nhashInput => $hashInput \n";
151 print "apiToken => $apiToken \n";
152 }
153  
154 return $apiToken;
155 }
156  
157 1;
158  
159 __END__