| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Coincheck; | 
| 2 | 3 |  |  | 3 |  | 803356 | use strict; | 
|  | 3 |  |  |  |  | 30 |  | 
|  | 3 |  |  |  |  | 128 |  | 
| 3 | 3 |  |  | 3 |  | 25 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 184 |  | 
| 4 | 3 |  |  | 3 |  | 25 | use Carp qw/croak/; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 250 |  | 
| 5 | 3 |  |  | 3 |  | 2168 | use HTTP::Tiny; | 
|  | 3 |  |  |  |  | 136785 |  | 
|  | 3 |  |  |  |  | 152 |  | 
| 6 | 3 |  |  | 3 |  | 1822 | use Time::HiRes qw/time/; | 
|  | 3 |  |  |  |  | 4349 |  | 
|  | 3 |  |  |  |  | 15 |  | 
| 7 | 3 |  |  | 3 |  | 2489 | use URI::Query; | 
|  | 3 |  |  |  |  | 27944 |  | 
|  | 3 |  |  |  |  | 217 |  | 
| 8 | 3 |  |  | 3 |  | 1585 | use Digest::SHA qw/hmac_sha256_hex/; | 
|  | 3 |  |  |  |  | 9015 |  | 
|  | 3 |  |  |  |  | 293 |  | 
| 9 | 3 |  |  | 3 |  | 1365 | use String::CamelCase qw/camelize/; | 
|  | 3 |  |  |  |  | 1717 |  | 
|  | 3 |  |  |  |  | 165 |  | 
| 10 | 3 |  |  | 3 |  | 1338 | use Class::Load qw/load_class/; | 
|  | 3 |  |  |  |  | 53658 |  | 
|  | 3 |  |  |  |  | 224 |  | 
| 11 | 3 |  |  | 3 |  | 1794 | use JSON qw//; | 
|  | 3 |  |  |  |  | 28419 |  | 
|  | 3 |  |  |  |  | 161 |  | 
| 12 |  |  |  |  |  |  | use Class::Accessor::Lite ( | 
| 13 | 3 |  |  |  |  | 28 | ro  => [qw/ | 
| 14 |  |  |  |  |  |  | api_base | 
| 15 |  |  |  |  |  |  | access_key | 
| 16 |  |  |  |  |  |  | secret_key | 
| 17 |  |  |  |  |  |  | ticker | 
| 18 |  |  |  |  |  |  | trade | 
| 19 |  |  |  |  |  |  | order_book | 
| 20 |  |  |  |  |  |  | order | 
| 21 |  |  |  |  |  |  | leverage | 
| 22 |  |  |  |  |  |  | account | 
| 23 |  |  |  |  |  |  | send | 
| 24 |  |  |  |  |  |  | deposit | 
| 25 |  |  |  |  |  |  | bank_account | 
| 26 |  |  |  |  |  |  | withdraw | 
| 27 |  |  |  |  |  |  | borrow | 
| 28 |  |  |  |  |  |  | transfer | 
| 29 |  |  |  |  |  |  | /], | 
| 30 |  |  |  |  |  |  | rw  => [qw/ | 
| 31 |  |  |  |  |  |  | client | 
| 32 |  |  |  |  |  |  | signature | 
| 33 |  |  |  |  |  |  | nonce | 
| 34 |  |  |  |  |  |  | decode_json | 
| 35 |  |  |  |  |  |  | /], | 
| 36 | 3 |  |  | 3 |  | 1408 | ); | 
|  | 3 |  |  |  |  | 2932 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | our $VERSION = '0.04'; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub new { | 
| 41 | 1 |  |  | 1 | 1 | 96 | my $class = shift; | 
| 42 | 1 |  |  |  |  | 8 | my %args  = @_; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  |  |  | 17 | my $self = bless { | 
| 45 |  |  |  |  |  |  | decode_json => 0, | 
| 46 |  |  |  |  |  |  | api_base    => 'https://coincheck.jp/', | 
| 47 |  |  |  |  |  |  | %args, | 
| 48 |  |  |  |  |  |  | }, $class; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 1 |  |  |  |  | 10 | $self->_initialize($args{client_opt}); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 1 |  |  |  |  | 11 | return $self; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _initialize { | 
| 56 | 1 |  |  | 1 |  | 6 | my ($self, $client_opt) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | $self->client( | 
| 59 |  |  |  |  |  |  | HTTP::Tiny->new( | 
| 60 |  |  |  |  |  |  | agent   => __PACKAGE__ . "/$VERSION", | 
| 61 |  |  |  |  |  |  | default_headers => { | 
| 62 |  |  |  |  |  |  | 'Content-Type' => 'application/json', | 
| 63 |  |  |  |  |  |  | 'ACCESS-KEY'   => $self->access_key, | 
| 64 |  |  |  |  |  |  | }, | 
| 65 |  |  |  |  |  |  | timeout => 15, | 
| 66 |  |  |  |  |  |  | verify_SSL => 1, | 
| 67 | 1 | 50 |  |  |  | 11 | %{$client_opt || {}}, | 
|  | 1 |  |  |  |  | 31 |  | 
| 68 |  |  |  |  |  |  | ) | 
| 69 |  |  |  |  |  |  | ); | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 1 |  |  |  |  | 124 | for my $api (qw/ | 
| 72 |  |  |  |  |  |  | ticker | 
| 73 |  |  |  |  |  |  | trade | 
| 74 |  |  |  |  |  |  | order_book | 
| 75 |  |  |  |  |  |  | order | 
| 76 |  |  |  |  |  |  | leverage | 
| 77 |  |  |  |  |  |  | account | 
| 78 |  |  |  |  |  |  | send | 
| 79 |  |  |  |  |  |  | deposit | 
| 80 |  |  |  |  |  |  | bank_account | 
| 81 |  |  |  |  |  |  | withdraw | 
| 82 |  |  |  |  |  |  | borrow | 
| 83 |  |  |  |  |  |  | transfer | 
| 84 |  |  |  |  |  |  | /) { | 
| 85 | 12 |  |  |  |  | 47 | my $klass = 'WebService::Coincheck::' . camelize($api); | 
| 86 | 12 |  |  |  |  | 220 | $self->{$api} = load_class($klass)->new($self); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub set_signature { | 
| 91 | 0 |  |  | 0 | 1 |  | my ($self, $req_url, $body) = @_; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 |  |  |  |  |  | $self->nonce(int(time * 10000)); | 
| 94 | 0 |  | 0 |  |  |  | $self->signature(hmac_sha256_hex($self->nonce . $req_url . ($body || ''), $self->secret_key)); | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub request { | 
| 98 | 0 |  |  | 0 | 1 |  | my ($self, $method, $req_path, $query) = @_; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 |  |  |  |  |  | my $res; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 | 0 |  |  |  |  | if ($method =~ m!^get$!i) { | 
|  |  | 0 |  |  |  |  |  | 
| 103 | 0 |  |  |  |  |  | my $query_string = URI::Query->new($query)->stringify; | 
| 104 | 0 | 0 |  |  |  |  | my $req_url = join '', $self->api_base, $req_path, $query_string ? "?$query_string" : ''; | 
| 105 | 0 |  |  |  |  |  | $self->set_signature($req_url); | 
| 106 | 0 |  |  |  |  |  | $res = $self->client->get( | 
| 107 |  |  |  |  |  |  | $req_url, | 
| 108 |  |  |  |  |  |  | { | 
| 109 |  |  |  |  |  |  | headers => { | 
| 110 |  |  |  |  |  |  | 'ACCESS-NONCE'     => $self->nonce, | 
| 111 |  |  |  |  |  |  | 'ACCESS-SIGNATURE' => $self->signature, | 
| 112 |  |  |  |  |  |  | }, | 
| 113 |  |  |  |  |  |  | }, | 
| 114 |  |  |  |  |  |  | ); | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | elsif ($method =~ m!^(?:post|delete)$!i) { | 
| 117 | 0 |  |  |  |  |  | my $req_url = join '', $self->api_base, $req_path; | 
| 118 | 0 | 0 |  |  |  |  | my $content = $query ? JSON::encode_json($query) : ''; | 
| 119 | 0 |  |  |  |  |  | $self->set_signature($req_url, $content); | 
| 120 | 0 |  |  |  |  |  | $res = $self->client->request( | 
| 121 |  |  |  |  |  |  | uc $method, | 
| 122 |  |  |  |  |  |  | $req_url, | 
| 123 |  |  |  |  |  |  | { | 
| 124 |  |  |  |  |  |  | content => $content, | 
| 125 |  |  |  |  |  |  | headers => { | 
| 126 |  |  |  |  |  |  | 'ACCESS-NONCE'     => $self->nonce, | 
| 127 |  |  |  |  |  |  | 'ACCESS-SIGNATURE' => $self->signature, | 
| 128 |  |  |  |  |  |  | }, | 
| 129 |  |  |  |  |  |  | }, | 
| 130 |  |  |  |  |  |  | ); | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 | 0 |  |  |  |  | unless ($res->{success}) { | 
| 134 | 0 |  |  |  |  |  | croak "Error:" . join "\t", map { $res->{$_} } (qw/url status reason content/); | 
|  | 0 |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 0 | 0 |  |  |  |  | if ($self->decode_json) { | 
| 138 | 0 |  |  |  |  |  | return JSON::decode_json($res->{content}); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  | else { | 
| 141 | 0 |  |  |  |  |  | return $res->{content}; | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | 1; | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | __END__ |